Skip to content

(WIP) Start moving from Spans to SpanIDs - #12239

Closed
kubouch wants to merge 23 commits into
nushell:mainfrom
kubouch:span-id
Closed

kubouch wants to merge 23 commits into
nushell:mainfrom
kubouch:span-id

Conversation

@kubouch

@kubouch kubouch commented Mar 19, 2024 •

Copy link
Copy Markdown
Contributor

Description

Starting a big refactor that will change Expressions and Values (and possibly other related types) to use Span IDs pointing at an array of spans instead of storing the spans manually.

This should save 8 bytes on every Expression/Value etc. Given how much overhead in Nushell is caused by memory (de)allocation, there is a high chance of positive performance impact.

The main motivation, though, is preparing for the new (WIP!) parser, which uses the array-based approach throughout. After this refactor, it should be much easier to adapt the evaluator to the output of the new parser.

The plan is to finish up this PR before the next release and merge in the calm period after the release to avoid excessive merge conflicts piling up during normal operation.

TODO:

  • Re-enable format pattern
  • Rename .get_span() to .span()
  • Remove span from Call
  • Resolve TODO span_id
  • Resolve TODO SPAN
  • Check if detect columns still works

User-Facing Changes

Hopefully none, except possibly performance improvements.

Temporarily disabled format pattern because it was trying to do eval

Tests + Formatting

After Submitting

@kubouch kubouch added the api-change This PR should be mentioned in #api-updates channel on Discord label Mar 19, 2024
@devyn

devyn commented Mar 19, 2024 •

Copy link
Copy Markdown
Contributor

Hey, this is cool. I have a few questions:

  • Is this able to bring the size of Value down as a whole? I'm thinking that there are some variants that are a bit larger and it's unfortunate that we have to pay that price even if we just have an array of integers. On the other hand, we have to consider cache. I would think probably the most reasonable target would be 32 bytes on 64-bit as that's enough to hold a span ID and a String or Vec. Value is currently 72 bytes, and the biggest culprit right now is Record.

  • Does this make it impossible to derive spans from spans? I'm thinking of things like .past() or if we want to point to something inside a string. Maybe this isn't a big deal, but even if there's a solution for the engine it seems like it would be tough for plugins to handle.

  • How does this work for errors? We don't really have a handle to where the spans are being stored for the Diagnostic stuff so do those have to be converted to plain Spans before they can be used in an error? And if that's the case, again I'm slightly worried about plugins in an error case having to call the engine to look up a span, and what that might do to being able to test plugin code. Though I do feel like I need a solution for EngineInterface for testing purposes already.

  • I don't know if it's already done here but I suggest reserving ID 0 for unknown / test data I can see you did this. 👍

@FilipAndersson245

FilipAndersson245 commented Mar 20, 2024 •

Copy link
Copy Markdown
Contributor

I am looking over nnethercote, Rust performance book and have some thoughts
How difficult would it be if we used u32 instead of usize?, that would save an additional 4 bytes it would allow
4294967295 spans compared to
18446744073709551615?
I don't know many spans we use atm, roughly.

As @devyn say the biggest culprit Record is currently 72 bytes, and it seem to be because the two vecs that it contains,
it stores capacity, length on the stack for a total of 24 bytes, If we would test out to use thinVec instead we could reduce the 48 bytes to only 16. With the con being having to a heap allocation being an indirection.
Otherwise we could reduce some of the larger variants by just boxing them,

Additional thought, should the indexes stored in a Vec be allocated with an arena allocator to reuse allocations and deallocations?

@FilipAndersson245

Copy link
Copy Markdown
Contributor

#12252 I boxed the records and the new largest is Closure with the total being 56 bytes.

@sholderbach

Copy link
Copy Markdown
Member

Thanks for taking on this massive endeavour @kubouch !

My two cents to the comments from @devyn and @FilipAndersson245 I think we should keep the priority on this PR to get the correctness (and existing error reporting features if possible) right and directly compare what saving the 8 bytes (which is the required alignment of Value as well) and simplified operations in the happy path already gains us to offset the book keeping.

On top of perf proving out the IDs may make it easier if we swap out the parser or introduce compression/GC of the source cache.

After that we can compare different options for optimal memory layout.

@yizhepku

Copy link
Copy Markdown
Contributor

The main motivation, though, is preparing for the new (WIP!) parser, which uses the array-based approach throughout.

Do you plan to replace the current parser entirely? I'm curious about the "array-based" approach. Why do we need a new parser? Why does it need to use SpanId instead of Span?

@kubouch

kubouch commented Mar 21, 2024

Copy link
Copy Markdown
Contributor Author

@devyn

Is this able to bring the size of Value down as a whole? I'm thinking that there are some variants that are a bit larger and it's unfortunate that we have to pay that price even if we just have an array of integers. On the other hand, we have to consider cache. I would think probably the most reasonable target would be 32 bytes on 64-bit as that's enough to hold a span ID and a String or Vec. Value is currently 72 bytes, and the biggest culprit right now is Record.

If every Span is replaced by SpanId, then it should shrink the whole Value by 8 bytes, though it depends on alignment and padding as well. I'm kind of half-thinking that after the span replacement, the Values could perhaps be stored in an array-like fashion as in the parser and then you could do all sorts of memory optimizations.

Does this make it impossible to derive spans from spans? I'm thinking of things like .past() or if we want to point to something inside a string. Maybe this isn't a big deal, but even if there's a solution for the engine it seems like it would be tough for plugins to handle.

Not sure what you mean by deriving spans from spans. You mean stuff like span(&[span1, span2, ...])? Plugins shouldn't produce any new spans because they do not parse any code, so this shouldn't be needed.

How does this work for errors? We don't really have a handle to where the spans are being stored for the Diagnostic stuff so do those have to be converted to plain Spans before they can be used in an error? And if that's the case, again I'm slightly worried about plugins in an error case having to call the engine to look up a span, and what that might do to being able to test plugin code. Though I do feel like I need a solution for EngineInterface for testing purposes already.

I'm thinking errors could stay as-is for now. When creating an error you'd need to look up the span with SpanId from the state. We'll see how it goes once I progress with it :-D. The plugin-engine chatter from looking up spans from IDs sounds bad, but plugins shouldn't really need it that much, apart from creating errors, but perhaps that could be also abstracted somehow via the engine interface so that we don't need to go back and forth with the spans.

@FilipAndersson245
4 billion spans is pretty high, it's not unimaginably high. You can easily fit is in today's device memories. 18 quintillion spans is unimaginably high in comparison. usize is really future-proof in this regard. We can consider the u32 as an optimization later, though, it's quite an obvious place to easily save a lot of bytes, but we'd probably need to handle the case of running out of IDs.

@yizhepku
Yes, replace entirely. See https://github.com/sophiajt/new-nu-parser for implementation details. The purpose of a new parser is higher parsing speed and easier maintainability. Hopefully also paying back some tech debt we've accumulated via various hacks.

@WindSoilder

WindSoilder commented Mar 27, 2024 •

Copy link
Copy Markdown
Contributor

It's a pity that we don't have a way to test span values automatically.

Actually I'm not sure for one question: can it saves memory usage? After this pr, spans are saved in EngineState/StateWorkingSet rather than the value it self, it doesn't reduce spans allocating, maybe I'm missing something.
And after we save it in EngineState, these spans will no-longer be freed, will it leak memory?

@FilipAndersson245

FilipAndersson245 commented Mar 27, 2024 •

Copy link
Copy Markdown
Contributor

It's a pity that we don't have a way to test span values automatically.

Actually I'm not sure for one question: can it saves memory usage? After this pr, spans are saved in EngineState/StateWorkingSet rather than the value it self, it doesn't reduce spans allocating, maybe I'm missing something.
And after we save it in EngineState, these spans will no-longer be freed, will it leak memory?

My 2cent would be that it would be fever larger allocations that are more likely to be on the hot path.

About the second point im not sure, seeds like a resonable problem, maybe we would have to add a gc or simular in the mid term?

@devyn

devyn commented Mar 27, 2024

Copy link
Copy Markdown
Contributor

@kubouch

Not sure what you mean by deriving spans from spans. You mean stuff like span(&[span1, span2, ...])? Plugins shouldn't produce any new spans because they do not parse any code, so this shouldn't be needed.

Sorry, I didn't see your reply until now. There are methods on Span like .past(). I'm not sure how much they're used, but they do some things to spans to create new ones.

@kubouch

kubouch commented Mar 28, 2024

Copy link
Copy Markdown
Contributor Author

@devyn I've seen the .past() used only in one place in the code, shouldn't be a big of a deal. In general, generating spans outside the parser shouldn't happen, so in theory, the whole impl Span could go away, maybe except for cases like .contains().

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

Labels

api-change This PR should be mentioned in #api-updates channel on Discord

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants