Skip to content

Faster with preallocated output array and remapped values in nodes#6

Open
nicolaitanes wants to merge 2 commits intoekg:masterfrom
nicolaitanes:faster-with-less-garbage-collection
Open

Faster with preallocated output array and remapped values in nodes#6
nicolaitanes wants to merge 2 commits intoekg:masterfrom
nicolaitanes:faster-with-less-garbage-collection

Conversation

@nicolaitanes
Copy link
Copy Markdown

@nicolaitanes nicolaitanes commented Jun 30, 2023

Yielding the same array each time saves a lot of time on garbage collection.

Starting from an array of remap elements is reasonably fast compared to reallocating it in each call to visit, which adds about 69% to the run time when the callback/body is trivial.

Starting from an empty array adds about 17% to the runtime (compared to remap elements), probably because the JIT has to emit more array operations.

Prefilling with a constant, though incorrect, is even faster than remap; maybe the runtime can pick a simpler array subclass? e.g.

  • p.map(() => 'why???') as unknown as T[] runtime is about 95% compared to remap
  • p.slice() as unknown as T[] runtime is about 93% compared to remap -- the fastest

Updated 2024-09: Faster by precomputing the remapped value in each node

Nodes' positional value v changes infrequently, if at all.
By including r: remap[node.v] in each node, we can save about 25% of runtime.

nicolaitanes and others added 2 commits June 30, 2023 14:19
…ection.

Starting from an array of `remap` elements is reasonably fast compared to reallocating
it in each call to `visit`, which adds about 69% to the run time when the callback/body is trivial.

Starting from an empty array adds about 17% to the runtime (compared to `remap` elements),
probably because the JIT has to emit more array operations.

Prefilling with a constant, though incorrect, is even faster than remap;
maybe the runtime can pick a simpler array subclass? e.g.
- p.map(() => 'why???') as unknown as T[] : runtime is about 95% compared to remap
- p.slice() as unknown as T[]             : runtime is about 93% compared to remap -- fastest
Nodes' positional value `v` changes infrequently, if at all.
By including `r: remap[node.v]` in each node, we can save about 25% of runtime.
@nicolaitanes nicolaitanes changed the title Faster with preallocated output array Faster with preallocated output array and remapped values in nodes Sep 29, 2024
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.

1 participant