Skip to content

Niche Optimization for Option<ReferenceId> and IndexVec<ReferenceId, T> #3318

@Boshen

Description

@Boshen

we have these index types

define_index_type! {
pub struct ReferenceId = u32;
}

define_index_type! {
pub struct SymbolId = u32;
}

where

pub reference_id: Cell<Option<ReferenceId>>,

symbol_id: Option<SymbolId>,

can be trimmed by niche optimization.

But,

use std::num::NonZeroU32;
define_index_type! {
    pub struct ReferenceId = NonZeroU32;
}

is not supported by the macro define_index_type

error[E0599]: no function or associated item named `max_value` found for struct `NonZero` in the current scope
   --> crates/oxc_syntax/src/reference.rs:7:1
    |
7   | / define_index_type! {
8   | |     pub struct ReferenceId = NonZeroU32;
9   | | }
    | |_^ function or associated item not found in `NonZero<u32>`
    |
note: if you're trying to build a new `NonZero<u32>` consider using one of the following associated functions:
      NonZero::<T>::new
      NonZero::<T>::new_unchecked
   --> /Users/boshen/.rustup/toolchains/1.78.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/num/nonzero.rs:308:5

but the good news is that we forked it and lives in https://github.com/oxc-project/oxc/tree/main/crates/oxc_index

The task of this issue is to make it work, if possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory - New feature or requestC-performanceCategory - Solution not expected to change functional behavior, only performanceE-Help WantedExperience level - For the experienced collaborators

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions