boa
boa copied to clipboard
NaN boxed JavaScript Value
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
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.
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>()
).
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 :)
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 :sweat_smile: )
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.
What about having
ValueVariant
enum which essentially can hold all the values NaN-boxed value can encode and then addJsValue::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 any news on this?
@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 :)
Will try to do some investigation on how to reduce the size of JsObject.