Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NaN boxed JavaScript Value #1373

Open
HalidOdat opened this issue Jun 29, 2021 · 11 comments · May be fixed by #1830
Open

NaN boxed JavaScript Value #1373

HalidOdat opened this issue Jun 29, 2021 · 11 comments · May be fixed by #1830
Assignees
Labels
execution Issues or PRs related to code execution performance Performance related changes and issues

Comments

@HalidOdat
Copy link
Member

Currently a JavaScript value in Boa is defined as a fat enum. With NaN boxing we can reduce this to 8 bytes having all the variants of a JS value fit in u64/f64. With this we can reduce the memory footprint of Boa, also a JS value will be able to be put into a 64bit register.

Many JS engines use this, like spidermonkey, JavaScriptCore, etc.

For more information:
- https://anniecherkaev.com/the-secret-life-of-nan
- https://leonardschuetz.ch/blog/nan-boxing/
- https://github.com/mozilla/gecko-dev/blob/master/js/public/Value.h

@HalidOdat HalidOdat added performance Performance related changes and issues execution Issues or PRs related to code execution labels Jun 29, 2021
@HalidOdat HalidOdat self-assigned this Jun 29, 2021
@neeldug
Copy link
Contributor

neeldug commented Jun 29, 2021

I ended up reading through the links and can see the benefit in doing NaN boxing, however, what is the memory footprint of the fat enum being used currently? Not totally sure how or whether Rust does any sort of padding or whatnot on fat enums.

@HalidOdat
Copy link
Member Author

I ended up reading through the links and can see the benefit in doing NaN boxing, however, what is the memory footprint of the fat enum being used currently? Not totally sure how or whether Rust does any sort of padding or whatnot on fat enums.

Currently the size of Value is 24 bytes (size_of::<Value>()).

@playXE
Copy link

playXE commented Jul 7, 2021

If you're looking for impl of NaN boxing in Rust Starlight has two different implementations of them for handling JS values: https://github.com/Starlight-JS/starlight/blob/dev/crates/starlight/src/vm/value.rs :)

@HalidOdat
Copy link
Member Author

If you're looking for impl of NaN boxing in Rust Starlight has two different implementations of them for handling JS values: https://github.com/Starlight-JS/starlight/blob/dev/crates/starlight/src/vm/value.rs :)

Awesome! thanks, for the suggestion. I have implemented NaN boxing locally, but the problem is refactoring the existing match value stuff (which we use a lot, this generates a couple of hundred of erros 😅 )

@playXE
Copy link

playXE commented Jul 7, 2021

What about having ValueVariant enum which essentially can hold all the values NaN-boxed value can encode and then add JsValue::variant() which returns this ValueVariant. I'm pretty sure Rust and LLVM are able to optimize this.

@HalidOdat
Copy link
Member Author

What about having ValueVariant enum which essentially can hold all the values NaN-boxed value can encode and then add JsValue::variant() which returns this ValueVariant. I'm pretty sure Rust and LLVM are able to optimize this.

Yup. thought about this as a possible solution and it's probably the best solution, since refactoring will be a nightmare otherwise.

@HalidOdat HalidOdat removed the blocked Waiting for another code change label Jul 17, 2021
@Razican
Copy link
Member

Razican commented Jan 31, 2022

@HalidOdat any news on this?

@HalidOdat
Copy link
Member Author

@HalidOdat any news on this?

Sorry for not replying I have been very busy, but plan to start to do more contributions in the future.

I'll start working on this again. had a semi-working implementation of this that needs a rebase (it's pretty old). Hopefully I can get it working by this weekend :)

@jedel1043
Copy link
Member

Will try to do some investigation on how to reduce the size of JsObject.

@jedel1043 jedel1043 reopened this Apr 14, 2024
@jasonwilliams
Copy link
Member

Would be nice to revisit this as part of our performance focus, I’m happy to team up with @jedel1043 if at some point you wanted to take another look at this

@hansl hansl assigned hansl and unassigned HalidOdat and jedel1043 Dec 19, 2024
@hansl
Copy link
Contributor

hansl commented Dec 19, 2024

PR #4080 is the first step towards this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
execution Issues or PRs related to code execution performance Performance related changes and issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants