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:
Alex Crichton
2018-06-01 16:46:42 -05:00
committed by GitHub
parent 346d2fda22
commit cb1e5cf136
3 changed files with 41 additions and 48 deletions

View File

@ -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;
"));
}