Skip to content

Commit d8ec97c

Browse files
use with_capacity to reduce re-allocations fixes #3896 (#3961)
* 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
1 parent d3dbb4a commit d8ec97c

File tree

56 files changed

+138
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+138
-9
lines changed

core/engine/src/builtins/array/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ impl BuiltInObject for Array {
190190
}
191191

192192
impl BuiltInConstructor for Array {
193+
const P: usize = 41;
194+
const SP: usize = 5;
195+
193196
const LENGTH: usize = 1;
194197

195198
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =

core/engine/src/builtins/array_buffer/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ impl BuiltInObject for ArrayBuffer {
374374
}
375375

376376
impl BuiltInConstructor for ArrayBuffer {
377+
const P: usize = 9;
378+
const SP: usize = 2;
377379
const LENGTH: usize = 1;
378380

379381
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =

core/engine/src/builtins/array_buffer/shared.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ impl BuiltInObject for SharedArrayBuffer {
155155

156156
impl BuiltInConstructor for SharedArrayBuffer {
157157
const LENGTH: usize = 1;
158+
const P: usize = 6;
159+
const SP: usize = 1;
158160

159161
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
160162
StandardConstructors::shared_array_buffer;

core/engine/src/builtins/async_function/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ impl BuiltInObject for AsyncFunction {
5252

5353
impl BuiltInConstructor for AsyncFunction {
5454
const LENGTH: usize = 1;
55+
const P: usize = 1;
56+
const SP: usize = 0;
5557

5658
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
5759
StandardConstructors::async_function;

core/engine/src/builtins/async_generator_function/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ impl BuiltInObject for AsyncGeneratorFunction {
5757

5858
impl BuiltInConstructor for AsyncGeneratorFunction {
5959
const LENGTH: usize = 1;
60+
const P: usize = 2;
61+
const SP: usize = 0;
6062

6163
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
6264
StandardConstructors::async_generator_function;

core/engine/src/builtins/bigint/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ impl BuiltInObject for BigInt {
6565

6666
impl BuiltInConstructor for BigInt {
6767
const LENGTH: usize = 1;
68+
const P: usize = 3;
69+
const SP: usize = 2;
6870

6971
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
7072
StandardConstructors::bigint;

core/engine/src/builtins/boolean/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ impl BuiltInObject for Boolean {
5151

5252
impl BuiltInConstructor for Boolean {
5353
const LENGTH: usize = 1;
54+
const P: usize = 2;
55+
const SP: usize = 0;
5456

5557
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
5658
StandardConstructors::boolean;

core/engine/src/builtins/builder.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ impl ApplyToObject for OrdinaryObject {
135135
fn apply_to(self, _: &JsObject) {}
136136
}
137137

138+
// The number of properties that are always present in a standard constructor. See build method
139+
const OWN_PROPS: usize = 3;
140+
138141
/// Builder for creating built-in objects, like `Array`.
139142
///
140143
/// The marker `ObjectType` restricts the methods that can be called depending on the
@@ -528,6 +531,7 @@ impl<'ctx> BuiltInBuilder<'ctx, OrdinaryObject> {
528531
}
529532

530533
impl<'ctx> BuiltInBuilder<'ctx, Callable<Constructor>> {
534+
/// Create a new builder for a constructor function setting the properties ahead of time for optimizations (less reallocations)
531535
pub(crate) fn from_standard_constructor<SC: BuiltInConstructor>(
532536
realm: &'ctx Realm,
533537
) -> BuiltInConstructorWithPrototype<'ctx> {
@@ -537,11 +541,11 @@ impl<'ctx> BuiltInBuilder<'ctx, Callable<Constructor>> {
537541
function: SC::constructor,
538542
name: js_string!(SC::NAME),
539543
length: SC::LENGTH,
540-
object_property_table: PropertyTableInner::default(),
541-
object_storage: Vec::default(),
544+
object_property_table: PropertyTableInner::with_capacity(SC::SP + OWN_PROPS),
545+
object_storage: Vec::with_capacity(SC::SP + OWN_PROPS),
542546
object: constructor.constructor(),
543-
prototype_property_table: PropertyTableInner::default(),
544-
prototype_storage: Vec::default(),
547+
prototype_property_table: PropertyTableInner::with_capacity(SC::P),
548+
prototype_storage: Vec::with_capacity(SC::P),
545549
prototype: constructor.prototype(),
546550
__proto__: Some(realm.intrinsics().constructors().function().prototype()),
547551
inherits: Some(realm.intrinsics().constructors().object().prototype()),

core/engine/src/builtins/dataview/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ impl BuiltInObject for DataView {
167167

168168
impl BuiltInConstructor for DataView {
169169
const LENGTH: usize = 1;
170+
const P: usize = 24;
171+
const SP: usize = 0;
170172

171173
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
172174
StandardConstructors::data_view;

core/engine/src/builtins/date/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ impl BuiltInObject for Date {
185185

186186
impl BuiltInConstructor for Date {
187187
const LENGTH: usize = 7;
188+
const P: usize = 47;
189+
const SP: usize = 3;
188190

189191
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
190192
StandardConstructors::date;

0 commit comments

Comments
 (0)