Skip to content

Commit

Permalink
Some string optimizations (#4030)
Browse files Browse the repository at this point in the history
* chore: replace `js_str` with `js_string`

* chore: replace `boolean`'s `to_string` with literal

* chore: remove unnecessary `to_string`

* chore: fix lint and format

* chore: add feature gate to import `js_str`

* chore: change `get_typed_fn`'s name to `JsString`

* chore: replace js_str with js_string(cli)

* chore: replace js_str with js_string(`try_into_js`)

* chore: replace js_str with js_string(`Console`)

* chore: fix lint

* perf: use more efficient `LazyLock` on `RAW_STATICS_CACHE`

* chore: move `js_str` together with other imports
  • Loading branch information
CrazyboyQCD authored Nov 11, 2024
1 parent 5ea44d8 commit f30514b
Show file tree
Hide file tree
Showing 87 changed files with 679 additions and 666 deletions.
9 changes: 5 additions & 4 deletions cli/src/debug/function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use boa_engine::{
builtins::function::OrdinaryFunction,
js_str, js_string,
js_string,
object::ObjectInitializer,
vm::flowgraph::{Direction, Graph},
Context, JsArgs, JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
Expand Down Expand Up @@ -68,9 +68,10 @@ fn flowgraph(_this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResu
let mut direction = Direction::LeftToRight;
if let Some(arguments) = args.get(1) {
if let Some(arguments) = arguments.as_object() {
format = flowgraph_parse_format_option(&arguments.get(js_str!("format"), context)?)?;
direction =
flowgraph_parse_direction_option(&arguments.get(js_str!("direction"), context)?)?;
format = flowgraph_parse_format_option(&arguments.get(js_string!("format"), context)?)?;
direction = flowgraph_parse_direction_option(
&arguments.get(js_string!("direction"), context)?,
)?;
} else if value.is_string() {
format = flowgraph_parse_format_option(value)?;
} else {
Expand Down
20 changes: 10 additions & 10 deletions cli/src/debug/limits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use boa_engine::{
js_str,
js_string,
object::{FunctionObjectBuilder, ObjectInitializer},
property::Attribute,
Context, JsArgs, JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
Expand Down Expand Up @@ -51,51 +51,51 @@ fn set_recursion(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResu
pub(super) fn create_object(context: &mut Context) -> JsObject {
let get_loop =
FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(get_loop))
.name(js_str!("get loop"))
.name(js_string!("get loop"))
.length(0)
.build();
let set_loop =
FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(set_loop))
.name(js_str!("set loop"))
.name(js_string!("set loop"))
.length(1)
.build();

let get_stack =
FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(get_stack))
.name(js_str!("get stack"))
.name(js_string!("get stack"))
.length(0)
.build();
let set_stack =
FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(set_stack))
.name(js_str!("set stack"))
.name(js_string!("set stack"))
.length(1)
.build();

let get_recursion =
FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(get_recursion))
.name(js_str!("get recursion"))
.name(js_string!("get recursion"))
.length(0)
.build();
let set_recursion =
FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(set_recursion))
.name(js_str!("set recursion"))
.name(js_string!("set recursion"))
.length(1)
.build();
ObjectInitializer::new(context)
.accessor(
js_str!("loop"),
js_string!("loop"),
Some(get_loop),
Some(set_loop),
Attribute::WRITABLE | Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE,
)
.accessor(
js_str!("stack"),
js_string!("stack"),
Some(get_stack),
Some(set_stack),
Attribute::WRITABLE | Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE,
)
.accessor(
js_str!("recursion"),
js_string!("recursion"),
Some(get_recursion),
Some(set_recursion),
Attribute::WRITABLE | Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE,
Expand Down
20 changes: 10 additions & 10 deletions cli/src/debug/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Allow lint so it, doesn't warn about `JsResult<>` unneeded return on functions.
#![allow(clippy::unnecessary_wraps)]

use boa_engine::{js_str, object::ObjectInitializer, property::Attribute, Context, JsObject};
use boa_engine::{js_string, object::ObjectInitializer, property::Attribute, Context, JsObject};

mod function;
mod gc;
Expand All @@ -24,42 +24,42 @@ fn create_boa_object(context: &mut Context) -> JsObject {

ObjectInitializer::new(context)
.property(
js_str!("function"),
js_string!("function"),
function_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
js_str!("object"),
js_string!("object"),
object_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
js_str!("shape"),
js_string!("shape"),
shape_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
js_str!("optimizer"),
js_string!("optimizer"),
optimizer_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
js_str!("gc"),
js_string!("gc"),
gc_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
js_str!("realm"),
js_string!("realm"),
realm_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
js_str!("limits"),
js_string!("limits"),
limits_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
js_str!("string"),
js_string!("string"),
string_module,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
Expand All @@ -71,7 +71,7 @@ pub(crate) fn init_boa_debug_object(context: &mut Context) {
let boa_object = create_boa_object(context);
context
.register_global_property(
js_str!("$boa"),
js_string!("$boa"),
boa_object,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
Expand Down
6 changes: 3 additions & 3 deletions cli/src/debug/optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use boa_engine::{
js_str,
js_string,
object::{FunctionObjectBuilder, ObjectInitializer},
optimizer::OptimizerOptions,
property::Attribute,
Expand Down Expand Up @@ -64,13 +64,13 @@ pub(super) fn create_object(context: &mut Context) -> JsObject {
.build();
ObjectInitializer::new(context)
.accessor(
js_str!("constantFolding"),
js_string!("constantFolding"),
Some(get_constant_folding),
Some(set_constant_folding),
Attribute::WRITABLE | Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE,
)
.accessor(
js_str!("statistics"),
js_string!("statistics"),
Some(get_statistics),
Some(set_statistics),
Attribute::WRITABLE | Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE,
Expand Down
3 changes: 1 addition & 2 deletions core/engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::{
Context, JsData, JsResult,
};
use boa_gc::{Finalize, Trace};
use boa_macros::js_str;
use boa_profiler::Profiler;

/// The Array Iterator object represents an iteration over an array. It implements the iterator protocol.
Expand Down Expand Up @@ -53,7 +52,7 @@ impl IntrinsicObject for ArrayIterator {
.static_method(Self::next, js_string!("next"), 0)
.static_property(
JsSymbol::to_string_tag(),
js_str!("Array Iterator"),
js_string!("Array Iterator"),
Attribute::CONFIGURABLE,
)
.build();
Expand Down
39 changes: 19 additions & 20 deletions core/engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

use boa_gc::{Finalize, Trace};
use boa_macros::js_str;
use boa_profiler::Profiler;
use thin_vec::ThinVec;

Expand Down Expand Up @@ -2169,12 +2168,12 @@ impl Array {
#[cfg(feature = "intl")]
{
// TODO: this should eventually return a locale-sensitive separator.
js_str!(", ")
js_string!(", ")
}

#[cfg(not(feature = "intl"))]
{
js_str!(", ")
js_string!(", ")
}
};

Expand All @@ -2197,7 +2196,7 @@ impl Array {
if !next.is_null_or_undefined() {
// i. Let S be ? ToString(? Invoke(nextElement, "toLocaleString", « locales, options »)).
let s = next
.invoke(js_str!("toLocaleString"), args, context)?
.invoke(js_string!("toLocaleString"), args, context)?
.to_string(context)?;

// ii. Set R to the string-concatenation of R and S.
Expand Down Expand Up @@ -3258,37 +3257,37 @@ impl Array {
{
let mut obj = unscopable_list.borrow_mut();
// 2. Perform ! CreateDataPropertyOrThrow(unscopableList, "at", true).
obj.insert(js_str!("at"), true_prop.clone());
obj.insert(js_string!("at"), true_prop.clone());
// 3. Perform ! CreateDataPropertyOrThrow(unscopableList, "copyWithin", true).
obj.insert(js_str!("copyWithin"), true_prop.clone());
obj.insert(js_string!("copyWithin"), true_prop.clone());
// 4. Perform ! CreateDataPropertyOrThrow(unscopableList, "entries", true).
obj.insert(js_str!("entries"), true_prop.clone());
obj.insert(js_string!("entries"), true_prop.clone());
// 5. Perform ! CreateDataPropertyOrThrow(unscopableList, "fill", true).
obj.insert(js_str!("fill"), true_prop.clone());
obj.insert(js_string!("fill"), true_prop.clone());
// 6. Perform ! CreateDataPropertyOrThrow(unscopableList, "find", true).
obj.insert(js_str!("find"), true_prop.clone());
obj.insert(js_string!("find"), true_prop.clone());
// 7. Perform ! CreateDataPropertyOrThrow(unscopableList, "findIndex", true).
obj.insert(js_str!("findIndex"), true_prop.clone());
obj.insert(js_string!("findIndex"), true_prop.clone());
// 8. Perform ! CreateDataPropertyOrThrow(unscopableList, "findLast", true).
obj.insert(js_str!("findLast"), true_prop.clone());
obj.insert(js_string!("findLast"), true_prop.clone());
// 9. Perform ! CreateDataPropertyOrThrow(unscopableList, "findLastIndex", true).
obj.insert(js_str!("findLastIndex"), true_prop.clone());
obj.insert(js_string!("findLastIndex"), true_prop.clone());
// 10. Perform ! CreateDataPropertyOrThrow(unscopableList, "flat", true).
obj.insert(js_str!("flat"), true_prop.clone());
obj.insert(js_string!("flat"), true_prop.clone());
// 11. Perform ! CreateDataPropertyOrThrow(unscopableList, "flatMap", true).
obj.insert(js_str!("flatMap"), true_prop.clone());
obj.insert(js_string!("flatMap"), true_prop.clone());
// 12. Perform ! CreateDataPropertyOrThrow(unscopableList, "includes", true).
obj.insert(js_str!("includes"), true_prop.clone());
obj.insert(js_string!("includes"), true_prop.clone());
// 13. Perform ! CreateDataPropertyOrThrow(unscopableList, "keys", true).
obj.insert(js_str!("keys"), true_prop.clone());
obj.insert(js_string!("keys"), true_prop.clone());
// 14. Perform ! CreateDataPropertyOrThrow(unscopableList, "toReversed", true).
obj.insert(js_str!("toReversed"), true_prop.clone());
obj.insert(js_string!("toReversed"), true_prop.clone());
// 15. Perform ! CreateDataPropertyOrThrow(unscopableList, "toSorted", true).
obj.insert(js_str!("toSorted"), true_prop.clone());
obj.insert(js_string!("toSorted"), true_prop.clone());
// 16. Perform ! CreateDataPropertyOrThrow(unscopableList, "toSpliced", true).
obj.insert(js_str!("toSpliced"), true_prop.clone());
obj.insert(js_string!("toSpliced"), true_prop.clone());
// 17. Perform ! CreateDataPropertyOrThrow(unscopableList, "values", true).
obj.insert(js_str!("values"), true_prop);
obj.insert(js_string!("values"), true_prop);
}

// 13. Return unscopableList.
Expand Down
4 changes: 2 additions & 2 deletions core/engine/src/builtins/async_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ impl AsyncGenerator {
generator.clone(),
),
)
.name(js_string!(""))
.name(js_string!())
.length(1)
.build();

Expand Down Expand Up @@ -592,7 +592,7 @@ impl AsyncGenerator {
generator.clone(),
),
)
.name(js_string!(""))
.name(js_string!())
.length(1)
.build();

Expand Down
7 changes: 3 additions & 4 deletions core/engine/src/builtins/atomics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::{
JsString, JsValue,
};

use boa_macros::js_str;
use boa_profiler::Profiler;

use super::{
Expand Down Expand Up @@ -468,9 +467,9 @@ impl Atomics {
};

Ok(match result {
futex::AtomicsWaitResult::NotEqual => js_str!("not-equal"),
futex::AtomicsWaitResult::TimedOut => js_str!("timed-out"),
futex::AtomicsWaitResult::Ok => js_str!("ok"),
futex::AtomicsWaitResult::NotEqual => js_string!("not-equal"),
futex::AtomicsWaitResult::TimedOut => js_string!("timed-out"),
futex::AtomicsWaitResult::Ok => js_string!("ok"),
}
.into())
}
Expand Down
6 changes: 5 additions & 1 deletion core/engine/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ impl Boolean {
#[allow(clippy::wrong_self_convention)]
pub(crate) fn to_string(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let boolean = Self::this_boolean_value(this)?;
Ok(JsValue::new(js_string!(boolean.to_string())))
Ok(JsValue::new(if boolean {
js_string!("true")
} else {
js_string!("false")
}))
}

/// The `valueOf()` method returns the primitive value of a `Boolean` object.
Expand Down
12 changes: 5 additions & 7 deletions core/engine/src/builtins/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use boa_macros::js_str;

use crate::{
js_string,
native_function::{NativeFunctionObject, NativeFunctionPointer},
Expand Down Expand Up @@ -119,7 +117,7 @@ impl<S: ApplyToObject + IsConstructor> ApplyToObject for Callable<S> {
.configurable(true),
);
object.insert(
js_str!("name"),
js_string!("name"),
PropertyDescriptor::builder()
.value(self.name)
.writable(false)
Expand Down Expand Up @@ -368,8 +366,8 @@ impl BuiltInConstructorWithPrototype<'_> {
let length = self.length;
let name = self.name.clone();
let prototype = self.prototype.clone();
self = self.static_property(js_str!("length"), length, Attribute::CONFIGURABLE);
self = self.static_property(js_str!("name"), name, Attribute::CONFIGURABLE);
self = self.static_property(js_string!("length"), length, Attribute::CONFIGURABLE);
self = self.static_property(js_string!("name"), name, Attribute::CONFIGURABLE);
self = self.static_property(PROTOTYPE, prototype, Attribute::empty());

let attributes = self.attributes;
Expand Down Expand Up @@ -416,8 +414,8 @@ impl BuiltInConstructorWithPrototype<'_> {
pub(crate) fn build_without_prototype(mut self) {
let length = self.length;
let name = self.name.clone();
self = self.static_property(js_str!("length"), length, Attribute::CONFIGURABLE);
self = self.static_property(js_str!("name"), name, Attribute::CONFIGURABLE);
self = self.static_property(js_string!("length"), length, Attribute::CONFIGURABLE);
self = self.static_property(js_string!("name"), name, Attribute::CONFIGURABLE);

let mut object = self.object.borrow_mut();
let function = object
Expand Down
Loading

0 comments on commit f30514b

Please sign in to comment.