oxc-project / oxc-project/backlog

Method to split a `Box<T>` into multiple `Box`es for each of its fields

Open
#172 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
7
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Similar to #162.

Sometimes we pull apart a Box<T> and then reallocate some of its fields in Boxes again.

We could codegen methods to convert a Box<T> into multiple Boxes for each field, without any reallocations.

e.g.:

pub struct FormalParameter<'a> {
    pub span: Span,
    pub decorators: Vec<'a, Decorator<'a>>,
    pub pattern: BindingPattern<'a>,
    pub accessibility: Option<TSAccessibility>,
    pub readonly: bool,
    pub r#override: bool,
}
trait IntoBoxedFields {
    type Fields;
    fn into_boxed_fields(self) -> Self::Fields;
}
// Codegenned for all AST structs

pub struct FormalParameterBoxedFields<'a> {
    pub span: Box<'a, Span>,
    pub decorators: Box<'a, Vec<'a, Decorator<'a>>>,
    pub pattern: Box<'a, BindingPattern<'a>>,
    pub accessibility: Box<'a, Option<TSAccessibility>>,
    pub readonly: Box<'a, bool>,
    pub r#override: Box<'a, bool>,
}

impl<'a> IntoBoxedFields for Box<'a, FormalParameter<'a>> {
    type Fields = FormalParameterBoxedFields;

    fn into_boxed_fields(self) -> Self::Fields {
        let ptr = Box::into_non_null(self).as_ptr();
        unsafe {
            FormalParameterBoxedFields {
                span: Box::from_ptr(addr_of_mut!((*ptr).span)),
                decorators: Box::from_ptr(addr_of_mut!((*ptr).decorators)),
                pattern: Box::from_ptr(addr_of_mut!((*ptr).pattern)),
                accessibility: Box::from_ptr(addr_of_mut!((*ptr).accessibility)),
                readonly: Box::from_ptr(addr_of_mut!((*ptr).readonly)),
                r#override: Box::from_ptr(addr_of_mut!((*ptr).r#override)),
            }
        }
    }
}

Most of the fields are not useful (why would you want a Box<bool>?). But extracting a Box<'a, BindingPattern<'a>> from Box<'a, FormalParameter<'a>> can be useful.

// Extract 1 field
fn foo(param: Box<'a, FormalParameter<'a>>) -> Box<'a, BindingPattern<'a>> {
    param.into_boxed_fields().pattern
}

// Extract multiple fields
fn bar(param: Box<'a, FormalParameter<'a>>) -> (
    Box<'a, BindingPattern<'a>>,
    Box<'a, Option<TSAccessibility>>,
) {
    let FormalParameterBoxedFields { pattern, accessibility, .. } = param.into_boxed_fields();
    (pattern, accessibility)
}

Compiler will elide the code for any fields you don't use. It avoids using unbox which pulls the whole struct onto the stack, and you don't have to reallocate the data for fields back into the arena again later.

Could also provide more flexibility with a macro:

let (pattern, readonly) = extract_fields!(param, (@box pattern, readonly));
// pattern: Box<'a, BindingPattern<'a>>
// readonly: bool

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading issue #162 and reviewing the proposed IntoBoxedFields trait and extract_fields! macro. Inspect how AST structs are code-generated and how Box::into_non_null and Box::from_ptr are supported. Done requires an agreed design for generated boxed-field extraction, including which fields and API should be supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.