Skip to content

Parser string interning #1763

Closed
Closed

Description

@Razican

As part of the interning of all strings, we currently only intern strings in the lexer (#1758). This is not good enough of course, since creating AST nodes we convert those string symbols back to pointers to re-allocated strings, which means we need an extra allocation and more memory is being used.

The next step is to use Sym also in AST nodes. This means changing any Box<str> for a Sym (most of the times), but it also means that the Display trait can no longer be implemented, since we need an Interner reference to resolve those symbols.

One option here is to create a ToInternedString trait, that would have one function:

pub trait ToInternedString {
    fn to_interned_string(&self, interner: &Interner) -> String;
}

This could replace all to_string() calls, and any format!() calls could be replaced by calling this function and then concatenating (or formatting) the resulting strings. For any Node which also receives an indentation when displaying it, we could have a to_indented_string() function:

fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
    todo!()
}

Then, for those nodes, the to_interned_string() function would just call to_indented_string():

implement ToInternedString for T {
    fn to_interned_string(&self, interner: &Interner) -> String {
        self.to_indented_string(interner, 0)
    }
}

This is similar to how the current Display implementation works.

Maybe we want to call these functions differently. One option is to just call them to_string() or to_indented_string(), but this could conflict with the ToString trait from the standard library. In any case, this might not be an issue since we will not be implementing the Display trait for AST nodes, but we should probably leave room for it for the future if needed.

Since this requires implementing this for all AST nodes, it can be divided in smaller steps. Note that the most complex step is to implement it for one single Node, since it requires already changing how we print all Node types. We could already start by creating the traits and adding these functions for the base Node type, which would just call format!("{}", self) for now. Then, a good candidate for a first node could be the Break or Continue nodes, for example, which also require changes in the execution.

Note that the interner can be retrieved from the Context using context.interner() or context.interner_mu() to get a mutable reference.

Activity

  1. added
    A-Enhancement
    A-Performance
    A-Memory
    E-Hard
    C-AST
    A-Internal
    on Dec 26, 2021
  2. added a commit that references this issue on Jan 23, 2022
  3. added this to the v0.14.0 milestone on Mar 14, 2022
  4. Razican commented on Mar 14, 2022

    @Razican
    MemberAuthor

    This was implemented in 0.14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-EnhancementA-InternalA-MemoryA-PerformanceC-ASTE-Hard

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions