mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 04:51:23 +00:00
Optimize JsValue::{from_bool, undefined, null} constructors (#220)
This commit optimizes constructing an instance of `JsValue` which is one of `null`, `undefined`, `true`, or `false`. These are commonly created on the Rust side of things and since there's only a limited set of values we can easily prepopulate the global slab with a few entries and use hardcoded indices to refer to these constants. This should avoid the need to travel into JS to insert a `null` or and `undefined` into the global slab.
This commit is contained in:
@ -690,17 +690,23 @@ impl<'a> Context<'a> {
|
||||
if !self.exposed_globals.insert("slab") {
|
||||
return;
|
||||
}
|
||||
self.global(&format!("let slab = [];"));
|
||||
let initial_values = [
|
||||
"{ obj: null }",
|
||||
"{ obj: undefined }",
|
||||
"{ obj: true }",
|
||||
"{ obj: false }",
|
||||
];
|
||||
self.global(&format!("let slab = [{}];", initial_values.join(", ")));
|
||||
if self.config.debug {
|
||||
self.export("assertSlabEmpty", "
|
||||
function() {
|
||||
for (let i = 0; i < slab.length; i++) {
|
||||
self.export("assertSlabEmpty", &format!("
|
||||
function() {{
|
||||
for (let i = {}; i < slab.length; i++) {{
|
||||
if (typeof(slab[i]) === 'number')
|
||||
continue;
|
||||
throw new Error('slab is not currently empty');
|
||||
}
|
||||
}
|
||||
");
|
||||
}}
|
||||
}}
|
||||
", initial_values.len()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -708,8 +714,9 @@ impl<'a> Context<'a> {
|
||||
if !self.exposed_globals.insert("slab_next") {
|
||||
return;
|
||||
}
|
||||
self.expose_global_slab();
|
||||
self.global(&format!("
|
||||
let slab_next = 0;
|
||||
let slab_next = slab.length;
|
||||
"));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user