Skip to content

Add Address API #4807

@overlookmotel

Description

@overlookmotel

Rolldown is using Spans as unique identifiers for AST nodes.

As I've said I think Span is the wrong tool for this job - Spans by their nature just aren't unique.

We have been discussing adding AstNodeId to all AST nodes, which would be a unique ID, but we've not reached a decision whether to do that or not yet.

There is one other unique identifier available to us - the address of structs in memory. Our bump allocator does not reuse freed memory, and it's guaranteed that the contents of a Box never moves in memory. So if T is in a Box<T>, the memory address of T is guaranteed to be stable and unique for as long as the allocator containing the AST is "live".

Add an API like this:

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Address(usize);

impl<'a, T> oxc_allocator::Box<'a, T> {
    pub fn address(&self) -> Address {
        Address(std::ptr::addr_of!(**self) as usize)
    }
}

I know that on face of it, this looks "hacky", but I feel it's completely legitimate. Our choice of using an arena allocator gives us certain extra powers and guarantees - why not take advantage of them?

Memory address of a &T is also stable, as long as the T is in a Box<T> in arena. But to avoid mistakes (e.g. &T refers to a T on the stack, not in arena), I think better to only implement for Box.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory - New feature or request

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions