diff --git a/TODO.md b/TODO.md index e501aa60a..e0f01c321 100644 --- a/TODO.md +++ b/TODO.md @@ -1166,7 +1166,8 @@ only reproducible input. opaque callee that returns its strict `arguments` costs 340 ns per call against Node's 6.6 (`call-arguments-escape`). Dense storage needs define, delete, freeze, and own-key order to understand it, and a move to the table once an - index gets non-default attributes. + index gets non-default attributes. In V8's EarleyBoyer, creating arguments + objects takes 19% of the main thread's samples. - [ ] Deterministically release nonescaping RegExp and ICU handles at compiled scope end once ownership analysis proves their lifetime. Retain finalization for every @@ -1184,6 +1185,28 @@ contracts or investigates costs still visible after the string follow-ups. ## Focused performance work +- [ ] Build fewer primordials at startup. A closed-world `console.log` program + starts in 2.1 ms against Porffor's 1.3 and a 1.2 ms spawn floor, and + `mal_intrinsics_init` takes about 0.7 ms of that. The cost is spread over the + builtin installs at 10 to 50 µs each, so only lazy installs for families a + program never reaches, or a prebuilt heap image, would remove most of it. +- [ ] Search UTF-16 subjects with regress start predicates. regress 0.11.1 skips its + start-position prefilter whenever the `utf16` feature is on, so a pattern that + can only start at `<` tries a backtracking match at every code unit. Upstream + master searches UTF-16 input with unit predicates, which takes + `/\x20\t\r\n\f]*` is followed by `[^>]*`, which accepts every unit the + first run gives back, so treating such a run as possessive would remove most + of that work. +- [ ] Admit array literals whose constant keys LICM hoisted into a later loop's + preheader. The native initialization region resolves keys only from the + function's entry chain or the instructions just before the allocation. A + 13-element literal in a second loop that shares keys with an earlier literal + therefore falls back to `createArray` and one `defineProperty` per element: + 4M of them take 270 ms against Bun's 21. - [ ] Keep medium flat strings' units in their own cell. A string past the 16 Latin-1 or 8 UTF-16 inline units allocates a separate owned buffer that a finalizer frees, so building `${order.region}/${order.category}` costs 27.7 ns diff --git a/runtime/secret_buffer_test_main.c b/runtime/secret_buffer_test_main.c index 396ca17fa..35ffc1a72 100644 --- a/runtime/secret_buffer_test_main.c +++ b/runtime/secret_buffer_test_main.c @@ -54,15 +54,19 @@ static void secret_observation_reset(void) { memset(&g_observed, 0, sizeof(g_observed)); } +/* An ordinary store this small would live in its cell and never reach the release + * observer, so the ordinary control uses one past the inline limit. */ +#define PLAIN_LENGTH (MAL_ARRAY_BUFFER_INLINE_MAX_BYTES + SECRET_LENGTH) + static MalArrayBufferObject *secret_buffer_new(MalVm *vm, bool sensitive) { MalObject *prototype = mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]); + u32 length = sensitive ? SECRET_LENGTH : PLAIN_LENGTH; MalArrayBufferObject *buffer = sensitive - ? mal_array_buffer_object_new_sensitive(&vm->heap, prototype, SECRET_LENGTH) - : mal_array_buffer_object_new( - &vm->heap, prototype, SECRET_LENGTH, SECRET_LENGTH, false, false); + ? mal_array_buffer_object_new_sensitive(&vm->heap, prototype, length) + : mal_array_buffer_object_new(&vm->heap, prototype, length, length, false, false); if (buffer == nullptr || buffer->data == nullptr) return nullptr; - memset(buffer->data, SECRET_BYTE, SECRET_LENGTH); + memset(buffer->data, SECRET_BYTE, length); g_observed.target = (uptr) buffer; return buffer; } @@ -128,8 +132,9 @@ static bool a_shrunken_store_is_scrubbed_to_its_capacity(MalVm *vm) { mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]); MalArrayBufferObject *buffer = mal_array_buffer_object_new( &vm->heap, prototype, SECRET_LENGTH, SECRET_LENGTH, true, false); - if (buffer->data == nullptr) return false; - buffer->sensitive = true; + if (buffer->data == nullptr || !mal_array_buffer_object_set_sensitive(&vm->heap, buffer, true)) { + return false; + } memset(buffer->data, SECRET_BYTE, SECRET_LENGTH); g_observed.target = (uptr) buffer; bool shrunk = mal_array_buffer_object_resize(buffer, 8); @@ -215,11 +220,11 @@ static NativeStore native_store(MalVm *vm, bool adopted, u32 length, u32 capacit MalArrayBufferObject *buffer = adopted ? mal_array_buffer_object_adopt(&vm->heap, prototype, malloc(capacity), capacity, true) : mal_array_buffer_object_new(&vm->heap, prototype, length, capacity, resizable, false); - buffer->sensitive = true; + if (!mal_array_buffer_object_set_sensitive(&vm->heap, buffer, true)) abort(); if (buffer->data != nullptr) memset(buffer->data, SECRET_BYTE, buffer->allocation_capacity); if (resizable && !mal_array_buffer_object_resize(buffer, 8)) abort(); - MalArrayBufferStoreLease *lease = mal_array_buffer_object_retain_store(buffer); - MalArrayBufferStoreLease *other = mal_array_buffer_object_retain_store(buffer); + MalArrayBufferStoreLease *lease = mal_array_buffer_object_retain_store(&vm->heap, buffer); + MalArrayBufferStoreLease *other = mal_array_buffer_object_retain_store(&vm->heap, buffer); if (lease == nullptr || other == nullptr) abort(); mal_array_buffer_store_release(other); return (NativeStore) {lease, buffer->data, buffer->allocation_capacity, (uptr) buffer, false}; diff --git a/runtime/src/array_buffer_object.c b/runtime/src/array_buffer_object.c index be2e76607..a404d0a3b 100644 --- a/runtime/src/array_buffer_object.c +++ b/runtime/src/array_buffer_object.c @@ -29,9 +29,33 @@ static void release_store_data(byte *data, u32 capacity, const MalArrayBufferRel mal_gc_process_release(capacity); } -MalArrayBufferStoreLease *mal_array_buffer_object_retain_store(MalArrayBufferObject *buffer) { +bool mal_array_buffer_object_move_out_of_cell(MalHeap *heap, MalArrayBufferObject *buffer) { + u32 capacity = buffer->allocation_capacity; + byte *data = malloc(capacity); + if (data == nullptr) return false; + memcpy(data, buffer->inline_bytes, capacity); + buffer->data = data; + mal_heap_cell_needs_finalizer(&buffer->object.header); + mal_heap_array_buffer_acquire(heap, capacity); + mal_gc_process_charge(capacity); + mal_profile_native_allocation(heap, capacity, MAL_PROFILE_ALLOCATION_FAMILY_BUFFER); + return true; +} + +bool mal_array_buffer_object_set_sensitive( + MalHeap *heap, MalArrayBufferObject *buffer, bool sensitive +) { + if (sensitive && !mal_array_buffer_object_externalize(heap, buffer)) return false; + buffer->sensitive = sensitive; + return true; +} + +MalArrayBufferStoreLease *mal_array_buffer_object_retain_store( + MalHeap *heap, MalArrayBufferObject *buffer +) { if (buffer->shared || buffer->detached - || (buffer->data == nullptr && buffer->allocation_capacity > 0)) return nullptr; + || (buffer->data == nullptr && buffer->allocation_capacity > 0) + || !mal_array_buffer_object_externalize(heap, buffer)) return nullptr; MalArrayBufferStoreLease *lease = buffer->store_lease; if (lease == nullptr) { lease = malloc(sizeof(*lease)); @@ -68,14 +92,15 @@ struct MalArrayBufferPin { u32 references; }; -MalArrayBufferPin *mal_array_buffer_object_retain_pin(MalArrayBufferObject *buffer) { - if (buffer->pins == UINT32_MAX) return nullptr; +MalArrayBufferPin *mal_array_buffer_object_retain_pin(MalHeap *heap, MalArrayBufferObject *buffer) { + if (buffer->pins == UINT32_MAX || !mal_array_buffer_object_externalize(heap, buffer)) return nullptr; MalArrayBufferPin *pin = buffer->retained_pin; if (pin == nullptr) { pin = malloc(sizeof(*pin)); if (pin == nullptr) return nullptr; *pin = (MalArrayBufferPin) {.buffer = buffer}; buffer->retained_pin = pin; + mal_heap_cell_needs_finalizer(&buffer->object.header); } pin->references++; buffer->pins++; @@ -98,15 +123,27 @@ static MalArrayBufferObject *mal_array_buffer_object_new_impl( u32 max_byte_length, bool resizable, bool shared, - bool initialize + bool initialize, + bool sensitive ) { - MalArrayBufferObject *buffer = mal_heap_alloc(heap, sizeof(MalArrayBufferObject), MAL_HEAP_ARRAY_BUFFER_OBJECT); - mal_object_init(heap, &buffer->object, MAL_HEAP_ARRAY_BUFFER_OBJECT, prototype); - // Resizable buffers reserve the maximum so the backing store never moves. u32 capacity = resizable ? max_byte_length : byte_length; + // A small fixed store lives in the cell, which allocation already zeroed, so + // the sweep reclaims it with the cell instead of finalizing and freeing it. + bool store_inline = !resizable && !shared && !sensitive && capacity > 0 + && capacity <= MAL_ARRAY_BUFFER_INLINE_MAX_BYTES; + MalArrayBufferObject *buffer = mal_heap_alloc( + heap, sizeof(MalArrayBufferObject) + (store_inline ? capacity : 0), + MAL_HEAP_ARRAY_BUFFER_OBJECT); + mal_object_init(heap, &buffer->object, MAL_HEAP_ARRAY_BUFFER_OBJECT, prototype); + buffer->data = nullptr; buffer->shared_memory = nullptr; + if (store_inline) { + buffer->data = buffer->inline_bytes; + } else { + mal_heap_cell_needs_finalizer(&buffer->object.header); + } if (shared) { // Even a zero-length SharedArrayBuffer needs a backing: its identity is // what clones and wait/notify keys share across isolates. @@ -114,7 +151,7 @@ static MalArrayBufferObject *mal_array_buffer_object_new_impl( if (buffer->shared_memory != nullptr) { buffer->data = mal_shared_memory_data(buffer->shared_memory); } - } else if (capacity > 0) { + } else if (capacity > 0 && !store_inline) { // Only currently exposed bytes require zero semantics. Reserving a much // larger resizable capacity must not eagerly dirty every future page; // resize zeroes each newly exposed range before publishing its length. @@ -127,7 +164,7 @@ static MalArrayBufferObject *mal_array_buffer_object_new_impl( buffer->data = calloc(capacity, 1); } } - if (buffer->data != nullptr && !shared) { + if (buffer->data != nullptr && !shared && !store_inline) { mal_heap_array_buffer_acquire(heap, capacity); mal_gc_process_charge(capacity); mal_profile_native_allocation( @@ -140,7 +177,7 @@ static MalArrayBufferObject *mal_array_buffer_object_new_impl( buffer->detached = false; buffer->shared = shared; buffer->immutable = false; - buffer->sensitive = false; + buffer->sensitive = sensitive; buffer->pins = 0; buffer->retained_pin = nullptr; buffer->store_lease = nullptr; @@ -157,16 +194,16 @@ MalArrayBufferObject *mal_array_buffer_object_new( bool shared ) { return mal_array_buffer_object_new_impl( - heap, prototype, byte_length, max_byte_length, resizable, shared, true); + heap, prototype, byte_length, max_byte_length, resizable, shared, true, false); } MalArrayBufferObject *mal_array_buffer_object_new_uninitialized( MalHeap *heap, MalObject *prototype, u32 byte_length, u32 max_byte_length, - bool resizable, bool shared) { + bool resizable, bool shared, bool sensitive) { return mal_array_buffer_object_new_impl( heap, prototype, byte_length, max_byte_length, - resizable, shared, false); + resizable, shared, false, sensitive); } MalArrayBufferObject *mal_array_buffer_object_move_store( @@ -179,6 +216,20 @@ MalArrayBufferObject *mal_array_buffer_object_move_store( (resizable && byte_length > max_byte_length)) { abort(); } + if (mal_array_buffer_object_store_is_inline(source)) { + // Inline bytes die with the source cell, so they are copied rather than moved. + // An inline store is fixed and never sensitive, and the copy fits inline too. + MalArrayBufferObject *result = mal_array_buffer_object_new_impl( + heap, prototype, byte_length, max_byte_length, resizable, false, true, false); + u32 kept = byte_length < source->byte_length ? byte_length : source->byte_length; + memcpy(result->data, source->data, kept); + result->immutable = immutable; + source->data = nullptr; + source->byte_length = 0; + source->allocation_capacity = 0; + source->detached = true; + return result; + } if (byte_length > source->byte_length) { memset(source->data + source->byte_length, 0, byte_length - source->byte_length); @@ -186,7 +237,7 @@ MalArrayBufferObject *mal_array_buffer_object_move_store( // The process charge travels with the allocation: the empty header charged // nothing, and the detached source no longer frees it. MalArrayBufferObject *result = mal_array_buffer_object_new_impl( - heap, prototype, 0, 0, false, false, false); + heap, prototype, 0, 0, false, false, false, false); result->data = source->data; result->store_lease = mal_array_buffer_object_take_store_lease(source); result->byte_length = byte_length; @@ -207,10 +258,8 @@ MalArrayBufferObject *mal_array_buffer_object_move_store( MalArrayBufferObject *mal_array_buffer_object_new_sensitive( MalHeap *heap, MalObject *prototype, u32 byte_length ) { - MalArrayBufferObject *buffer = - mal_array_buffer_object_new(heap, prototype, byte_length, byte_length, false, false); - buffer->sensitive = true; - return buffer; + return mal_array_buffer_object_new_impl( + heap, prototype, byte_length, byte_length, false, false, true, true); } MalArrayBufferObject *mal_array_buffer_object_adopt( @@ -236,7 +285,7 @@ MalArrayBufferObject *mal_array_buffer_object_adopt( MalArrayBufferObject *mal_array_buffer_object_wrap_shared( MalHeap *heap, MalObject *prototype, MalSharedMemory *memory) { MalArrayBufferObject *buffer = mal_array_buffer_object_new_impl( - heap, prototype, 0, 0, false, false, false); + heap, prototype, 0, 0, false, false, false, false); mal_shared_memory_retain(memory); buffer->shared_memory = memory; buffer->data = mal_shared_memory_data(memory); @@ -280,6 +329,11 @@ void mal_array_buffer_object_release_store(MalHeap *heap, MalArrayBufferObject * buffer->allocation_capacity = 0; return; } + if (mal_array_buffer_object_store_is_inline(buffer)) { + buffer->data = nullptr; + buffer->allocation_capacity = 0; + return; + } MalArrayBufferStoreLease *lease = mal_array_buffer_object_take_store_lease(buffer); if (buffer->data != nullptr) mal_heap_array_buffer_release(heap, buffer->allocation_capacity); if (lease != nullptr) mal_array_buffer_store_release(lease); diff --git a/runtime/src/array_buffer_object.h b/runtime/src/array_buffer_object.h index ac1eb74b9..47f08f8bf 100644 --- a/runtime/src/array_buffer_object.h +++ b/runtime/src/array_buffer_object.h @@ -10,9 +10,14 @@ typedef struct MalArrayBufferStoreLease MalArrayBufferStoreLease; /** * ArrayBuffer / SharedArrayBuffer backing store. * - * `data` is a native allocation. A store lease can retain its bytes independently - * of the GC wrapper; ordinary stores are released by detach or the GC sweep. - * An ordinary store charges its allocation_capacity to the process budget + * `data` is a native allocation, or the cell's own `inline_bytes` for a small + * fixed-length store. An inline store dies with its cell, so such a buffer owns + * nothing for the sweep to finalize; it moves to a native allocation before its + * bytes can outlive the cell (a pin, a store lease, a transfer) or must be scrubbed + * (sensitive). The heap never moves cells, so inline bytes stay put while the + * buffer lives. A store lease can retain native bytes independently of the GC + * wrapper; ordinary stores are released by detach or the GC sweep. + * A native store charges its allocation_capacity to the process budget * (gc_process.h) once; the charge moves with the allocation (move_store, * structured-clone transfer) and is released only where it is freed. Shared * backings are charged by shared_memory.c instead. @@ -52,14 +57,46 @@ typedef struct MalArrayBufferObject { u32 pins; MalArrayBufferPin *retained_pin; MalArrayBufferStoreLease *store_lease; + alignas(16) byte inline_bytes[]; } MalArrayBufferObject; -/** A VM-thread pin that can be retired after its buffer wrapper has been swept. */ -MalArrayBufferPin *mal_array_buffer_object_retain_pin(MalArrayBufferObject *buffer); +/** Largest fixed-length ordinary store kept inside its cell. */ +#define MAL_ARRAY_BUFFER_INLINE_MAX_BYTES 256u + +static inline bool mal_array_buffer_object_store_is_inline(const MalArrayBufferObject *buffer) { + return buffer->data == buffer->inline_bytes; +} + +/** Whether a dead buffer holds storage outside its cell that its finalizer must release. */ +static inline bool mal_array_buffer_object_owns_storage(const MalArrayBufferObject *buffer) { + return (buffer->data != nullptr && !mal_array_buffer_object_store_is_inline(buffer)) + || buffer->shared_memory != nullptr || buffer->retained_pin != nullptr + || buffer->store_lease != nullptr; +} + +bool mal_array_buffer_object_move_out_of_cell(MalHeap *heap, MalArrayBufferObject *buffer); + +/** Move an inline store to a native allocation; false when that allocation fails. */ +static inline bool mal_array_buffer_object_externalize( + MalHeap *heap, MalArrayBufferObject *buffer +) { + return !mal_array_buffer_object_store_is_inline(buffer) + || mal_array_buffer_object_move_out_of_cell(heap, buffer); +} + +/** Apply the scrub-before-release contract, moving an inline store out of its cell + * first so the sweep scrubs it; false when that allocation fails. */ +bool mal_array_buffer_object_set_sensitive( + MalHeap *heap, MalArrayBufferObject *buffer, bool sensitive); + +/** A VM-thread pin that can be retired after its buffer wrapper has been swept. + * Pinned bytes never live in the cell, since C may hold them past the wrapper. */ +MalArrayBufferPin *mal_array_buffer_object_retain_pin(MalHeap *heap, MalArrayBufferObject *buffer); void mal_array_buffer_object_release_pin(MalArrayBufferPin *pin); /** Retain ordinary backing bytes independently of their wrapper. Does not pin it. */ -MalArrayBufferStoreLease *mal_array_buffer_object_retain_store(MalArrayBufferObject *buffer); +MalArrayBufferStoreLease *mal_array_buffer_object_retain_store( + MalHeap *heap, MalArrayBufferObject *buffer); /** Retire a native owner on any thread; the last owner scrubs and frees the store. */ void mal_array_buffer_store_release(MalArrayBufferStoreLease *lease); /** Move the lease's wrapper reference alongside a raw store ownership transfer. */ @@ -84,12 +121,12 @@ MalArrayBufferObject *mal_array_buffer_object_new( * This is only for leaf native constructors that overwrite the complete visible * range before the object can become observable. Hidden resizable capacity is * still zeroed by resize immediately before exposure. Prefer the initialized - * constructor otherwise. + * constructor otherwise. `sensitive` selects the scrub-before-release contract. */ MalArrayBufferObject *mal_array_buffer_object_new_uninitialized( MalHeap *heap, MalObject *prototype, u32 byte_length, u32 max_byte_length, - bool resizable, bool shared); + bool resizable, bool shared, bool sensitive); /** * Move an existing store into a fresh ArrayBuffer header without copying its diff --git a/runtime/src/array_object.h b/runtime/src/array_object.h index 4ae6abbc3..ffe86c360 100644 --- a/runtime/src/array_object.h +++ b/runtime/src/array_object.h @@ -72,7 +72,7 @@ static inline void mal_array_object_card(MalArrayObject *array, u32 index, MalVa } mal_gc_array_card_slow(owner, index, value); } -#define MAL_ARRAY_INLINE_CAPACITY_MAX 8u +#define MAL_ARRAY_INLINE_CAPACITY_MAX 14u static_assert(sizeof(MalArrayObject) <= 64, "MalArrayObject outgrew its 64-byte size class"); diff --git a/runtime/src/builtin_array.c b/runtime/src/builtin_array.c index 1d46be2f4..58ed6d4a7 100644 --- a/runtime/src/builtin_array.c +++ b/runtime/src/builtin_array.c @@ -810,6 +810,9 @@ static bool mal_builtin_array_create_data_property_wide(MalVm *vm, MalValue targ static bool mal_builtin_array_create_data_property(MalVm *vm, MalValue target, u32 index, MalValue value); static bool mal_builtin_array_from_set_length(MalVm *vm, MalValue a, u32 length); +/** 512 KiB of elements: what a sparse `new Array(n)` may hold before its first store. */ +#define MAL_ARRAY_CREATE_RESERVE_MAX 65536u + static MalValue mal_builtin_array_constructor(MalVm *vm, MalValue this_value, const MalValue *args, i32 arg_count, MalValue new_target, MalValue callee) { (void) this_value; // A single Number argument is the new array's length: ToUint32(len) must @@ -821,7 +824,14 @@ static MalValue mal_builtin_array_constructor(MalVm *vm, MalValue this_value, co mal_vm_throw_error(vm, MAL_INTRINSIC_RANGE_ERROR_PROTOTYPE, "Invalid array length"); return mal_value_new_undefined(); } - return mal_value_from_array_object(mal_intrinsic_new_array(vm, length)); + // ArrayCreate(len) is nearly always filled next, so a bounded length gets + // its elements at birth. Longer arrays are often sparse and grow on demand. + MalObject *prototype = mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_PROTOTYPE]); + MalArrayObject *array = length <= MAL_ARRAY_CREATE_RESERVE_MAX + ? mal_array_object_new_reserved(&vm->heap, prototype, length) + : mal_array_object_new(&vm->heap, prototype); + mal_array_object_set_length(array, length); + return mal_value_from_array_object(array); } MalArrayObject *array = mal_intrinsic_new_dense_array(vm, (u32) arg_count); @@ -3819,6 +3829,64 @@ static bool mal_builtin_array_sort_pair_order( return mal_builtin_array_sort_order(vm, comparator, left, right, order_out); } +bool mal_numeric_radix_sort( + MalValue *values, MalValue *scratch, const f64 *keys, u32 count, bool descending +) { + usize bytes; + if (!mal_checked_size_multiply(count, 2 * (sizeof(u64) + sizeof(u32)), SIZE_MAX, &bytes)) { + return false; + } + u64 *storage = malloc(bytes); + if (storage == nullptr) return false; + u64 *radix = storage; + u64 *radix_out = radix + count; + u32 *order = (u32 *) (radix_out + count); + u32 *order_out = order + count; + u32 digits[8][256] = {0}; + for (u32 index = 0; index < count; index++) { + f64 key = keys[index]; + if (key != key) { + free(storage); + return false; + } + // The comparator ties -0 with 0, so both take one key and keep their order. + f64 normalized = key == 0 ? 0.0 : key; + u64 bits; + memcpy(&bits, &normalized, sizeof(bits)); + bits = bits >> 63 ? ~bits : bits | (UINT64_C(1) << 63); + if (descending) bits = ~bits; + radix[index] = bits; + order[index] = index; + for (u32 digit = 0; digit < 8; digit++) digits[digit][(bits >> (8 * digit)) & 0xff]++; + } + for (u32 digit = 0; digit < 8; digit++) { + u32 *bucket = digits[digit]; + u32 shift = 8 * digit; + // A byte every key shares cannot reorder anything. + if (bucket[(radix[0] >> shift) & 0xff] == count) continue; + for (u32 value = 0, offset = 0; value < 256; value++) { + u32 size = bucket[value]; + bucket[value] = offset; + offset += size; + } + for (u32 index = 0; index < count; index++) { + u32 slot = bucket[(radix[index] >> shift) & 0xff]++; + radix_out[slot] = radix[index]; + order_out[slot] = order[index]; + } + u64 *swap_radix = radix; + radix = radix_out; + radix_out = swap_radix; + u32 *swap_order = order; + order = order_out; + order_out = swap_order; + } + for (u32 index = 0; index < count; index++) scratch[index] = values[order[index]]; + memcpy(values, scratch, sizeof(MalValue) * count); + free(storage); + return true; +} + static bool mal_array_sort_record_keys( const MalValue *values, u32 count, MalString *const *keys, u32 key_count, f64 *out ) { @@ -5358,7 +5426,7 @@ static bool mal_array_default_species(MalVm *vm, MalValue recv) { // own layout holds no `constructor` that could redirect species. bool may_shadow = (receiver->shape != nullptr && receiver->shape->inline_count != 0) || mal_object_has_public_overflow(receiver); - MalKey ctor_key = mal_intrinsic_string_key(vm, (const byte *) "constructor"); + MalKey ctor_key = mal_intrinsic_hot_string_key(vm, MAL_HOT_KEY_CONSTRUCTOR); if (may_shadow && mal_object_get_own(receiver, ctor_key).present) { return false; } diff --git a/runtime/src/builtin_array_buffer.c b/runtime/src/builtin_array_buffer.c index 1fa0178f8..e8168e3af 100644 --- a/runtime/src/builtin_array_buffer.c +++ b/runtime/src/builtin_array_buffer.c @@ -317,7 +317,7 @@ static MalValue mal_builtin_array_buffer_slice(MalVm *vm, MalValue this_value, c &vm->heap, mal_value_to_object( vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]), - new_length, new_length, false, false)); + new_length, new_length, false, false, false)); } else { MalValue len_arg = mal_value_from_i32((i32) new_length); MalCompletion completion = @@ -454,11 +454,11 @@ static MalValue mal_builtin_array_buffer_transfer_impl(MalVm *vm, MalValue this_ return mal_value_from_array_buffer_object(result); } - MalArrayBufferObject *result = mal_array_buffer_object_new_uninitialized( - &vm->heap, prototype, new_length, max_byte_length, resizable, false); // The contents move, so the scrub-before-release contract moves with them: // transferring a derived key must not leave an unscrubbed copy behind. - result->sensitive = buffer->sensitive; + MalArrayBufferObject *result = mal_array_buffer_object_new_uninitialized( + &vm->heap, prototype, new_length, max_byte_length, resizable, false, + buffer->sensitive); u32 copy = new_length < buffer->byte_length ? new_length : buffer->byte_length; if (copy > 0) { memcpy(result->data, buffer->data, copy); @@ -535,9 +535,8 @@ static MalValue mal_builtin_array_buffer_transfer_to_immutable(MalVm *vm, MalVal MalArrayBufferObject *result = mal_array_buffer_object_new_uninitialized( &vm->heap, prototype, - new_length, new_length, false, false); + new_length, new_length, false, false, buffer->sensitive); result->immutable = true; - result->sensitive = buffer->sensitive; u32 copy = new_length < buffer->byte_length ? new_length : buffer->byte_length; if (copy > 0) { memcpy(result->data, buffer->data, copy); @@ -588,10 +587,9 @@ static MalValue mal_builtin_array_buffer_slice_to_immutable(MalVm *vm, MalValue MalArrayBufferObject *result = mal_array_buffer_object_new_uninitialized( &vm->heap, mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]), - new_length, new_length, false, false + new_length, new_length, false, false, buffer->sensitive ); result->immutable = true; - result->sensitive = buffer->sensitive; if (new_length > 0) { memcpy(result->data, buffer->data + first, new_length); } @@ -710,7 +708,7 @@ static MalValue mal_shared_array_buffer_slice(MalVm *vm, MalValue this_value, co &vm->heap, mal_value_to_object( vm->intrinsics[MAL_INTRINSIC_SHARED_ARRAY_BUFFER_PROTOTYPE]), - new_length, new_length, false, true)); + new_length, new_length, false, true, false)); } else { MalValue len_arg = mal_value_from_i32((i32) new_length); MalCompletion completion = diff --git a/runtime/src/builtin_object.c b/runtime/src/builtin_object.c index bed7befa5..478d58f01 100644 --- a/runtime/src/builtin_object.c +++ b/runtime/src/builtin_object.c @@ -1543,6 +1543,42 @@ static bool mal_builtin_object_assign_from(MalVm *vm, MalValue target, MalValue return ok; } +#if MAL_PRIMORDIALS_LOCKED +/** + * Whether [[Set]] of data properties on `target` defines them exactly as object spread + * does: an extensible ordinary target whose prototype is the locked %Object.prototype%, + * whose own properties are all default-attribute data properties. A write then either + * replaces such a value or creates a property, except through the inherited + * `__proto__` setter, which the caller excludes per source. + */ +static bool mal_builtin_object_assign_plain_target(MalVm *vm, MalValue target_value) { + if (!mal_value_is_heap_type(target_value, MAL_HEAP_OBJECT)) return false; + const MalObject *target = mal_value_to_object(target_value); + if (!target->extensible || target->is_prototype || mal_object_has_public_overflow(target) || + mal_object_prototype(target) != + mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_OBJECT_PROTOTYPE])) { + return false; + } + const u8 default_attrs = + MAL_PROPERTY_WRITABLE | MAL_PROPERTY_ENUMERABLE | MAL_PROPERTY_CONFIGURABLE; + for (u32 i = 0; i < target->shape->inline_count; i++) { + if (target->shape->props[i].attrs != default_attrs) return false; + } + return true; +} + +static bool mal_builtin_object_assign_names_proto(MalVm *vm, const MalObject *source) { + MalValue proto = 0; + for (u32 i = 0; i < source->shape->inline_count; i++) { + MalValue key = source->shape->props[i].key; + if (!mal_value_is_string(key) || mal_string_length(mal_value_to_string(key)) != 9) continue; + if (proto == 0) proto = mal_intrinsic_string_key(vm, (const byte *) "__proto__").value; + if (mal_string_equals(mal_value_to_string(key), mal_value_to_string(proto))) return true; + } + return false; +} +#endif + static MalValue mal_builtin_object_assign(MalVm *vm, MalValue this_value, const MalValue *args, i32 arg_count, MalValue new_target, MalValue callee) { (void) this_value; (void) new_target; @@ -1578,6 +1614,14 @@ static MalValue mal_builtin_object_assign(MalVm *vm, MalValue this_value, const source_value = mal_builtin_object_box_primitive(vm, source_value); } roots[1] = source_value; +#if MAL_PRIMORDIALS_LOCKED + if (mal_builtin_object_assign_plain_target(vm, target_value) && + mal_value_is_heap_type(source_value, MAL_HEAP_OBJECT) && + !mal_builtin_object_assign_names_proto(vm, mal_value_to_object(source_value)) && + mal_vm_try_merge_data_properties(vm, target_value, source_value)) { + continue; + } +#endif if (!mal_builtin_object_assign_from(vm, target_value, source_value)) { ok = false; break; diff --git a/runtime/src/builtin_regexp.c b/runtime/src/builtin_regexp.c index c213c6c4d..8362c7acf 100644 --- a/runtime/src/builtin_regexp.c +++ b/runtime/src/builtin_regexp.c @@ -1200,7 +1200,7 @@ static MalValue regexp_constructor(MalVm *vm, MalValue this_value, const MalValu // RegExp(re) with no flags and re.constructor === RegExp returns re. if (pattern_is_regexp && mal_value_is_undefined(flags)) { MalValue pattern_constructor; - if (!mal_vm_get_property(vm, pattern, mal_intrinsic_string_key(vm, (const byte *) "constructor"), &pattern_constructor)) { + if (!mal_vm_get_property(vm, pattern, mal_intrinsic_hot_string_key(vm, MAL_HOT_KEY_CONSTRUCTOR), &pattern_constructor)) { return mal_value_new_undefined(); } if (pattern_constructor == used_new_target) { @@ -1661,7 +1661,7 @@ static bool regexp_same_value(MalValue a, MalValue b) { static MalValue regexp_species_constructor(MalVm *vm, MalValue rx, bool *ok) { *ok = true; MalValue constructor; - if (!mal_vm_get_property(vm, rx, mal_intrinsic_string_key(vm, (const byte *) "constructor"), &constructor)) { + if (!mal_vm_get_property(vm, rx, mal_intrinsic_hot_string_key(vm, MAL_HOT_KEY_CONSTRUCTOR), &constructor)) { *ok = false; return mal_value_new_undefined(); } diff --git a/runtime/src/builtin_typed_array.c b/runtime/src/builtin_typed_array.c index b3390a489..c9acc88f9 100644 --- a/runtime/src/builtin_typed_array.c +++ b/runtime/src/builtin_typed_array.c @@ -62,7 +62,7 @@ static MalValue mal_ta_create_impl( byte_length, byte_length, false, false) : mal_array_buffer_object_new_uninitialized( &vm->heap, buffer_prototype, - byte_length, byte_length, false, false); + byte_length, byte_length, false, false, false); MalTypedArrayObject *array = mal_typed_array_object_new(&vm->heap, mal_ta_kind_prototype(vm, kind), buffer, kind, 0, length, false); return mal_value_from_typed_array_object(array); } diff --git a/runtime/src/function_object.c b/runtime/src/function_object.c index df253936c..bdc37e380 100644 --- a/runtime/src/function_object.c +++ b/runtime/src/function_object.c @@ -90,11 +90,44 @@ static void mal_native_function_init_state( #endif function->slots = nullptr; function->slot_count = 0; + function->slots_inline = false; #if MAL_REALMS function->realm = heap->current_realm; #endif } +// Larger slot sets keep the cell in the small size classes and use a RAW vector. +#define MAL_NATIVE_FUNCTION_INLINE_SLOTS_MAX 16 + +static MalNativeFunctionObject *mal_native_function_alloc( + MalHeap *heap, u32 metadata_count, i32 slot_count +) { + u32 inline_slots = slot_count > 0 && slot_count <= MAL_NATIVE_FUNCTION_INLINE_SLOTS_MAX + ? (u32) slot_count : 0; + return mal_heap_alloc( + heap, sizeof(MalNativeFunctionObject) + (metadata_count + inline_slots) * sizeof(MalValue), + MAL_HEAP_NATIVE_FUNCTION_OBJECT); +} + +static void mal_native_function_attach_slots( + MalHeap *heap, MalNativeFunctionObject *function, u32 metadata_count, + const MalValue *slots, i32 slot_count +) { + if (slot_count <= 0) return; + if (slot_count <= MAL_NATIVE_FUNCTION_INLINE_SLOTS_MAX) { + function->slots = (MalValue *) (function + 1) + metadata_count; + function->slots_inline = true; + } else { + function->slots = mal_heap_alloc_raw_profiled( + heap, sizeof(MalValue) * slot_count, MAL_PROFILE_ALLOCATION_FAMILY_FUNCTION); + mal_heap_cell_needs_finalizer(&function->object.header); + } + function->slot_count = slot_count; + for (i32 i = 0; i < slot_count; i++) { + function->slots[i] = slots[i]; + } +} + static void mal_function_init_state( MalHeap *heap, MalFunctionObject *function, @@ -219,19 +252,11 @@ MalNativeFunctionObject *mal_native_function_object_new_with_slots_arity( const MalValue *slots, i32 slot_count ) { - MalNativeFunctionObject *function = - mal_native_function_object_new_arity(heap, prototype, name, length, callback); - if (slot_count > 0) { - function->slots = mal_heap_alloc_raw_profiled( - heap, sizeof(MalValue) * slot_count, - MAL_PROFILE_ALLOCATION_FAMILY_FUNCTION); - function->slot_count = slot_count; - for (i32 i = 0; i < slot_count; i++) { - function->slots[i] = slots[i]; - } - } - - return function; + MalKey length_key; + MalKey name_key; + mal_native_function_metadata_keys(heap, name != nullptr, &length_key, &name_key); + return mal_native_function_object_new_with_slots_arity_keys( + heap, prototype, name, length, callback, slots, slot_count, length_key, name_key); } MalNativeFunctionObject *mal_native_function_object_new_with_slots_arity_keys( @@ -246,22 +271,12 @@ MalNativeFunctionObject *mal_native_function_object_new_with_slots_arity_keys( MalKey name_key ) { u32 metadata_count = name == nullptr ? 1 : 2; - MalNativeFunctionObject *function = - mal_heap_alloc(heap, sizeof(MalNativeFunctionObject) + metadata_count * sizeof(MalValue), - MAL_HEAP_NATIVE_FUNCTION_OBJECT); + MalNativeFunctionObject *function = mal_native_function_alloc(heap, metadata_count, slot_count); mal_native_function_init_state(heap, function, prototype, name, length, callback); mal_function_init_metadata( heap, &function->object, length_key, mal_value_from_i32(length), name_key, name, (MalValue *) (function + 1)); - if (slot_count > 0) { - function->slots = mal_heap_alloc_raw_profiled( - heap, sizeof(MalValue) * slot_count, - MAL_PROFILE_ALLOCATION_FAMILY_FUNCTION); - function->slot_count = slot_count; - for (i32 i = 0; i < slot_count; i++) { - function->slots[i] = slots[i]; - } - } + mal_native_function_attach_slots(heap, function, metadata_count, slots, slot_count); return function; } diff --git a/runtime/src/function_object.h b/runtime/src/function_object.h index a80005191..099e65e56 100644 --- a/runtime/src/function_object.h +++ b/runtime/src/function_object.h @@ -89,13 +89,15 @@ typedef struct MalNativeFunctionObject { #endif /** - * Internal slots (the spec's [[...]] closure state). Heap-owned, nullptr - * when the function carries none. Created via - * mal_native_function_object_new_with_slots; a pair of functions can share - * one mutable cell by storing the same heap object in a slot. + * Internal slots (the spec's [[...]] closure state), nullptr when the function + * carries none. Created via mal_native_function_object_new_with_slots; a pair of + * functions can share one mutable cell by storing the same heap object in a slot. + * A small set lives in the function's own cell (`slots_inline`), so creating and + * sweeping such a function needs no separate allocation. */ MalValue *slots; i32 slot_count; + bool slots_inline; #if MAL_REALMS /** diff --git a/runtime/src/gc.c b/runtime/src/gc.c index 7100f6e27..9d9e018b0 100644 --- a/runtime/src/gc.c +++ b/runtime/src/gc.c @@ -2335,10 +2335,10 @@ static __attribute__((noinline)) void mal_gc_finalize_cell_owned(MalHeapHeader * } case MAL_HEAP_NATIVE_FUNCTION_OBJECT: { MalNativeFunctionObject *fn = (MalNativeFunctionObject *) cell; - if (fn->slots != nullptr) { + if (fn->slots != nullptr && !fn->slots_inline) { gc_free_raw(&g_gc_vm->heap, fn->slots); - fn->slots = nullptr; } + fn->slots = nullptr; break; } case MAL_HEAP_REGEXP_OBJECT: { diff --git a/runtime/src/heap.c b/runtime/src/heap.c index 0ab9d44c9..052b3a60d 100644 --- a/runtime/src/heap.c +++ b/runtime/src/heap.c @@ -12,7 +12,9 @@ #include "./gc.h" #include "./gc_process.h" +#include "./array_buffer_object.h" #include "./array_object.h" +#include "./function_object.h" #include "./heap_string.h" #include "./iterator_object.h" #include "./object.h" @@ -403,6 +405,16 @@ static bool mal_heap_dead_cell_finalizes(const MalHeap *heap, const MalHeapHeade const MalIteratorObject *iterator = (const MalIteratorObject *) cell; return iterator->collection_pinned || mal_object_finalizes(&iterator->object); } + case MAL_HEAP_NATIVE_FUNCTION_OBJECT: { + const MalNativeFunctionObject *function = (const MalNativeFunctionObject *) cell; + return (function->slots != nullptr && !function->slots_inline) || + mal_object_finalizes(&function->object); + } + case MAL_HEAP_ARRAY_BUFFER_OBJECT: { + const MalArrayBufferObject *buffer = (const MalArrayBufferObject *) cell; + return mal_array_buffer_object_owns_storage(buffer) || + mal_object_finalizes(&buffer->object); + } default: return false; } diff --git a/runtime/src/heap.h b/runtime/src/heap.h index 893a06ac8..df119c22c 100644 --- a/runtime/src/heap.h +++ b/runtime/src/heap.h @@ -773,8 +773,9 @@ static inline u8 mal_heap_mark_load(const MalHeapHeader *header) { /** * Whether a dead cell of this type may own storage outside its cell. Ordinary objects, - * arrays, strings and iterators start owning nothing and report it through - * mal_heap_cell_needs_finalizer; the other non-owning types never do. Mirrors the + * arrays, ArrayBuffers, native functions, strings and iterators start owning nothing + * and report it through mal_heap_cell_needs_finalizer; the other non-owning types never + * do. Mirrors the * collector's finalizer, so a type that gains owned state there must either return * true here or report it. */ @@ -794,6 +795,8 @@ static inline bool mal_heap_type_finalizes_at_birth(MalHeapType type) { case MAL_HEAP_ASYNC_RUN_SCOPE_STATE: case MAL_HEAP_OBJECT: case MAL_HEAP_FUNCTION_OBJECT: + case MAL_HEAP_NATIVE_FUNCTION_OBJECT: + case MAL_HEAP_ARRAY_BUFFER_OBJECT: #if !MAL_PERF_STATS // Stats builds count every Array finalization. case MAL_HEAP_ARRAY_OBJECT: @@ -852,6 +855,14 @@ static inline u32 mal_heap_class_cell_size(u32 size_class) { static inline __attribute__((always_inline)) void mal_heap_clear_cell(u8 *cell, u32 size) { switch (size) { + case 256: mal_heap_clear_16(cell + 240); [[fallthrough]]; + case 240: mal_heap_clear_16(cell + 224); [[fallthrough]]; + case 224: mal_heap_clear_16(cell + 208); [[fallthrough]]; + case 208: mal_heap_clear_16(cell + 192); [[fallthrough]]; + case 192: mal_heap_clear_16(cell + 176); [[fallthrough]]; + case 176: mal_heap_clear_16(cell + 160); [[fallthrough]]; + case 160: mal_heap_clear_16(cell + 144); [[fallthrough]]; + case 144: mal_heap_clear_16(cell + 128); [[fallthrough]]; case 128: mal_heap_clear_16(cell + 112); [[fallthrough]]; case 112: mal_heap_clear_16(cell + 96); [[fallthrough]]; case 96: mal_heap_clear_16(cell + 80); [[fallthrough]]; diff --git a/runtime/src/object.c b/runtime/src/object.c index 58eff179a..ef581536b 100644 --- a/runtime/src/object.c +++ b/runtime/src/object.c @@ -183,6 +183,19 @@ void mal_object_invalidate_prototype_dependents(MalObject *object) { } } +// Links every object from `first` through `holder`, or the whole chain without a holder. +static bool mal_prototype_dependency_link_chain(MalObject *first, MalObject *holder, void *cache) { + for (MalObject *cursor = first; cursor != nullptr; cursor = mal_object_prototype(cursor)) { + MalPrototypeCacheDependency *dependency = mal_prototype_dependency_alloc(); + MAL_PERF_COUNT(prototype_dependency_register_nodes); + mal_prototype_dependency_link(dependency, cursor, cache); + if (cursor == holder) { + return true; + } + } + return holder == nullptr; +} + bool mal_object_register_prototype_cache( MalObject *receiver, MalObject *holder, void *cache, bool include_receiver @@ -190,16 +203,18 @@ bool mal_object_register_prototype_cache( MAL_PERF_COUNT(prototype_dependency_register_calls); mal_object_unregister_prototype_cache(cache); if (include_receiver) mal_object_mark_as_prototype(receiver); - for (MalObject *cursor = include_receiver ? receiver : mal_object_prototype(receiver); - cursor != nullptr; cursor = mal_object_prototype(cursor)) { - MalPrototypeCacheDependency *dependency = mal_prototype_dependency_alloc(); - MAL_PERF_COUNT(prototype_dependency_register_nodes); - mal_prototype_dependency_link(dependency, cursor, cache); - if (cursor == holder) { - return true; - } + if (mal_prototype_dependency_link_chain( + include_receiver ? receiver : mal_object_prototype(receiver), holder, cache)) { + return true; } - if (holder == nullptr) { + MAL_PERF_COUNT(prototype_dependency_register_failures); + mal_object_unregister_prototype_cache(cache); + return false; +} + +bool mal_object_extend_prototype_cache(MalObject *receiver, MalObject *holder, void *cache) { + MAL_PERF_COUNT(prototype_dependency_register_calls); + if (mal_prototype_dependency_link_chain(mal_object_prototype(receiver), holder, cache)) { return true; } MAL_PERF_COUNT(prototype_dependency_register_failures); diff --git a/runtime/src/object.h b/runtime/src/object.h index 2ae25af8e..27cd959a0 100644 --- a/runtime/src/object.h +++ b/runtime/src/object.h @@ -277,6 +277,10 @@ bool mal_object_register_prototype_cache( MalObject *receiver, MalObject *holder, void *cache, bool include_receiver); +/** Add a further receiver's chain through `holder` to a registered row's dependencies. + * On failure every dependency of the row is gone, so the caller must clear it. */ +bool mal_object_extend_prototype_cache(MalObject *receiver, MalObject *holder, void *cache); + bool mal_object_register_constructor_layout_cache( MalObject *constructor, MalObject *prototype, void *cache); void mal_object_unregister_prototype_cache(void *cache); diff --git a/runtime/src/runtime/mal_ffi.c b/runtime/src/runtime/mal_ffi.c index 45f9d5f6d..8d3a2119c 100644 --- a/runtime/src/runtime/mal_ffi.c +++ b/runtime/src/runtime/mal_ffi.c @@ -369,6 +369,21 @@ void mal_ffi_texts_free(MalFfiTexts *texts) { texts->blocks = nullptr; } +/* A pinned store never lives in its buffer's cell: C may keep the address for as long + * as the pin lasts, and a later lease or transfer must not move the bytes under it. */ +static inline bool ffi_pinnable_store(MalVm *vm, MalValue value) { + MalArrayBufferObject *buffer = nullptr; + if (mal_value_is_typed_array_object(value)) { + buffer = mal_value_to_typed_array_object(value)->buffer; + } else if (mal_value_is_array_buffer_object(value)) { + buffer = mal_value_to_array_buffer_object(value); + } else if (mal_value_is_data_view_object(value)) { + buffer = mal_value_to_data_view_object(value)->buffer; + } + return buffer == nullptr || mal_array_buffer_object_externalize(&vm->heap, buffer) + || mal_ffi_out_of_memory(vm); +} + __attribute__((always_inline)) bool mal_ffi_to_bytes( MalVm *vm, MalValue value, bool nullable, bool writable, const MalFfiSite *site, void **data, size_t *length, void **pin) { @@ -378,6 +393,7 @@ __attribute__((always_inline)) bool mal_ffi_to_bytes( *length = 0; return true; } + if (pin != nullptr && !ffi_pinnable_store(vm, value)) return false; MalTypedArraySpan view; if (mal_value_is_typed_array_object(value) && mal_typed_array_object_fixed_span(mal_value_to_typed_array_object(value), &view)) { @@ -432,6 +448,7 @@ __attribute__((always_inline)) bool mal_ffi_to_numeric_view( if (!mal_value_is_typed_array_object(value) || (u32) expected >= sizeof(kinds) / sizeof(*kinds)) return mal_ffi_expected(vm, site, "a matching numeric typed array"); MalTypedArrayObject *array = mal_value_to_typed_array_object(value); + if (pin != nullptr && !ffi_pinnable_store(vm, value)) return false; MalTypedArraySpan span; if (!(mal_typed_array_object_fixed_span(array, &span) || mal_typed_array_object_span(array, &span)) || span.shared || span.kind != kinds[expected] @@ -774,7 +791,7 @@ MalValue mal_ffi_from_bytes(MalVm *vm, const void *data, size_t length) { // The copy covers every visible byte before the buffer can be observed. MalArrayBufferObject *buffer = mal_array_buffer_object_new_uninitialized( &vm->heap, mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]), - (u32) length, (u32) length, false, false); + (u32) length, (u32) length, false, false, false); if (length > 0) memcpy(buffer->data, data, length); MalValue buffer_value = mal_value_from_array_buffer_object(buffer); MalRootSpan root; @@ -1875,7 +1892,7 @@ MalValue mal_ffi_new_callable( mal_gc_root(&root, slots, 2); MalArrayBufferObject *buffer = mal_array_buffer_object_new_uninitialized( &vm->heap, mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]), - (u32) target_size, (u32) target_size, false, false); + (u32) target_size, (u32) target_size, false, false, false); memcpy(buffer->data, target, target_size); slots[0] = mal_value_from_array_buffer_object(buffer); MalNativeFunctionObject *callable = mal_native_function_object_new_with_slots_arity( @@ -3209,7 +3226,7 @@ bool mal_ffi_close_prepare( MalFfiRetainedField *field = &handle->retained[index]; if (field->pin == nullptr) continue; MalArrayBufferStoreLease *lease = mal_array_buffer_object_retain_store( - mal_value_to_array_buffer_object(field->buffer)); + &vm->heap, mal_value_to_array_buffer_object(field->buffer)); if (lease == nullptr) { ffi_close_free(close); return mal_ffi_out_of_memory(vm); @@ -4568,7 +4585,7 @@ static bool ffi_prepare_handle( MalFfiRetainedField *field = &handle->retained[index]; if (field->pin == nullptr) continue; MalArrayBufferStoreLease *lease = mal_array_buffer_object_retain_store( - mal_value_to_array_buffer_object(field->buffer)); + &vm->heap, mal_value_to_array_buffer_object(field->buffer)); if (lease == nullptr) { for (i32 previous = 0; previous < count; previous++) mal_array_buffer_store_release(stores[previous]); @@ -4823,7 +4840,9 @@ bool mal_ffi_set_state_value( &vm->heap, mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]), capacity, capacity, false, false); roots[2] = mal_value_from_array_buffer_object(buffer); - if (buffer->data == nullptr) { + // Pinning can move the store out of the cell, so its address is read after. + next.pin = buffer->data == nullptr ? nullptr : mal_array_buffer_object_retain_pin(&vm->heap, buffer); + if (next.pin == nullptr) { mal_ffi_out_of_memory(vm); goto mal_done; } @@ -4831,21 +4850,18 @@ bool mal_ffi_set_state_value( next.data = buffer->data; next.length = text.length; next.buffer = roots[2]; - next.pin = mal_array_buffer_object_retain_pin(buffer); - if (next.pin == nullptr) { - mal_ffi_out_of_memory(vm); - goto mal_done; - } } else if (!mal_value_is_null(input)) { MalBufferSourceSpan span; - if (!mal_ffi_to_bytes(vm, input, false, field->writable, site, &next.data, &next.length, nullptr) - || !mal_ffi_check_count(vm, next.length, field->count_bits, field->count_signed, site)) goto mal_done; - if (mal_buffer_source_span(input, &span) != MAL_BUFFER_SOURCE_SPAN_OK) goto mal_done; - next.pin = mal_array_buffer_object_retain_pin(span.buffer); - if (next.pin == nullptr) { - mal_ffi_out_of_memory(vm); - goto mal_done; + if (mal_buffer_source_span(input, &span) == MAL_BUFFER_SOURCE_SPAN_OK && !span.shared) { + next.pin = mal_array_buffer_object_retain_pin(&vm->heap, span.buffer); + if (next.pin == nullptr) { + mal_ffi_out_of_memory(vm); + goto mal_done; + } } + if (!mal_ffi_to_bytes(vm, input, false, field->writable, site, &next.data, &next.length, nullptr) + || !mal_ffi_check_count(vm, next.length, field->count_bits, field->count_signed, site) + || next.pin == nullptr) goto mal_done; next.buffer = mal_value_from_array_buffer_object(span.buffer); } MalFfiRetainedField *range = &handle->retained[index]; @@ -4858,6 +4874,7 @@ bool mal_ffi_set_state_value( if (handle->native != nullptr) ffi_native_handle_stores_clear(handle->native); ok = true; mal_done: + if (!ok) mal_array_buffer_object_release_pin(next.pin); mal_ffi_string_release(&text); mal_gc_unroot(&root); return ok; diff --git a/runtime/src/runtime/node_buffer.c b/runtime/src/runtime/node_buffer.c index ca025208c..ece146c40 100644 --- a/runtime/src/runtime/node_buffer.c +++ b/runtime/src/runtime/node_buffer.c @@ -528,7 +528,7 @@ static MalValue mal_buffer_new_impl( length, length, false, false) : mal_array_buffer_object_new_uninitialized( &vm->heap, backing_prototype, - length, length, false, false); + length, length, false, false, false); MalValue backing = mal_value_from_array_buffer_object(buffer); MalRootSpan root; mal_gc_root(&root, &backing, 1); diff --git a/runtime/src/serialize.c b/runtime/src/serialize.c index 51e9d4868..02de71968 100644 --- a/runtime/src/serialize.c +++ b/runtime/src/serialize.c @@ -594,6 +594,11 @@ static bool enc_validate_transfers(SerEncoder *enc, MalValue list_value) { mal_array_buffer_object_is_pinned(buffer)) { return enc_fail(enc, "ArrayBuffer cannot be transferred"); } + // Commit hands the store itself to the receiver, and an inline store + // cannot outlive the sender's cell. + if (!mal_array_buffer_object_externalize(&enc->vm->heap, buffer)) { + return enc_fail(enc, "out of memory while serializing"); + } snap->transfer_sources[i] = buffer; } else if (enc->hooks == nullptr || enc->hooks->encode == nullptr) { return enc_fail(enc, "transfer list item is not transferable"); @@ -921,7 +926,7 @@ static MalArrayBufferObject *dec_buffer(SerDecoder *dec, SerResource *resource) if (dec->take && max_length <= resource->capacity) { // The backing already carries its process charge; only the empty wrapper is new. MalArrayBufferObject *buffer = mal_array_buffer_object_new_uninitialized( - &vm->heap, prototype, 0, 0, false, false); + &vm->heap, prototype, 0, 0, false, false, false); buffer->data = resource->data; buffer->store_lease = resource->store_lease; resource->store_lease = nullptr; @@ -937,14 +942,14 @@ static MalArrayBufferObject *dec_buffer(SerDecoder *dec, SerResource *resource) return buffer; } MalArrayBufferObject *buffer = mal_array_buffer_object_new_uninitialized( - &vm->heap, prototype, resource->length, max_length, resource->resizable, false); + &vm->heap, prototype, resource->length, max_length, resource->resizable, false, + resource->sensitive); if (max_length > 0 && buffer->data == nullptr) { return nullptr; } if (resource->length > 0) { memcpy(buffer->data, resource->data, resource->length); } - buffer->sensitive = resource->sensitive; return buffer; } diff --git a/runtime/src/vm_ops.c b/runtime/src/vm_ops.c index 09b565ed4..c15cf0307 100644 --- a/runtime/src/vm_ops.c +++ b/runtime/src/vm_ops.c @@ -6616,6 +6616,31 @@ static bool mal_ic_try_record_missing( return true; } +/** + * Admit a second receiver layout into an accessor row whose holder and accessor it + * shares. Its chain joins the row's dependencies, so a mutation along either chain still + * clears the whole row. + */ +static bool mal_ic_try_extend_inherited_accessor( + MalObject *object, MalValue key_value, MalObject *holder, MalValue accessor, u8 mode, + MalInlineCache *ic +) { + if (ic->mode != mode || ic->poly_count != 1 || ic->key != key_value || + ic->proto_object[1] != holder || ic->value != accessor || + object->header.type != ic->receiver_type || + (object->shape == ic->shape && mal_object_prototype(object) == ic->proto_object[0])) { + return false; + } + if (!mal_object_extend_prototype_cache(object, holder, ic)) { + mal_vm_property_cache_invalidate(ic); + return false; + } + ic->entry = (void *) object->shape; + ic->proto_object[2] = mal_object_prototype(object); + ic->poly_count = 2; + return true; +} + /** * Record an ordinary-chain accessor's getter. The chain registration clears the row * on any mutation of an object between the receiver and the holder, so a hit only @@ -6649,6 +6674,11 @@ static bool mal_ic_try_record_inherited_getter( own.desc.getter != resolution.desc.getter) { return false; } + if (mal_ic_try_extend_inherited_accessor( + object, key_value, resolution.holder, resolution.desc.getter, + MAL_IC_MODE_INHERITED_GETTER, ic)) { + return true; + } if (!mal_ic_record_local_prototype_chain(object, resolution.holder, ic, false)) { return false; } @@ -6697,6 +6727,11 @@ static bool mal_ic_try_record_inherited_setter( own.desc.setter != resolution.desc.setter) { return false; } + if (mal_ic_try_extend_inherited_accessor( + object, key_value, resolution.holder, resolution.desc.setter, + MAL_IC_MODE_INHERITED_SETTER, ic)) { + return true; + } if (!mal_ic_record_local_prototype_chain(object, resolution.holder, ic, false)) { return false; } @@ -7470,9 +7505,8 @@ static void mal_vm_op_store_object_property_ic( MalValue value, bool strict, MalInlineCache *ic ) { MalObject *object = (MalObject *) mal_value_to_heap(object_value); - if (ic->mode == MAL_IC_MODE_INHERITED_SETTER && key_value == ic->key && - object->shape == ic->shape && mal_object_prototype(object) == ic->proto_object[0] && - !mal_object_has_public_overflow(object)) { + if (ic->mode == MAL_IC_MODE_INHERITED_SETTER && + mal_vm_inherited_accessor_row_holds(object_value, key_value, ic)) { MAL_PERF_COUNT(ic_store_inherited_setter_hits); MalCompletion completion = mal_vm_call_value(vm, ic->value, object_value, &value, 1); if (completion.kind != MAL_COMPLETION_NORMAL) vm->completion = completion; @@ -9329,6 +9363,11 @@ static bool mal_vm_try_merge_plain_data_properties(MalObject *target, MalValue s return true; } +bool mal_vm_try_merge_data_properties(MalVm *vm, MalValue target_value, MalValue source) { + return mal_vm_try_merge_shaped_data_properties(vm, target_value, source) || + mal_vm_try_merge_plain_data_properties(mal_value_to_object(target_value), source); +} + void mal_vm_op_merge_data_properties(MalVm *vm, MalValue target_value, MalValue source) { // Spreading null/undefined contributes nothing. if (mal_value_is_nil(source)) { diff --git a/runtime/src/vm_ops.h b/runtime/src/vm_ops.h index 9c81ee100..2ed4f7c4b 100644 --- a/runtime/src/vm_ops.h +++ b/runtime/src/vm_ops.h @@ -3879,6 +3879,12 @@ void mal_vm_op_define_accessor( /** Object spread `{...source}` into target; a throwing getter sets completion. */ void mal_vm_op_merge_data_properties(MalVm *vm, MalValue target_value, MalValue source); +/** + * Copy an ordinary source's own enumerable data properties onto an ordinary target with + * CreateDataProperty semantics when its properties are all string-keyed data in its + * shape. False, with nothing copied, for any other source. + */ +bool mal_vm_try_merge_data_properties(MalVm *vm, MalValue target_value, MalValue source); /** Set [[Prototype]] for an object-literal `__proto__:` member or class heritage. */ void mal_vm_op_set_prototype(MalVm *vm, MalValue object_value, MalValue prototype_value, bool literal); @@ -4166,13 +4172,24 @@ static inline __attribute__((always_inline)) bool mal_vm_probe_named_read( return found; } -/** Whether an ordinary receiver still resolves `key` to the row's inherited accessor. */ /** Receivers whose own properties and prototype follow ordinary object semantics. */ static inline bool mal_vm_accessor_row_receiver(MalValue value) { return mal_value_is_heap_type(value, MAL_HEAP_OBJECT) || mal_value_is_heap_type(value, MAL_HEAP_FFI_HANDLE_OBJECT); } +/** An inherited accessor row's second receiver shape, kept where those rows store no + * other pointer; its first prototype is `proto_object[2]`. */ +static inline const struct MalShape *mal_ic_accessor_alternate_shape(const MalInlineCache *ic) { + return (const struct MalShape *) ic->entry; +} + +/** + * Whether an ordinary receiver still resolves `key` to the row's inherited accessor. A + * row holds two receiver layouts (shape and first prototype) that reach the same + * holder's accessor, such as instances of a class and of a subclass, so a site that + * alternates between them keeps hitting. + */ static inline bool mal_vm_inherited_accessor_row_holds( MalValue object_value, MalValue key_value, const MalInlineCache *ic ) { @@ -4181,8 +4198,11 @@ static inline bool mal_vm_inherited_accessor_row_holds( return false; } const MalObject *object = mal_value_to_object(object_value); - return object->shape == ic->shape && mal_object_prototype(object) == ic->proto_object[0] && - !mal_object_has_public_overflow(object); + if (mal_object_has_public_overflow(object)) return false; + const MalObject *prototype = mal_object_prototype(object); + return (object->shape == ic->shape && prototype == ic->proto_object[0]) || + (ic->poly_count == 2 && object->shape == mal_ic_accessor_alternate_shape(ic) && + prototype == ic->proto_object[2]); } bool mal_vm_probe_accessor_read_miss( @@ -4614,3 +4634,31 @@ static inline __attribute__((always_inline)) void mal_numeric_leaf_merge_sort( for (u32 index = 0; index < count; index++) keys[index] = mal_ops_number_as_f64(values[index]); MAL_LEAF_MERGE_SORT_BODY(f64, compare, keys); } + +/** Below this many elements the merge beats the radix passes' fixed cost. */ +#define MAL_NUMERIC_RADIX_SORT_MIN 128u + +/** + * Stably order `values` by their Number `keys`, ascending or descending, staging the + * result in `scratch`. False, with nothing moved, when a key is NaN or the pass + * storage cannot be allocated. + */ +bool mal_numeric_radix_sort( + MalValue *values, MalValue *scratch, const f64 *keys, u32 count, bool descending); + +/** + * Sort kernel for a leaf that returns `a - b` (or `b - a`) of its two Numbers. Without + * NaN keys that comparator is a consistent total preorder in which only equal Numbers + * tie, so the stable radix order is the order the merge would produce. + */ +static inline __attribute__((always_inline)) void mal_numeric_difference_sort( + MalValue *values, MalValue *scratch, f64 *keys, u32 count, bool descending, + f64 (*compare)(f64 left, f64 right) +) { + for (u32 index = 0; index < count; index++) keys[index] = mal_ops_number_as_f64(values[index]); + if (count >= MAL_NUMERIC_RADIX_SORT_MIN && + mal_numeric_radix_sort(values, scratch, keys, count, descending)) { + return; + } + MAL_LEAF_MERGE_SORT_BODY(f64, compare, keys); +} diff --git a/src/compiler/core/core-array-iteration.ts b/src/compiler/core/core-array-iteration.ts index 4ed0f3bc3..743ddf552 100644 --- a/src/compiler/core/core-array-iteration.ts +++ b/src/compiler/core/core-array-iteration.ts @@ -220,10 +220,17 @@ export function expandCoreArrayIterationCall( }).outputs[0]!; const zero = append(fast, "createNumber", [], { value: 0 }).outputs[0]!; const one = append(fast, "createNumber", [], { value: 1 }).outputs[0]!; + // With the default species, map's ArraySpeciesCreate(O, len) is ArrayCreate(len): the + // result has its final length before the first define, and the runtime reserves it. const created = - name === "map" || filter - ? append(fast, "createArray", [], { length: 0 }).outputs[0]! - : undefined; + name === "map" + ? append(fast, "construct", [ + append(fast, "loadIntrinsic", [], { intrinsic: "Array" }).outputs[0]!, + length, + ]).outputs[0]! + : filter + ? append(fast, "createArray", [], { length: 0 }).outputs[0]! + : undefined; if (reduce) instructionsIntroduced++; // The accumulator also carries boxed callback results, so an unboxed seed boxes first. const seed = reduce @@ -324,12 +331,6 @@ export function expandCoreArrayIterationCall( jump(normalExit, join, [undefinedValue]); break; case "map": - // Trailing holes leave the defines short of ArraySpeciesCreate's length. - append(normalExit, "storePropertyStatic", [created!, length], { - stringIndex: lengthKey, - }); - jump(normalExit, join, [created!]); - break; case "filter": jump(normalExit, join, [created!]); break; diff --git a/src/compiler/core/semantic-lowering.ts b/src/compiler/core/semantic-lowering.ts index 667bdfab6..95000dda5 100644 --- a/src/compiler/core/semantic-lowering.ts +++ b/src/compiler/core/semantic-lowering.ts @@ -522,6 +522,11 @@ interface CoreFrontendFunction { staticArgumentsFallbackRegister?: number; /** Incoming arguments as a packed rest view for in-range element reads. */ argumentsElementRegister?: number; + /** + * Locked primordials pin the builtin slice, so this function's `arguments` copies + * read the frame and its reads use `argumentsCopyAccesses`; no object exists. + */ + argumentsCopyPlan?: boolean; classContext?: SemanticClassContext; /** @@ -5709,18 +5714,27 @@ function compileFunctionParams( if (binding) mappedParameterBindings.set(i, binding); } } + if ( + argumentsBinding !== undefined && + program.facts.world.primordialPolicy === "locked" && + fn.semanticFile.argumentsCopyBindings.has(argumentsBinding) + ) + fn.argumentsCopyPlan = true; const materializesArguments = - argumentsBinding?.usageNodes.some( + fn.argumentsCopyPlan !== true && + (argumentsBinding?.usageNodes.some( (usage) => fn.semanticFile.staticArgumentsAccesses.get(usage) === undefined, - ) ?? false; + ) ?? + false); const lazilyMaterializesArguments = + fn.argumentsCopyPlan !== true && argumentsBinding !== undefined && fn.semanticFile.lazyArgumentsBindings.has(argumentsBinding); let hasStaticIndex = false; let hasElement = false; if (argumentsBinding && (!materializesArguments || lazilyMaterializesArguments)) { for (const usage of argumentsBinding.usageNodes) { - const access = fn.semanticFile.staticArgumentsAccesses.get(usage); + const access = staticArgumentsAccess(fn, usage); if (access?.kind === "index") hasStaticIndex = true; if (access?.kind === "element") hasElement = true; } @@ -5742,7 +5756,7 @@ function compileFunctionParams( let argumentCountRegister: number | undefined; const indexRegisters = new Map(); for (const usage of argumentsBinding.usageNodes) { - const access = fn.semanticFile.staticArgumentsAccesses.get(usage); + const access = staticArgumentsAccess(fn, usage); if (!access) continue; if (access.kind === "element") { if (argumentCountRegister === undefined) { @@ -13348,6 +13362,15 @@ function compileMemberExpression( return compileMemberLoad(program, fn, cursor, member); } +function staticArgumentsAccess( + fn: CoreFrontendFunction, + usage: ESTree.Node, +): StaticArgumentsAccess | undefined { + return fn.argumentsCopyPlan === true + ? fn.semanticFile.argumentsCopyAccesses.get(usage) + : fn.semanticFile.staticArgumentsAccesses.get(usage); +} + function compileStaticArgumentsMember( program: CoreFrontendContext, fn: CoreFrontendFunction, @@ -13356,8 +13379,7 @@ function compileStaticArgumentsMember( ): number | undefined { if (memberExpression.object.type !== "Identifier") return undefined; const usage = memberExpression.object; - const access: StaticArgumentsAccess | undefined = - fn.semanticFile.staticArgumentsAccesses.get(usage); + const access = staticArgumentsAccess(fn, usage); if (access?.member !== memberExpression) return undefined; if (access.kind === "element") return compileArgumentsElementRead(program, fn, cursor, usage, memberExpression); @@ -13372,7 +13394,10 @@ function compileStaticArgumentsMember( ) { return undefined; } - if (!fn.semanticFile.guardedArgumentsAccesses.has(usage)) { + if ( + fn.argumentsCopyPlan === true || + !fn.semanticFile.guardedArgumentsAccesses.has(usage) + ) { return compileDirectArgumentsRead( program, fn, @@ -15015,6 +15040,18 @@ function compileCall( cursor: CoreFrontendCursor, callExpression: ESTree.CallExpression, ): number { + const copyStart = fn.argumentsCopyPlan + ? fn.semanticFile.argumentsCopyCalls.get(callExpression) + : undefined; + if (copyStart !== undefined) { + const copy = nextCoreVariable(fn); + cursor.block.emitter.emit({ + type: "createRestArguments", + registers: [copy], + startIndex: copyStart, + }); + return copy; + } if (!fn.semanticFile.commonjs) { const required = tryCompileCreateRequireCall(program, fn, cursor, callExpression); if (required !== undefined) return required; diff --git a/src/compiler/frontend/analyze-module-graph.ts b/src/compiler/frontend/analyze-module-graph.ts index f2563d9c9..b74908902 100644 --- a/src/compiler/frontend/analyze-module-graph.ts +++ b/src/compiler/frontend/analyze-module-graph.ts @@ -59,6 +59,9 @@ function analyzeModuleRecord(record: ModuleRecord): SemanticFile { nodeToBinding: new Map(), withDynamicNodes: new Set(), staticArgumentsAccesses: new Map(), + argumentsCopyBindings: new Set(), + argumentsCopyAccesses: new Map(), + argumentsCopyCalls: new Map(), guardedArgumentsAccesses: new Set(), lazyArgumentsBindings: new Set(), hasDirectEval: new Set(), diff --git a/src/compiler/frontend/semantic-analysis.ts b/src/compiler/frontend/semantic-analysis.ts index 2b6c9a27e..b47c2e776 100644 --- a/src/compiler/frontend/semantic-analysis.ts +++ b/src/compiler/frontend/semantic-analysis.ts @@ -59,6 +59,18 @@ export interface SemanticFile { */ staticArgumentsAccesses: Map; + /** + * Bindings whose uses are all direct reads or `Array.prototype.slice.call(arguments[, k])` + * and `[].slice.call(arguments[, k])` copies. While primordials are locked, lowering + * builds each copy from the frame and never creates the object; otherwise the + * builtin receives the object and the classification above applies. + */ + argumentsCopyBindings: Set; + /** Reads of an `argumentsCopyBindings` binding, valid only under that plan. */ + argumentsCopyAccesses: Map; + /** Copy calls of an `argumentsCopyBindings` binding, with their start index. */ + argumentsCopyCalls: Map; + /** * Body reads of a lazy binding in `staticArgumentsAccesses`. They read the frame * only while the binding is still EMPTY: once a body use has materialized the @@ -273,6 +285,9 @@ export function analyzeSourceAndRunSemanticAnalysis( nodeToBinding: new Map(), withDynamicNodes: new Set(), staticArgumentsAccesses: new Map(), + argumentsCopyBindings: new Set(), + argumentsCopyAccesses: new Map(), + argumentsCopyCalls: new Map(), guardedArgumentsAccesses: new Set(), lazyArgumentsBindings: new Set(), hasDirectEval: new Set(), @@ -749,6 +764,69 @@ function usageIsInFunctionParameters( } } +/** + * The call and start index when this `arguments` reference is the receiver of + * `Array.prototype.slice.call(arguments[, k])` or `[].slice.call(arguments[, k])` with + * a non-negative integer literal `k`, outside any optional chain. + */ +function argumentsCopyCall( + file: SemanticFile, + identifier: ESTree.Identifier, + parents: Map, +): { call: ESTree.CallExpression; start: number } | undefined { + const call = parents.get(identifier); + if ( + call?.type !== "CallExpression" || + call.optional || + call.arguments[0] !== identifier || + call.arguments.length > 2 + ) + return undefined; + let start = 0; + const second = call.arguments[1]; + if (second !== undefined) { + if ( + second.type !== "Literal" || + typeof second.value !== "number" || + !Number.isInteger(second.value) || + second.value < 0 || + second.value > 0x7fff_ffff + ) + return undefined; + start = second.value; + } + const named = (node: ESTree.Node, name: string): node is ESTree.MemberExpression => + node.type === "MemberExpression" && + !node.computed && + !node.optional && + node.property.type === "Identifier" && + node.property.name === name; + const callee = call.callee as ESTree.Node; + if (!named(callee, "call") || !named(callee.object, "slice")) return undefined; + const owner = callee.object.object; + if (owner.type === "ArrayExpression") { + if (owner.elements.length !== 0) return undefined; + } else if ( + !named(owner, "prototype") || + owner.object.type !== "Identifier" || + owner.object.name !== "Array" || + file.withDynamicNodes.has(owner.object) || + file.nodeToBinding.get(owner.object)?.undeclared === undefined + ) { + return undefined; + } + for ( + let current: ESTree.Node | undefined = parents.get(call); + current?.type === "MemberExpression" || + current?.type === "CallExpression" || + current?.type === "ChainExpression"; + current = parents.get(current) + ) { + if (current.type === "ChainExpression") return undefined; + } + return { call, start }; +} + function canLazilyMaterializeArgumentsUse( file: SemanticFile, usage: ESTree.Node, @@ -773,9 +851,29 @@ function canLazilyMaterializeArgumentsUse( function classifyStaticArgumentsUsage(file: SemanticFile): void { const parents = new Map(); const candidates = new Map>(); + const copies = new Map< + Binding, + Map + >(); traverseEstree(file.ast, (node, { parent }) => { if (parent) parents.set(node, parent); + if (node.type === "Identifier") { + const binding = file.nodeToBinding.get(node); + const copy = + binding?.implicit === "arguments" && + !file.withDynamicNodes.has(node) && + accessIsInOwningFunction(file, node, binding) + ? argumentsCopyCall(file, node, parents) + : undefined; + if (binding && copy) { + const byNode = + copies.get(binding) ?? + new Map(); + byNode.set(node, copy); + copies.set(binding, byNode); + } + } if (node.type === "MemberExpression" && node.object.type === "Identifier") { const identifier = node.object; const binding = file.nodeToBinding.get(identifier); @@ -808,7 +906,10 @@ function classifyStaticArgumentsUsage(file: SemanticFile): void { } }); - for (const [binding, byNode] of candidates) { + for (const binding of new Set([...candidates.keys(), ...copies.keys()])) { + const byNode = + candidates.get(binding) ?? new Map(); + const bindingCopies = copies.get(binding); const isStaticRead = (usage: ESTree.Node): boolean => { const access = byNode.get(usage); return ( @@ -833,8 +934,23 @@ function classifyStaticArgumentsUsage(file: SemanticFile): void { file.staticArgumentsAccesses.set(usage, access); continue; } - const owner = argumentsBindingOwner(file, binding); + // A copy reads the object's current elements, which are the incoming values + // only while no formal it maps is ever written. + const isCopy = (usage: ESTree.Node): boolean => + bindingCopies?.has(usage) === true && + owner !== undefined && + argumentsMatchIncomingValues(file, owner, parents); + if ( + bindingCopies !== undefined && + binding.usageNodes.every((usage) => isStaticRead(usage) || isCopy(usage)) + ) { + file.argumentsCopyBindings.add(binding); + for (const [usage, access] of byNode) file.argumentsCopyAccesses.set(usage, access); + for (const copy of bindingCopies.values()) + file.argumentsCopyCalls.set(copy.call, copy.start); + } + if (!owner) continue; const parameterUsages = binding.usageNodes.filter((usage) => usageIsInFunctionParameters(usage, owner, parents), diff --git a/src/compiler/target/render-native-c.ts b/src/compiler/target/render-native-c.ts index 29744bf39..e275fb05d 100644 --- a/src/compiler/target/render-native-c.ts +++ b/src/compiler/target/render-native-c.ts @@ -5334,7 +5334,7 @@ function renderCompiledFunction( const source = `static __attribute__((aligned(64))) f64 ${symbol}(${parameters.join(", ") || "void"}) {\n#pragma STDC FP_CONTRACT OFF\n${worker.join("\n")}\n}\n${emitted.source.replace( "#pragma STDC FP_CONTRACT OFF\n", `#pragma STDC FP_CONTRACT OFF\n if (mal_vm_leaf_unobserved(vm) && mal_vm_leaf_stack_available(vm)) return ${symbol}(${args.join(", ")});\n`, - )}${sortSymbol === undefined ? "" : renderNumericSortKernel(sortSymbol, symbol, entry)}`; + )}${sortSymbol === undefined ? "" : renderNumericSortKernel(sortSymbol, symbol, entry, native)}`; return [ { entry, @@ -5457,14 +5457,52 @@ function numericSortRecordKeys(entry: NativeDirectEntryPlan): ReadonlyArray key))]; } +/** + * `ascending` for a leaf whose only work is returning its first Number parameter minus + * its second, `descending` for the reverse difference. + */ +function numericSortDifference( + native: NativeFunctionPlan, + entry: NativeDirectEntryPlan, +): "ascending" | "descending" | undefined { + const storage = entry.storage; + if (storage === undefined || native.body.parameterCount !== 2) return undefined; + const skipped = new Set([ + ...(storage.controlFlow?.unreachableIps ?? []), + ...storage.elidedPureIps, + ]); + const live = native.body.instructions.filter( + (op, ip) => !skipped.has(ip) && !(op.opcode === "JUMP" && op.targetIp === ip + 1), + ); + if (live.length !== 2) return undefined; + const [difference, result] = live; + if ( + difference?.opcode !== "BINARY" || + difference.operator !== "-" || + result?.opcode !== "RETURN" || + result.value !== difference.dst + ) + return undefined; + if (difference.left === 0 && difference.right === 1) return "ascending"; + if (difference.left === 1 && difference.right === 0) return "descending"; + return undefined; +} + function renderNumericSortKernel( symbol: string, leaf: string, entry: NativeDirectEntryPlan, + native: NativeFunctionPlan, ): string { const fields = entry.fieldParameters; - if (fields === undefined) - return `\n${numericSortKernelDeclaration(symbol)} {\n mal_numeric_leaf_merge_sort(values, scratch, keys, count, ${leaf});\n}\n`; + if (fields === undefined) { + const difference = numericSortDifference(native, entry); + const body = + difference === undefined + ? `mal_numeric_leaf_merge_sort(values, scratch, keys, count, ${leaf})` + : `mal_numeric_difference_sort(values, scratch, keys, count, ${difference === "descending"}, ${leaf})`; + return `\n${numericSortKernelDeclaration(symbol)} {\n ${body};\n}\n`; + } const keys = numericSortRecordKeys(entry); const type = `${symbol}_key`; const compare = `${symbol}_compare`; diff --git a/tests/fixtures/gc-current-usage/main.c b/tests/fixtures/gc-current-usage/main.c index b52acc3bd..174609d14 100644 --- a/tests/fixtures/gc-current-usage/main.c +++ b/tests/fixtures/gc-current-usage/main.c @@ -17,7 +17,8 @@ static usize walked_buffer_bytes; static void count_buffer(MalHeapHeader *header) { if (header->type != MAL_HEAP_ARRAY_BUFFER_OBJECT) return; MalArrayBufferObject *buffer = (MalArrayBufferObject *) header; - if (buffer->shared_memory == nullptr && buffer->data != nullptr) { + if (buffer->shared_memory == nullptr && buffer->data != nullptr && + !mal_array_buffer_object_store_is_inline(buffer)) { walked_buffer_bytes += buffer->allocation_capacity; } } diff --git a/tests/fixtures/utf8-string-storage/main.c b/tests/fixtures/utf8-string-storage/main.c index 059c1f276..7f6ab8a67 100644 --- a/tests/fixtures/utf8-string-storage/main.c +++ b/tests/fixtures/utf8-string-storage/main.c @@ -172,7 +172,7 @@ static bool rejects_unrepresentable_outputs_before_materialization(MalVm *vm) { // These methods must reject from the extent before observing any byte. MalArrayBufferObject *backing = mal_array_buffer_object_new_uninitialized( &vm->heap, mal_value_to_object(vm->intrinsics[MAL_INTRINSIC_ARRAY_BUFFER_PROTOTYPE]), - lengths[i], lengths[i], false, false); + lengths[i], lengths[i], false, false, false); CHECK(backing->data != nullptr); MalValue roots[] = {mal_value_from_array_buffer_object(backing), mal_value_new_undefined(), mal_value_new_undefined()}; MalRootSpan span; diff --git a/tests/local/arguments-slice-copy.cjs b/tests/local/arguments-slice-copy.cjs new file mode 100644 index 000000000..7635a430f --- /dev/null +++ b/tests/local/arguments-slice-copy.cjs @@ -0,0 +1,82 @@ +// Copies of `arguments` through the builtin slice, next to the cases that must still +// observe a real arguments object. + +const lines = []; + +function all() { + return Array.prototype.slice.call(arguments); +} +function tail() { + return [].slice.call(arguments, 1); +} +function pastTheEnd() { + return Array.prototype.slice.call(arguments, 5); +} +function strictCopy() { + "use strict"; + return Array.prototype.slice.call(arguments, 2); +} +lines.push( + JSON.stringify([ + all(1, "a", null), + all(), + tail(1, 2, 3), + tail(), + pastTheEnd(1, 2), + strictCopy(1, 2, 3, 4), + ]), +); + +// Indirect eval keeps these functions sloppy, so their arguments objects map the formals. +const writtenFormal = (0, eval)(`(function writtenFormal(a, b) { + a = 10; + return Array.prototype.slice.call(arguments); +})`); +const sloppyCopy = (0, eval)(`(function sloppyCopy(a, b) { + return [].slice.call(arguments, 1); +})`); +function strictWrittenFormal(a, b) { + "use strict"; + a = 10; + return Array.prototype.slice.call(arguments); +} +lines.push( + JSON.stringify([writtenFormal(1, 2), strictWrittenFormal(1, 2), sloppyCopy(1, 2, 3)]), +); + +function lengthAndCopy() { + return `${arguments.length} ${Array.prototype.slice.call(arguments).join("|")} ${arguments[0]}`; +} +lines.push(lengthAndCopy("x", "y")); + +function freshCopies() { + const first = [].slice.call(arguments); + const second = [].slice.call(arguments); + first.push("more"); + return `${first.length} ${second.length} ${first !== second} ${Array.isArray(first)}`; +} +lines.push(freshCopies(1, 2)); + +function shadowedArray() { + const Array = { prototype: { slice: { call: () => "shadowed" } } }; + return Array.prototype.slice.call(arguments); +} +lines.push(shadowedArray(1)); + +function arrowCopy() { + const inner = () => Array.prototype.slice.call(arguments); + return inner(9).join(","); +} +lines.push(arrowCopy(4, 5)); + +function optionalCopy() { + return Array.prototype.slice.call(arguments)?.length; +} +lines.push(String(optionalCopy(1, 2, 3))); + +let total = 0; +for (let round = 0; round < 2000; round++) + total += all(round, 1).length + tail(round, 2, 3).length; +lines.push(`total ${total}`); + +console.log(lines.join("\n")); diff --git a/tests/local/inherited-getter-cache.js b/tests/local/inherited-getter-cache.js index a954951b4..dc57b2d34 100644 --- a/tests/local/inherited-getter-cache.js +++ b/tests/local/inherited-getter-cache.js @@ -95,4 +95,52 @@ lines.push(`dictionary ${readDoubled(dictionaryChild)} ${readDoubled(dictionaryC Object.defineProperty(dictionaryPrototype, "doubled", { value: "dictionary data" }); lines.push(`dictionary redefined ${readDoubled(dictionaryChild)}`); +// One site alternating between a class and its subclass reaches the same inherited +// getter through two chains; a change on either chain reaches the site's next read. +class Account { + constructor(value) { + this.value = value; + } + get score() { + return this.value + 1; + } +} +class Premium extends Account {} +const accounts = [new Account(1), new Premium(2), new Account(3), new Premium(4)]; +function scoreRound() { + let scores = ""; + for (const account of accounts) scores += `${account.score},`; + return scores; +} +const chainChanges = [ + ["steady", () => {}], + [ + "subclass override", + () => + Object.defineProperty(Premium.prototype, "score", { + get() { + return this.value * 100; + }, + configurable: true, + }), + ], + ["subclass restored", () => delete Premium.prototype.score], + [ + "base redefined", + () => + Object.defineProperty(Account.prototype, "score", { + get() { + return -this.value; + }, + configurable: true, + }), + ], +]; +for (const [name, change] of chainChanges) { + change(); + let rounds = ""; + for (let round = 0; round < 20; round++) rounds = scoreRound(); + lines.push(`${name} ${rounds}`); +} + console.log(lines.join("\n")); diff --git a/tests/local/literal-object-assign.mjs b/tests/local/literal-object-assign.mjs index e8e73fd01..ce312773d 100644 --- a/tests/local/literal-object-assign.mjs +++ b/tests/local/literal-object-assign.mjs @@ -159,6 +159,36 @@ const scenarios = { Object.hasOwn(target, "__proto__"), ]; }, + plainSources() { + const first = { a: 1, b: 2 }; + const target = Object.assign({}, first, { b: 3, c: 4 }, null, { toString: 5 }); + first.a = 9; + return [ + Object.keys(target), + target.a, + target.b, + target.c, + Object.hasOwn(target, "toString"), + ]; + }, + hiddenTarget() { + const target = {}; + Object.defineProperty(target, "hidden", { + value: 1, + writable: true, + enumerable: false, + configurable: true, + }); + Object.assign(target, { hidden: 2, shown: 3 }); + return [Object.keys(target), target.hidden]; + }, + sharedPrototype() { + const base = { kept: 1 }; + const child = Object.create(base); + const before = child.added; + Object.assign(base, { added: 2 }); + return [before, child.added, child.kept]; + }, mutable() { Object.assign = (target, source) => [target.original, source.revision, "custom"]; return Object.assign({ original: 3 }, { revision: 11 }); diff --git a/tests/local/native-inherited-setters.js b/tests/local/native-inherited-setters.js index 8e67463d3..62093c75e 100644 --- a/tests/local/native-inherited-setters.js +++ b/tests/local/native-inherited-setters.js @@ -112,4 +112,47 @@ const frozen = Object.freeze( for (let i = 1; i <= 4; i++) frozen.value = i; expectValue(frozenCalls, 10, "an inherited setter runs for a frozen receiver"); +class Gauge { + #level = 0; + get level() { + return this.#level; + } + set level(next) { + this.#level = next; + } +} +class Dial extends Gauge {} +const gauges = [new Gauge(), new Dial(), new Gauge(), new Dial()]; +function levelRound(base) { + for (let index = 0; index < gauges.length; index++) gauges[index].level = base + index; +} +const overrides = []; +const levelChanges = [ + () => {}, + () => + Object.defineProperty(Dial.prototype, "level", { + get() { + return -1; + }, + set(next) { + overrides.push(next); + }, + configurable: true, + }), +]; +for (const [phase, change] of levelChanges.entries()) { + change(); + for (let round = 0; round < 10; round++) levelRound(phase * 100); +} +expectValue( + `${gauges[0].level},${gauges[2].level}`, + "100,102", + "one site stores through the base class setter", +); +expectValue( + overrides.slice(-2).join(","), + "101,103", + "a subclass override reaches the shared store site", +); + console.log("native-inherited-setters PASS"); diff --git a/tests/local/numeric-sort-callback.mjs b/tests/local/numeric-sort-callback.mjs index e42a97c4a..6f3d6da7f 100644 --- a/tests/local/numeric-sort-callback.mjs +++ b/tests/local/numeric-sort-callback.mjs @@ -287,3 +287,24 @@ try { } catch (error) { console.log("run-throw", error.message, runComparisons); } +// Past the size where `a - b` and `b - a` sorts take the radix order: equal Numbers, +// including -0 and 0, keep their original order in both directions. +const signedValues = [0, -0, 3, -3, 0.5, -0.5, Infinity, -Infinity, 2 ** 40, -(2 ** 40)]; +const wide = Array.from( + { length: 300 }, + (_, index) => signedValues[(index * 7) % signedValues.length], +); +const describeOrder = (values) => + values.map((value) => (Object.is(value, -0) ? "-0" : String(value))).join(","); +console.log( + "wide-ascending", + describeOrder(wide.slice().sort((left, right) => left - right)), +); +console.log( + "wide-descending", + describeOrder(wide.slice().sort((left, right) => right - left)), +); +console.log( + "wide-copy", + describeOrder(wide.toSorted((left, right) => left - right).slice(0, 40)), +); diff --git a/tests/local/process-memory-usage-fast.mjs b/tests/local/process-memory-usage-fast.mjs index 75eed1503..ee237f897 100644 --- a/tests/local/process-memory-usage-fast.mjs +++ b/tests/local/process-memory-usage-fast.mjs @@ -3,46 +3,52 @@ function check(value, message) { } const baseline = process.memoryUsage().arrayBuffers; +// Fixed stores up to 256 bytes live in the buffer's heap cell and count as heap +// memory, so every fixed length here stays above that. +const fixedLength = 384; function exercise() { - const source = new ArrayBuffer(64, { maxByteLength: 128 }); + const source = new ArrayBuffer(512, { maxByteLength: 1024 }); check( - process.memoryUsage().arrayBuffers === baseline + 128, + process.memoryUsage().arrayBuffers === baseline + 1024, "resizable buffer counts reserved capacity", ); source.resize(16); check( - process.memoryUsage().arrayBuffers === baseline + 128, + process.memoryUsage().arrayBuffers === baseline + 1024, "resize retains backing capacity", ); - const moved = source.transferToFixedLength(8); - check(source.byteLength === 0 && moved.byteLength === 8, "store moved to fixed buffer"); + const moved = source.transferToFixedLength(fixedLength); check( - process.memoryUsage().arrayBuffers === baseline + 128, + source.byteLength === 0 && moved.byteLength === fixedLength, + "store moved to fixed buffer", + ); + check( + process.memoryUsage().arrayBuffers === baseline + 1024, "move preserves one backing owner", ); const copied = structuredClone(moved); check( - process.memoryUsage().arrayBuffers === baseline + 136, + process.memoryUsage().arrayBuffers === baseline + 1024 + fixedLength, "clone adds one destination backing", ); const transferred = structuredClone(copied, { transfer: [copied] }); const usage = process.memoryUsage(); check( - copied.byteLength === 0 && transferred.byteLength === 8, + copied.byteLength === 0 && transferred.byteLength === fixedLength, "clone transfer detached source", ); check( - usage.arrayBuffers === baseline + 136 && + usage.arrayBuffers === baseline + 1024 + fixedLength && usage.external >= usage.arrayBuffers && usage.heapTotal >= usage.heapUsed, "serialization moves the backing without changing total ownership", ); check( - moved.byteLength === 8 && new Uint8Array(moved)[0] === 0, + moved.byteLength === fixedLength && new Uint8Array(moved)[0] === 0, "moved backing remains live", ); - check(transferred.byteLength === 8, "transferred backing remains live"); + check(transferred.byteLength === fixedLength, "transferred backing remains live"); } exercise(); diff --git a/tests/local/shared-memory-atomics/clone-take.c b/tests/local/shared-memory-atomics/clone-take.c index 170417f73..f9249245a 100644 --- a/tests/local/shared-memory-atomics/clone-take.c +++ b/tests/local/shared-memory-atomics/clone-take.c @@ -56,9 +56,9 @@ static MalSerializedValue *snapshot(MalVm *vm, MalValue value, MalValue transfer return result; } -static void retain_then_retire_native_owner(MalArrayBufferObject *buffer, bool leased) { +static void retain_then_retire_native_owner(MalHeap *heap, MalArrayBufferObject *buffer, bool leased) { if (!leased) return; - MalArrayBufferStoreLease *lease = mal_array_buffer_object_retain_store(buffer); + MalArrayBufferStoreLease *lease = mal_array_buffer_object_retain_store(heap, buffer); CHECK(lease != nullptr); mal_array_buffer_store_release(lease); } @@ -76,7 +76,7 @@ static void check_transfers(bool leased) { roots[0] = mal_value_from_array_buffer_object(source); memset(source->data, 0xA5, source->allocation_capacity); source->sensitive = true; - retain_then_retire_native_owner(source, leased); + retain_then_retire_native_owner(&vm.heap, source, leased); watched_store = source->data; mal_array_buffer_object_set_release_observer(observe_release); roots[1] = array(&vm, roots, 1); @@ -101,10 +101,12 @@ static void check_transfers(bool leased) { CHECK(mal_array_buffer_object_resize(received, 32)); for (u32 i = 16; i < 32; i++) CHECK(received->data[i] == 0); + // Above the inline limit, so the store is a native allocation the moves can share. + const u32 large_capacity = MAL_ARRAY_BUFFER_INLINE_MAX_BYTES * 2; roots[4] = mal_value_from_array_buffer_object(mal_array_buffer_object_new( - &vm.heap, prototype, 64, 64, false, false)); + &vm.heap, prototype, large_capacity, large_capacity, false, false)); MalArrayBufferObject *large = mal_value_to_array_buffer_object(roots[4]); - retain_then_retire_native_owner(large, leased); + retain_then_retire_native_owner(&vm.heap, large, leased); roots[5] = mal_value_from_array_buffer_object(mal_array_buffer_object_move_store( &vm.heap, prototype, large, 8, 8, false, false)); byte *physical = mal_value_to_array_buffer_object(roots[5])->data; @@ -112,7 +114,7 @@ static void check_transfers(bool leased) { moved = snapshot(&vm, roots[5], roots[6], nullptr); CHECK(mal_deserialize_take(&vm, moved, nullptr, &roots[7])); MalArrayBufferObject *small = mal_value_to_array_buffer_object(roots[7]); - CHECK(small->data == physical && small->allocation_capacity == 64); + CHECK(small->data == physical && small->allocation_capacity == large_capacity); CHECK(!small->resizable && small->byte_length == 8 && small->max_byte_length == 8); mal_serialized_value_release(moved); @@ -132,7 +134,7 @@ static void check_transfers(bool leased) { roots[4] = mal_value_from_array_buffer_object(mal_array_buffer_object_new( &vm.heap, prototype, 16, 64, true, false)); MalArrayBufferObject *failure_source = mal_value_to_array_buffer_object(roots[4]); - retain_then_retire_native_owner(failure_source, leased); + retain_then_retire_native_owner(&vm.heap, failure_source, leased); byte *failure_store = failure_source->data; roots[5] = mal_value_from_object(mal_intrinsic_new_object(&vm)); const MalSerializeHooks hooks = {.data = &roots[5], .encode = encode_failure_marker, .decode = reject_marker}; @@ -149,7 +151,7 @@ static void check_transfers(bool leased) { roots[0] = mal_value_from_array_buffer_object(mal_array_buffer_object_new( &vm.heap, prototype, 32, 32, false, false)); - retain_then_retire_native_owner(mal_value_to_array_buffer_object(roots[0]), leased); + retain_then_retire_native_owner(&vm.heap, mal_value_to_array_buffer_object(roots[0]), leased); roots[1] = array(&vm, roots, 1); moved = snapshot(&vm, roots[0], roots[1], nullptr); mal_serialized_value_release(moved); diff --git a/tests/native/arguments-slice-copy.test.ts b/tests/native/arguments-slice-copy.test.ts new file mode 100644 index 000000000..9aa03ad52 --- /dev/null +++ b/tests/native/arguments-slice-copy.test.ts @@ -0,0 +1,64 @@ +import { execFileSync, spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { + buildBackendPairFromOneProgramImage, + resolveHarnessExecutionInvocation, + runToStdout, + STRESS_ENV, +} from "../../src/test-harness.ts"; +import { preparedNativeEvalConfig } from "../helpers/native-eval-config.ts"; + +const fixture = "tests/local/arguments-slice-copy.cjs"; +const outDir = mkdtempSync(path.join(os.tmpdir(), "mal-arguments-slice-copy-")); +afterAll(() => rmSync(outDir, { recursive: true, force: true })); + +function argumentObjects(binary: string): number { + const invocation = resolveHarnessExecutionInvocation(binary); + const result = spawnSync(invocation.executable, invocation.args, { + env: { ...process.env, MAL_PERF_STATS: "1" }, + encoding: "utf-8", + }); + expect(result.status, result.stderr || result.stdout).toBe(0); + const line = result.stderr + .split("\n") + .find((candidate) => candidate.startsWith("[perf-arguments-stats]")); + expect(line).toBeDefined(); + return Number(line?.match(/(?:^|\s)objects=([0-9]+)/)?.[1]); +} + +describe.each(["mutable", "locked"] as const)( + "arguments copies through slice (%s primordials)", + (primordials) => { + let compiled: string; + let interpreted: string; + let expected: string; + beforeAll(() => { + ({ compiled, interpreted } = buildBackendPairFromOneProgramImage({ + fixture, + name: `arguments-slice-copy-${primordials}`, + outDir, + config: preparedNativeEvalConfig({ primordials }), + environment: { ...process.env, MAL_PERF_STATS: "1" }, + })); + expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); + }, 600_000); + + it("copy the incoming values, or the current ones once a mapped formal changed", () => { + for (const binary of [compiled, interpreted]) { + expect(runToStdout(binary)).toBe(expected); + expect(runToStdout(binary, { env: STRESS_ENV, timeoutMs: 60000 })).toBe(expected); + } + }); + + it.runIf(primordials === "locked")( + "copy without building an arguments object", + () => { + // 4000 copying calls; the shadowed, arrow, optional and written-formal cases build a few. + expect(argumentObjects(compiled)).toBeLessThan(20); + }, + ); + }, +); diff --git a/tests/native/literal-object-assign.test.ts b/tests/native/literal-object-assign.test.ts index c1cd677c9..43899f7d4 100644 --- a/tests/native/literal-object-assign.test.ts +++ b/tests/native/literal-object-assign.test.ts @@ -65,6 +65,9 @@ describe("literal Object.assign", () => { ["specialValues", "preserves Number and BigInt values"], ["keys", "preserves integer and Symbol enumeration"], ["prototype", "invokes the inherited prototype setter"], + ["plainSources", "copies plain data sources in order without aliasing them"], + ["hiddenTarget", "keeps a nonenumerable target field nonenumerable"], + ["sharedPrototype", "reaches objects that inherit from the target"], ])("%s: %s", (scenario) => { const oracle = expected(scenario); for (const binary of [pair.compiled, pair.interpreted]) diff --git a/tests/semantic-analysis-feature-gating.test.ts b/tests/semantic-analysis-feature-gating.test.ts index c4385ae72..22f446b68 100644 --- a/tests/semantic-analysis-feature-gating.test.ts +++ b/tests/semantic-analysis-feature-gating.test.ts @@ -134,3 +134,58 @@ test("dynamic and escaping parameter uses keep mixed arguments bindings eager", expect(file.staticArgumentsAccesses.size).toBe(0); expect(file.lazyArgumentsBindings.size).toBe(0); }); + +test.each([ + [ + "Array.prototype.slice copies record their start", + "return Array.prototype.slice.call(arguments, 2);", + false, + [2], + ], + [ + "array literal slice copies start at zero", + "return [].slice.call(arguments);", + false, + [0], + ], + [ + "a sloppy written formal keeps the object", + "a = 1; return [].slice.call(arguments);", + false, + [], + ], + [ + "a strict written formal still copies", + "a = 1; return [].slice.call(arguments);", + true, + [0], + ], + [ + "a local Array is not the builtin", + "const Array = {}; return Array.prototype.slice.call(arguments);", + true, + [], + ], + [ + "an arrow copies another function's arguments", + "return () => [].slice.call(arguments);", + true, + [], + ], + [ + "an optional chain keeps the object", + "return [].slice.call(arguments)?.length;", + true, + [], + ], + [ + "an element store keeps the object", + "arguments[0] = 1; return [].slice.call(arguments);", + true, + [], + ], + ["a negative start keeps the object", "return [].slice.call(arguments, -1);", true, []], +] as const)("%s", (_, body, strict, starts) => { + const file = analyze(`function copy(a) { ${body} }`, strict); + expect([...file.argumentsCopyCalls.values()]).toEqual(starts); +}); diff --git a/tests/test-suite-native-perf.txt b/tests/test-suite-native-perf.txt index 8a31fff34..295001a91 100644 --- a/tests/test-suite-native-perf.txt +++ b/tests/test-suite-native-perf.txt @@ -2,6 +2,7 @@ # Prepare this shared archive before timed hooks, alongside the ordinary archive. tests/native/arguments-element-reads.test.ts tests/native/arguments-forwarding.test.ts +tests/native/arguments-slice-copy.test.ts tests/native/arguments-static.test.ts tests/native/array-deque-direct.test.ts tests/native/array-push-direct.test.ts @@ -15,18 +16,23 @@ tests/native/direct-known-call.test.ts tests/native/entry-pair-parameters.test.ts tests/native/error-capture-stack.test.ts tests/native/fresh-dense-indexed-reserve.test.ts +tests/native/function-inherited-cache.test.ts tests/native/function-own-slot-cache.test.ts tests/native/gc-raw-realloc.test.ts tests/native/inherited-method-cache.test.ts tests/native/interpreter-property-ic.test.ts tests/native/interpreter-strict-equality.test.ts +tests/native/json-late-fallback.test.ts tests/native/known-own-slot-load.test.ts +tests/native/lazy-capture-environment.test.ts tests/native/map-get-set-cache.test.ts +tests/native/map-string-representative.test.ts tests/native/native-constructor-prototypes.test.ts tests/native/native-shape-snapshots.test.ts tests/native/numeric-sort-callback.test.ts tests/native/object-rest-shaped.test.ts tests/native/object-slot-coallocation.test.ts +tests/native/own-getter-cache.test.ts tests/native/own-table-property-cache.test.ts tests/native/perf-stats.test.ts tests/native/promise-direct-capability.test.ts @@ -36,18 +42,9 @@ tests/native/property-transition-cache.test.ts tests/native/regexp-case-span.test.ts tests/native/shape-case-load.test.ts tests/native/string-char-code-at-direct.test.ts -tests/native/string-split-fresh-array.test.ts -tests/native/tiny-string-cache.test.ts - -tests/native/text-buffer.test.ts - -tests/native/string-rope-followups.test.ts tests/native/string-cursor-frontier.test.ts -tests/native/json-late-fallback.test.ts - +tests/native/string-rope-followups.test.ts tests/native/string-search-followups.test.ts - -tests/native/map-string-representative.test.ts -tests/native/lazy-capture-environment.test.ts -tests/native/own-getter-cache.test.ts -tests/native/function-inherited-cache.test.ts +tests/native/string-split-fresh-array.test.ts +tests/native/text-buffer.test.ts +tests/native/tiny-string-cache.test.ts