Skip to content

Box ImportPattern in Expr - #12568

Merged
sholderbach merged 1 commit into
nushell:mainfrom
IanManske:box-import-pattern-expr
Apr 18, 2024
Merged

sholderbach merged 1 commit into
nushell:mainfrom
IanManske:box-import-pattern-expr

Conversation

@IanManske

@IanManske IanManske commented Apr 18, 2024 •

Copy link
Copy Markdown
Member

Description

Adds a Box around the ImportPattern in Expr which decreases the size of Expr from 152 to 64 bytes (and Expression from 216 to 128 bytes). This seems to speed up parsing a little bit according to the benchmarks (main is top, PR is bottom):

benchmarks                       fastest       │ slowest       │ median        │ mean          │ samples │ iters
benchmarks                       fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ parser_benchmarks                           │               │               │               │         │
├─ parser_benchmarks                           │               │               │               │         │
│  ├─ parse_default_config_file  2.287 ms      │ 4.532 ms      │ 2.311 ms      │ 2.437 ms      │ 100     │ 100
│  ├─ parse_default_config_file  2.255 ms      │ 2.781 ms      │ 2.281 ms      │ 2.312 ms      │ 100     │ 100
│  ╰─ parse_default_env_file     421.8 µs      │ 824.6 µs      │ 494.3 µs      │ 527.5 µs      │ 100     │ 100
│  ╰─ parse_default_env_file     402 µs        │ 486.6 µs      │ 414.8 µs      │ 416.2 µs      │ 100     │ 100

@sholderbach

Copy link
Copy Markdown
Member

That seems to be really good for the worst case performance! Nice find @IanManske

@sholderbach
sholderbach merged commit 9a26584 into nushell:main Apr 18, 2024
@hustcer hustcer added this to the v0.93.0 milestone Apr 19, 2024
IanManske added a commit that referenced this pull request Apr 24, 2024
# Description
Continuing from #12568, this PR further reduces the size of `Expr` from
64 to 40 bytes. It also reduces `Expression` from 128 to 96 bytes and
`Type` from 32 to 24 bytes.

This was accomplished by:
- for `Expr` with multiple fields (e.g., `Expr::Thing(A, B, C)`),
merging the fields into new AST struct types and then boxing this struct
(e.g. `Expr::Thing(Box<ABC>)`).
- replacing `Vec<T>` with `Box<[T]>` in multiple places. `Expr`s and
`Expression`s should rarely be mutated, if at all, so this optimization
makes sense.

By reducing the size of these types, I didn't notice a large performance
improvement (at least compared to #12568). But this PR does reduce the
memory usage of nushell. My config is somewhat light so I only noticed a
difference of 1.4MiB (38.9MiB vs 37.5MiB).

---------

Co-authored-by: Stefan Holderbach <[email protected]>
@IanManske
IanManske deleted the box-import-pattern-expr branch May 27, 2024 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants