Skip to content

Commit

Permalink
use with_capacity to reduce re-allocations fixes #3896 (#3961)
Browse files Browse the repository at this point in the history
* use with_capacity to reduce allocations

* Update to use const generics over runtime param

* add comment above with_capacity

* - move OWN_PROPS
- add profiling marks in more places

* use const in trait instead
  • Loading branch information
jasonwilliams authored Nov 2, 2024
1 parent d3dbb4a commit d8ec97c
Show file tree
Hide file tree
Showing 56 changed files with 138 additions and 9 deletions.
3 changes: 3 additions & 0 deletions core/engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ impl BuiltInObject for Array {
}

impl BuiltInConstructor for Array {
const P: usize = 41;
const SP: usize = 5;

const LENGTH: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/array_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ impl BuiltInObject for ArrayBuffer {
}

impl BuiltInConstructor for ArrayBuffer {
const P: usize = 9;
const SP: usize = 2;
const LENGTH: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/array_buffer/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ impl BuiltInObject for SharedArrayBuffer {

impl BuiltInConstructor for SharedArrayBuffer {
const LENGTH: usize = 1;
const P: usize = 6;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::shared_array_buffer;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/async_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl BuiltInObject for AsyncFunction {

impl BuiltInConstructor for AsyncFunction {
const LENGTH: usize = 1;
const P: usize = 1;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::async_function;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/async_generator_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl BuiltInObject for AsyncGeneratorFunction {

impl BuiltInConstructor for AsyncGeneratorFunction {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::async_generator_function;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ impl BuiltInObject for BigInt {

impl BuiltInConstructor for BigInt {
const LENGTH: usize = 1;
const P: usize = 3;
const SP: usize = 2;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::bigint;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl BuiltInObject for Boolean {

impl BuiltInConstructor for Boolean {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::boolean;
Expand Down
12 changes: 8 additions & 4 deletions core/engine/src/builtins/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ impl ApplyToObject for OrdinaryObject {
fn apply_to(self, _: &JsObject) {}
}

// The number of properties that are always present in a standard constructor. See build method
const OWN_PROPS: usize = 3;

/// Builder for creating built-in objects, like `Array`.
///
/// The marker `ObjectType` restricts the methods that can be called depending on the
Expand Down Expand Up @@ -528,6 +531,7 @@ impl<'ctx> BuiltInBuilder<'ctx, OrdinaryObject> {
}

impl<'ctx> BuiltInBuilder<'ctx, Callable<Constructor>> {
/// Create a new builder for a constructor function setting the properties ahead of time for optimizations (less reallocations)
pub(crate) fn from_standard_constructor<SC: BuiltInConstructor>(
realm: &'ctx Realm,
) -> BuiltInConstructorWithPrototype<'ctx> {
Expand All @@ -537,11 +541,11 @@ impl<'ctx> BuiltInBuilder<'ctx, Callable<Constructor>> {
function: SC::constructor,
name: js_string!(SC::NAME),
length: SC::LENGTH,
object_property_table: PropertyTableInner::default(),
object_storage: Vec::default(),
object_property_table: PropertyTableInner::with_capacity(SC::SP + OWN_PROPS),
object_storage: Vec::with_capacity(SC::SP + OWN_PROPS),
object: constructor.constructor(),
prototype_property_table: PropertyTableInner::default(),
prototype_storage: Vec::default(),
prototype_property_table: PropertyTableInner::with_capacity(SC::P),
prototype_storage: Vec::with_capacity(SC::P),
prototype: constructor.prototype(),
__proto__: Some(realm.intrinsics().constructors().function().prototype()),
inherits: Some(realm.intrinsics().constructors().object().prototype()),
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/dataview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ impl BuiltInObject for DataView {

impl BuiltInConstructor for DataView {
const LENGTH: usize = 1;
const P: usize = 24;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::data_view;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ impl BuiltInObject for Date {

impl BuiltInConstructor for Date {
const LENGTH: usize = 7;
const P: usize = 47;
const SP: usize = 3;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::date;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl BuiltInObject for AggregateError {

impl BuiltInConstructor for AggregateError {
const LENGTH: usize = 2;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::aggregate_error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ impl BuiltInObject for EvalError {

impl BuiltInConstructor for EvalError {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::eval_error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ impl BuiltInObject for Error {

impl BuiltInConstructor for Error {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl BuiltInObject for RangeError {

impl BuiltInConstructor for RangeError {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::range_error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl BuiltInObject for ReferenceError {

impl BuiltInConstructor for ReferenceError {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::reference_error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ impl BuiltInObject for SyntaxError {

impl BuiltInConstructor for SyntaxError {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::syntax_error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl BuiltInObject for TypeError {

impl BuiltInConstructor for TypeError {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::type_error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/error/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ impl BuiltInObject for UriError {

impl BuiltInConstructor for UriError {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::uri_error;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ impl BuiltInObject for BuiltInFunctionObject {

impl BuiltInConstructor for BuiltInFunctionObject {
const LENGTH: usize = 1;
const P: usize = 7;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::function;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/generator_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ impl BuiltInObject for GeneratorFunction {

impl BuiltInConstructor for GeneratorFunction {
const LENGTH: usize = 1;
const P: usize = 2;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::generator_function;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/intl/collator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ impl BuiltInObject for Collator {

impl BuiltInConstructor for Collator {
const LENGTH: usize = 0;
const P: usize = 3;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::collator;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/intl/date_time_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ impl BuiltInObject for DateTimeFormat {

impl BuiltInConstructor for DateTimeFormat {
const LENGTH: usize = 0;
const P: usize = 0;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::date_time_format;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/intl/list_format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ impl BuiltInObject for ListFormat {

impl BuiltInConstructor for ListFormat {
const LENGTH: usize = 0;
const P: usize = 4;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::list_format;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/intl/locale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ impl BuiltInObject for Locale {

impl BuiltInConstructor for Locale {
const LENGTH: usize = 1;
const P: usize = 14;
const SP: usize = 0;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::locale;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/intl/number_format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ impl BuiltInObject for NumberFormat {

impl BuiltInConstructor for NumberFormat {
const LENGTH: usize = 0;
const P: usize = 3;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::number_format;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/intl/plural_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ impl BuiltInObject for PluralRules {

impl BuiltInConstructor for PluralRules {
const LENGTH: usize = 0;
const P: usize = 4;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::plural_rules;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/intl/segmenter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ impl BuiltInObject for Segmenter {

impl BuiltInConstructor for Segmenter {
const LENGTH: usize = 0;
const P: usize = 3;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::segmenter;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ impl BuiltInObject for Map {

impl BuiltInConstructor for Map {
const LENGTH: usize = 0;
const P: usize = 11;
const SP: usize = 2;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::map;
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod weak_set;
mod builder;

use boa_macros::js_str;
use boa_profiler::Profiler;
use builder::BuiltInBuilder;

#[cfg(feature = "annex-b")]
Expand Down Expand Up @@ -157,6 +158,10 @@ pub(crate) trait BuiltInObject: IntrinsicObject {
///
/// [built-in object]: https://tc39.es/ecma262/#sec-built-in-object
pub(crate) trait BuiltInConstructor: BuiltInObject {
/// Const Generic `P` is the minimum storage capacity for the prototype's Property table.
const P: usize;
/// Const Generic `SP` is the minimum storage capacity for the object's Static Property table.
const SP: usize;
/// The amount of arguments this function object takes.
const LENGTH: usize;

Expand Down Expand Up @@ -304,6 +309,8 @@ impl Realm {
///
/// [spec]: https://tc39.es/ecma262/#sec-setdefaultglobalbindings
pub(crate) fn set_default_global_bindings(context: &mut Context) -> JsResult<()> {
let _timer =
Profiler::global().start_event("Builtins::set_default_global_bindings", "Builtins");
let global_object = context.global_object();

global_object.define_property_or_throw(
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ impl BuiltInObject for Number {

impl BuiltInConstructor for Number {
const LENGTH: usize = 1;
const P: usize = 6;
const SP: usize = 14;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::number;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ impl BuiltInObject for OrdinaryObject {

impl BuiltInConstructor for OrdinaryObject {
const LENGTH: usize = 1;
const P: usize = 11;
const SP: usize = 23;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::object;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/promise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ impl BuiltInObject for Promise {

impl BuiltInConstructor for Promise {
const LENGTH: usize = 1;
const P: usize = 4;
const SP: usize = 9;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::promise;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ impl BuiltInObject for Proxy {

impl BuiltInConstructor for Proxy {
const LENGTH: usize = 2;
const P: usize = 0;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::proxy;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ impl BuiltInObject for RegExp {

impl BuiltInConstructor for RegExp {
const LENGTH: usize = 2;
const P: usize = 19;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::regexp;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ impl BuiltInObject for Set {

impl BuiltInConstructor for Set {
const LENGTH: usize = 0;
const P: usize = 11;
const SP: usize = 1;
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::set;

Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ impl BuiltInObject for String {

impl BuiltInConstructor for String {
const LENGTH: usize = 1;
const P: usize = 36;
const SP: usize = 3;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::string;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/symbol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ impl BuiltInObject for Symbol {

impl BuiltInConstructor for Symbol {
const LENGTH: usize = 0;
const P: usize = 5;
const SP: usize = 15;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::symbol;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/temporal/duration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ impl IntrinsicObject for Duration {

impl BuiltInConstructor for Duration {
const LENGTH: usize = 0;
const P: usize = 22;
const SP: usize = 1;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::duration;
Expand Down
2 changes: 2 additions & 0 deletions core/engine/src/builtins/temporal/instant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ impl IntrinsicObject for Instant {

impl BuiltInConstructor for Instant {
const LENGTH: usize = 1;
const P: usize = 13;
const SP: usize = 4;

const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::instant;
Expand Down
Loading

0 comments on commit d8ec97c

Please sign in to comment.