Skip to content

[Merged by Bors] - Lexer string interning - #1758

Closed
Razican wants to merge 4 commits into
mainfrom
feature/interner
Closed

Razican wants to merge 4 commits into
mainfrom
feature/interner

Conversation

@Razican

@Razican Razican commented Dec 22, 2021 •

Copy link
Copy Markdown
Member

This Pull Request is part of #279.

It adds a string interner to Boa, which allows many types to not contain heap-allocated strings, and just contain a NonZeroUsize instead. This can move types to the stack (hopefully I'll be able to move Token, for example, maybe some Node types too.

Note that the internet is for now only available in the lexer. Next steps (in this PR or future ones) would include also using interning in the parser, and finally in execution. The idea is that strings should be represented with a Sym until they are displayed.

Talking about display. I have changed the ParseError type in order to not contain anything that could contain a Sym (basically tokens), which might be a bit faster, but what is important is that we don't depend on the interner when displaying errors.

The issue I have now is in order to display tokens. This requires the interner if we want to know identifiers, for example. The issue here is that Rust doesn't allow using a fmt::Formatter (only in nightly), which is making my head hurt. Maybe someone of you can find a better way of doing this.

Then, about cursor.expect(), this is the only place where we don't have the expected token type as a static string, so it's failing to compile. We have the option of changing the type definition of ParseError to contain an owned string, but maybe we can avoid this by having a &'static str come from a TokenKind with the default values, such as "identifier" for an identifier. I wanted for you to think about it and maybe we can just add that and avoid allocations there.

Oh, and this depends on the VM-only branch, so that has to be merged before :)

Another thing to check: should the interner be in its own module?

@Razican Razican added A-Enhancement New feature or request Help Wanted Extra attention is needed A-Performance Performance related changes and issues C-Parser Issues surrounding the parser lexer A-Memory PRs and Issues related to the memory management or memory footprint. A-Execution Issues or PRs related to code execution C-AST Issue surrounding the abstract syntax tree A-Internal Changes that don't modify execution behaviour labels Dec 22, 2021
@codecov

codecov Bot commented Dec 24, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #1758 (0d38f91) into main (76a27ce) will decrease coverage by 1.29%.
The diff coverage is 59.01%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1758      +/-   ##
==========================================
- Coverage   57.02%   55.72%   -1.30%     
==========================================
  Files         199      201       +2     
  Lines       16842    17336     +494     
==========================================
+ Hits         9604     9661      +57     
- Misses       7238     7675     +437     
Impacted Files Coverage Δ
boa/src/builtins/function/mod.rs 33.57% <0.00%> (-2.65%) ⬇️
boa/src/builtins/typed_array/mod.rs 3.79% <0.00%> (-0.01%) ⬇️
boa/src/object/mod.rs 28.76% <ø> (-0.53%) ⬇️
boa_cli/src/main.rs 6.25% <0.00%> (+0.36%) ⬆️
boa_tester/src/exec/js262.rs 0.00% <ø> (ø)
boa_unicode/src/lib.rs 69.69% <ø> (ø)
boa_wasm/src/lib.rs 0.00% <0.00%> (ø)
boa/src/syntax/parser/statement/mod.rs 39.87% <33.03%> (-2.01%) ⬇️
boa/src/syntax/parser/function/mod.rs 45.28% <40.00%> (-4.37%) ⬇️
...arser/expression/primary/object_initializer/mod.rs 44.48% <41.75%> (-4.97%) ⬇️
... and 116 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 76a27ce...0d38f91. Read the comment docs.

@Razican
Razican changed the base branch from main to justVM December 24, 2021 11:49
@bors
bors Bot changed the base branch from justVM to main December 25, 2021 17:56
@github-actions

github-actions Bot commented Dec 25, 2021 •

Copy link
Copy Markdown

Test262 conformance changes

VM implementation

Test result main count PR count difference
Total 87,200 87,200 0
Passed 40,828 40,828 0
Ignored 19,493 19,493 0
Failed 26,879 26,879 0
Panics 0 0 0
Conformance 46.82% 46.82% 0.00%

@Razican

Razican commented Dec 25, 2021

Copy link
Copy Markdown
Member Author

I will try to solve #503 with this.

@Razican Razican changed the title String interning Lexer string interning Dec 26, 2021
@Razican

Razican commented Dec 26, 2021

Copy link
Copy Markdown
Member Author

I tried to start implementing this for the parser, but the changes are huge. I created a project and will create new issues to implement the interner for the parser, the compiler and the executor, but I think this is ready for review and merge.

I did some benchmarks to see which backend was faster, and for now, I selected the fastest one in my machine. This might change once we implement the interner in more places of the engine.

@Razican Razican removed the A-Execution Issues or PRs related to code execution label Dec 26, 2021
@Razican Razican added this to the v0.14.0 milestone Dec 26, 2021
@Razican Razican removed the Help Wanted Extra attention is needed label Dec 26, 2021
@Razican

Razican commented Dec 27, 2021 •

Copy link
Copy Markdown
Member Author

Benchmarks

┌─────────┬──────────────────────────────────────────────┬───────────────────┬───────────────────────┬────────────┐
│ (index) │                     name                     │  changesDuration  │    masterDuration     │ difference │
├─────────┼──────────────────────────────────────────────┼───────────────────┼───────────────────────┼────────────┤
│    0    │      'Arithmetic operations (Compiler)'      │ '1199.0±58.10ns'  │ '**1107.7±56.24ns**'  │   '+8.0'   │
│    1    │     'Arithmetic operations (Execution)'      │   '3.0±0.25µs'    │   '**2.6±0.13µs**'    │   '+16'    │
│    2    │       'Arithmetic operations (Parser)'       │   '7.5±0.30µs'    │   '**6.7±0.38µs**'    │   '+12'    │
│    3    │          'Array access (Compiler)'           │ '1803.4±64.84ns'  │ '**1730.0±209.74ns**' │   '+4.0'   │
│    4    │          'Array access (Execution)'          │   '14.7±0.72µs'   │   '**14.5±1.31µs**'   │   '+2.0'   │
│    5    │           'Array access (Parser)'            │   '16.0±1.76µs'   │   '**14.2±1.30µs**'   │   '+13'    │
│    6    │         'Array creation (Compiler)'          │   '2.9±0.11µs'    │   '**2.7±0.16µs**'    │   '+8.0'   │
│    7    │         'Array creation (Execution)'         │   '3.8±0.23ms'    │   '**3.6±0.17ms**'    │   '+4.0'   │
│    8    │          'Array creation (Parser)'           │   '17.4±0.99µs'   │   '**16.3±0.93µs**'   │   '+7.0'   │
│    9    │            'Array pop (Compiler)'            │   '8.7±0.67µs'    │   '**8.2±0.43µs**'    │   '+6.0'   │
│   10    │           'Array pop (Execution)'            │ '1660.7±138.91µs' │ '**1537.0±80.57µs**'  │   '+8.0'   │
│   11    │             'Array pop (Parser)'             │  '194.8±11.57µs'  │  '**172.0±10.31µs**'  │   '+13'    │
│   12    │      'Boolean Object Access (Compiler)'      │   '2.5±0.16µs'    │   '**2.3±0.11µs**'    │   '+6.0'   │
│   13    │     'Boolean Object Access (Execution)'      │   '9.2±0.52µs'    │   '**8.3±0.34µs**'    │   '+12'    │
│   14    │       'Boolean Object Access (Parser)'       │   '18.4±1.35µs'   │   '**16.9±1.08µs**'   │   '+9.0'   │
│   15    │            'Clean js (Compiler)'             │   '6.7±0.34µs'    │   '**6.5±0.46µs**'    │   '+3.0'   │
│   16    │            'Clean js (Execution)'            │ '1738.5±74.08µs'  │ '**1687.6±73.17µs**'  │   '+3.0'   │
│   17    │             'Clean js (Parser)'              │   '38.0±2.51µs'   │   '**35.0±1.64µs**'   │   '+9.0'   │
│   18    │                'Create Realm'                │  '436.1±19.86ns'  │  '**407.6±45.13ns**'  │   '+7.0'   │
│   19    │ 'Dynamic Object Property Access (Compiler)'  │   '2.5±0.19µs'    │   '**2.2±0.11µs**'    │   '+12'    │
│   20    │ 'Dynamic Object Property Access (Execution)' │   '10.1±0.62µs'   │   '**9.2±0.49µs**'    │   '+9.0'   │
│   21    │  'Dynamic Object Property Access (Parser)'   │   '14.4±0.82µs'   │   '**12.5±0.69µs**'   │   '+15'    │
│   22    │            'Fibonacci (Compiler)'            │   '3.7±0.17µs'    │   '**3.3±0.21µs**'    │   '+11'    │
│   23    │           'Fibonacci (Execution)'            │   '3.9±0.25ms'    │   '**3.8±0.14ms**'    │   '+4.0'   │
│   24    │             'Fibonacci (Parser)'             │   '22.5±1.12µs'   │   '**18.8±0.95µs**'   │   '+20'    │
│   25    │            'For loop (Compiler)'             │   '3.0±0.19µs'    │   '**2.8±0.17µs**'    │   '+8.0'   │
│   26    │            'For loop (Execution)'            │   '62.2±3.76µs'   │   '**56.4±3.92µs**'   │   '+10'    │
│   27    │             'For loop (Parser)'              │   '19.8±1.33µs'   │   '**16.7±0.98µs**'   │   '+18'    │
│   28    │             'Mini js (Compiler)'             │   '6.7±0.54µs'    │   '**6.2±0.44µs**'    │   '+9.0'   │
│   29    │            'Mini js (Execution)'             │ '1601.3±62.24µs'  │ '**1569.4±68.19µs**'  │   '+2.0'   │
│   30    │              'Mini js (Parser)'              │   '33.9±1.96µs'   │   '**30.5±1.99µs**'   │   '+11'    │
│   31    │      'Number Object Access (Compiler)'       │   '2.1±0.10µs'    │ '**1914.9±83.96ns**'  │   '+9.0'   │
│   32    │      'Number Object Access (Execution)'      │   '7.3±0.47µs'    │   '**6.8±0.63µs**'    │   '+7.0'   │
│   33    │       'Number Object Access (Parser)'        │   '14.7±0.53µs'   │   '**13.1±1.23µs**'   │   '+13'    │
│   34    │         'Object Creation (Compiler)'         │ '1890.2±107.99ns' │ '**1796.3±114.47ns**' │   '+5.0'   │
│   35    │        'Object Creation (Execution)'         │   '9.3±0.49µs'    │   '**8.3±0.47µs**'    │   '+13'    │
│   36    │          'Object Creation (Parser)'          │   '12.8±0.58µs'   │   '**10.7±0.60µs**'   │   '+19'    │
│   37    │             'RegExp (Compiler)'              │   '2.2±0.11µs'    │   '**2.2±0.14µs**'    │   '+3.0'   │
│   38    │             'RegExp (Execution)'             │   '17.2±1.02µs'   │   '**15.8±0.60µs**'   │   '+9.0'   │
│   39    │              'RegExp (Parser)'               │   '13.9±0.71µs'   │   '**12.1±0.64µs**'   │   '+15'    │
│   40    │         'RegExp Creation (Compiler)'         │ '1852.9±105.97ns' │ '**1839.4±131.38ns**' │   '+1.0'   │
│   41    │        'RegExp Creation (Execution)'         │   '13.0±0.97µs'   │   '**12.3±0.71µs**'   │   '+5.0'   │
│   42    │          'RegExp Creation (Parser)'          │   '10.9±0.55µs'   │   '**10.1±0.55µs**'   │   '+8.0'   │
│   43    │         'RegExp Literal (Compiler)'          │   '2.2±0.08µs'    │   '**2.1±0.12µs**'    │   '+2.0'   │
│   44    │         'RegExp Literal (Execution)'         │   '17.4±0.98µs'   │   '**16.1±1.05µs**'   │   '+8.0'   │
│   45    │          'RegExp Literal (Parser)'           │   '10.5±0.48µs'   │   '**9.5±0.42µs**'    │   '+11'    │
│   46    │     'RegExp Literal Creation (Compiler)'     │ '1814.1±82.13ns'  │ '**1758.7±74.86ns**'  │   '+3.0'   │
│   47    │    'RegExp Literal Creation (Execution)'     │   '12.8±0.67µs'   │   '**12.4±0.83µs**'   │   '+4.0'   │
│   48    │      'RegExp Literal Creation (Parser)'      │   '8.3±0.63µs'    │   '**7.4±0.38µs**'    │   '+11'    │
│   49    │  'Static Object Property Access (Compiler)'  │   '2.0±0.13µs'    │ '**1928.4±292.73ns**' │   '+6.0'   │
│   50    │ 'Static Object Property Access (Execution)'  │   '9.2±0.63µs'    │   '**8.5±0.50µs**'    │   '+8.0'   │
│   51    │   'Static Object Property Access (Parser)'   │   '13.4±1.41µs'   │   '**11.7±0.91µs**'   │   '+15'    │
│   52    │      'String Object Access (Compiler)'       │   '3.0±0.17µs'    │   '**2.9±0.16µs**'    │   '+5.0'   │
│   53    │      'String Object Access (Execution)'      │   '11.7±0.62µs'   │   '**10.7±0.63µs**'   │   '+9.0'   │
│   54    │       'String Object Access (Parser)'        │   '18.2±1.02µs'   │   '**16.1±0.65µs**'   │   '+13'    │
│   55    │        'String comparison (Compiler)'        │   '3.1±0.21µs'    │   '**2.9±0.11µs**'    │   '+5.0'   │
│   56    │       'String comparison (Execution)'        │   '10.5±2.90µs'   │   '**9.9±0.66µs**'    │   '+6.0'   │
│   57    │         'String comparison (Parser)'         │   '14.8±0.88µs'   │   '**13.1±0.93µs**'   │   '+13'    │
│   58    │      'String concatenation (Compiler)'       │   '2.2±0.15µs'    │   '**2.1±0.10µs**'    │   '+7.0'   │
│   59    │      'String concatenation (Execution)'      │   '8.7±0.68µs'    │   '**8.1±0.49µs**'    │   '+7.0'   │
│   60    │       'String concatenation (Parser)'        │   '9.8±0.75µs'    │   '**8.9±0.41µs**'    │   '+10'    │
│   61    │           'String copy (Compiler)'           │ '1570.7±166.09ns' │ '**1492.5±90.95ns**'  │   '+5.0'   │
│   62    │          'String copy (Execution)'           │   '7.8±0.53µs'    │   '**7.2±0.31µs**'    │   '+7.0'   │
│   63    │            'String copy (Parser)'            │   '7.3±0.44µs'    │   '**6.6±0.43µs**'    │   '+11'    │
│   64    │             'Symbols (Compiler)'             │ '1003.7±62.29ns'  │  '**935.5±59.47ns**'  │   '+7.0'   │
│   65    │            'Symbols (Execution)'             │   '7.3±0.55µs'    │   '**6.9±0.40µs**'    │   '+7.0'   │
│   66    │              'Symbols (Parser)'              │   '5.9±0.21µs'    │   '**4.9±0.25µs**'    │   '+19'    │
└─────────┴──────────────────────────────────────────────┴───────────────────┴───────────────────────┴────────────┘

@RageKnify RageKnify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the interner in a global hidden by an API might make it easier to develop boa (since we wouldn't need to pass its reference around everywhere), but I don't feel too strongly either way.

@raskad

raskad commented Dec 28, 2021

Copy link
Copy Markdown
Member

Having the interner in a global hidden by an API might make it easier to develop boa (since we wouldn't need to pass its reference around everywhere), but I don't feel too strongly either way.

@Razican correct me if I'm wrong, but I think that would not work, because the interner must be specific to one parsed block of code, so it can be deallocated when the code block is being dropped right?

@Razican

Razican commented Dec 28, 2021

Copy link
Copy Markdown
Member Author

Having the interner in a global hidden by an API might make it easier to develop boa (since we wouldn't need to pass its reference around everywhere), but I don't feel too strongly either way.

@Razican correct me if I'm wrong, but I think that would not work, because the interner must be specific to one parsed block of code, so it can be deallocated when the code block is being dropped right?

If you don't de-allocate, you could use the same interner multiple times, in theory, but if the application is running for long, you might have a huge memory usage.

@RageKnify

RageKnify commented Dec 28, 2021 •

Copy link
Copy Markdown
Contributor

Yeah, I guess depending on the use case you would want to drop the Interner to avoid a never shrinking heap structure.
If boa were embedded in something like node then I don't think this would matter, right? Since once everything is parsed we shouldn't intern that many more strings.
Even in the browser, the only problem is a browser tab which keeps evaluating JS with new identifiers, right?

As I said, I can accept this choice, just not sure it makes much of a difference, if the Interner is part of the Context then the cli and future browsers (in the context of a given browser tab) would only have 1 at a time any ways.

Maybe for game engines or other embedding use cases it would be relevant to not use just 1 (Interner) since it would make sense to create new Contexts and drop old ones when the new ones are created. In that case I can see the possibility of the heap growing because the user keeps changing the name of their variables/functions with "updates" to their script.

(I feel this last point is good enough reason to keep it in the Context)

@Razican

Razican commented Dec 28, 2021

Copy link
Copy Markdown
Member Author

I'm also thinking on servers using it as their scripting language of choice. If they can run multiple scripts, in different days or so, I think this approach could be better.

@jasonwilliams jasonwilliams left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I agree with Rageknify's concern around this being passed everywhere, but I don't see any other option from what has been discussed

Comment thread boa_interner/src/lib.rs
/// The string interner for Boa.
///
/// This is a type alias that makes it easier to reference it in the code.
pub type Interner = StringInterner<BucketBackend<Sym>>;

@jasonwilliams jasonwilliams Jan 21, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come you chose bucketbackend over stringbackend? Im guessing because the use of static

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Statics seem to be the reason. It probably makes sense that we try out the stringbackend when we use the interner in the parser.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason was that I tried locally all backends and this was the one giving better results, but I have no personal preference xD in the future once we use the interner everywhere we can benchmark again.

@raskad

raskad commented Jan 22, 2022

Copy link
Copy Markdown
Member

bors r+

bors Bot pushed a commit that referenced this pull request Jan 22, 2022
This Pull Request is part of #279.

 It adds a string interner to Boa, which allows many types to not contain heap-allocated strings, and just contain a `NonZeroUsize` instead. This can move types to the stack (hopefully I'll be able to move `Token`, for example, maybe some `Node` types too.

Note that the internet is for now only available in the lexer. Next steps (in this PR or future ones) would include also using interning in the parser, and finally in execution. The idea is that strings should be represented with a `Sym` until they are displayed.

Talking about display. I have changed the `ParseError` type in order to not contain anything that could contain a `Sym` (basically tokens), which might be a bit faster, but what is important is that we don't depend on the interner when displaying errors.

The issue I have now is in order to display tokens. This requires the interner if we want to know identifiers, for example. The issue here is that Rust doesn't allow using a `fmt::Formatter` (only in nightly), which is making my head hurt. Maybe someone of you can find a better way of doing this.

Then, about `cursor.expect()`, this is the only place where we don't have the expected token type as a static string, so it's failing to compile. We have the option of changing the type definition of `ParseError` to contain an owned string, but maybe we can avoid this by having a `&'static str` come from a `TokenKind` with the default values, such as "identifier" for an identifier. I wanted for you to think about it and maybe we can just add that and avoid allocations there.

Oh, and this depends on the VM-only branch, so that has to be merged before :)

Another thing to check: should the interner be in its own module?
@bors

bors Bot commented Jan 22, 2022

Copy link
Copy Markdown

Build failed:

@Razican

Razican commented Jan 22, 2022

Copy link
Copy Markdown
Member Author

Build failed:

I will rebase this, it seems to fail due to Rust 1.58 lints.

@Razican

Razican commented Jan 22, 2022

Copy link
Copy Markdown
Member Author

LGTM

I agree with Rageknify's concern around this being passed everywhere, but I don't see any other option from what has been discussed

One option here would be to have the interner in the parser, as a reference. Might make sense actually, and I might do so in the other PR, if you're OK with it.

@Razican

Razican commented Jan 22, 2022

Copy link
Copy Markdown
Member Author

I had to revert the upgrade to Wasm-bindgen 0.2.79 due to wasm-bindgen/wasm-bindgen#2774.

Comment thread boa_wasm/Cargo.toml Resolved
@RageKnify

Copy link
Copy Markdown
Contributor

bors r+

bors Bot pushed a commit that referenced this pull request Jan 22, 2022
This Pull Request is part of #279.

 It adds a string interner to Boa, which allows many types to not contain heap-allocated strings, and just contain a `NonZeroUsize` instead. This can move types to the stack (hopefully I'll be able to move `Token`, for example, maybe some `Node` types too.

Note that the internet is for now only available in the lexer. Next steps (in this PR or future ones) would include also using interning in the parser, and finally in execution. The idea is that strings should be represented with a `Sym` until they are displayed.

Talking about display. I have changed the `ParseError` type in order to not contain anything that could contain a `Sym` (basically tokens), which might be a bit faster, but what is important is that we don't depend on the interner when displaying errors.

The issue I have now is in order to display tokens. This requires the interner if we want to know identifiers, for example. The issue here is that Rust doesn't allow using a `fmt::Formatter` (only in nightly), which is making my head hurt. Maybe someone of you can find a better way of doing this.

Then, about `cursor.expect()`, this is the only place where we don't have the expected token type as a static string, so it's failing to compile. We have the option of changing the type definition of `ParseError` to contain an owned string, but maybe we can avoid this by having a `&'static str` come from a `TokenKind` with the default values, such as "identifier" for an identifier. I wanted for you to think about it and maybe we can just add that and avoid allocations there.

Oh, and this depends on the VM-only branch, so that has to be merged before :)

Another thing to check: should the interner be in its own module?
@bors

bors Bot commented Jan 22, 2022

Copy link
Copy Markdown

Pull request successfully merged into main.

Build succeeded:

@bors bors Bot changed the title Lexer string interning [Merged by Bors] - Lexer string interning Jan 22, 2022
@bors bors Bot closed this Jan 22, 2022
@bors
bors Bot deleted the feature/interner branch January 22, 2022 18:19
@Razican

Razican commented Jan 22, 2022

Copy link
Copy Markdown
Member Author

LGTM
I agree with Rageknify's concern around this being passed everywhere, but I don't see any other option from what has been discussed

One option here would be to have the interner in the parser, as a reference. Might make sense actually, and I might do so in the other PR, if you're OK with it.

Actually, now that I check it, the only way to do this would be to change all the methods to receive a &mut Parser, which would include a &mut Interner, but it might not be easy. I guess this should be done in a different PR.

bors Bot pushed a commit that referenced this pull request Jan 23, 2022
This builds on top of #1758 to try to bring #1763 to life.

Something that should probably be done here would be to convert `JsString` to a `Sym` internally. Then, further optimizations could be done adding common strings to a custom interner type (those that we know statically).

This is definitely work in progress, but I would like to have feedback on the API, and feel free to contribute.

Co-authored-by: raskad <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Enhancement New feature or request A-Internal Changes that don't modify execution behaviour A-Memory PRs and Issues related to the memory management or memory footprint. A-Performance Performance related changes and issues C-AST Issue surrounding the abstract syntax tree C-Parser Issues surrounding the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants