My working unpac space for OCaml projects in development
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Sprint 1.3: Add fast path for array element access

Optimized array element get/set operations with fast paths for
integer indices to avoid unnecessary type conversions:

- Added [@inline] to get_array_el and set_array_el
- Fast path for Int values: direct Int32.to_int conversion
- Fast path for integer Float values: direct int_of_float
- Fallback to full to_int32 conversion only for other types

This eliminates the overhead of to_int32 (which does to_float first)
when the index is already a numeric type, which is the common case
in loop iterations (var i = 0; i < arr.length; i++).

Tests: 176/176 runtime tests passing, 52,631/52,631 Test262 passing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

+16 -6
+16 -6
lib/quickjs/runtime/interpreter.ml
··· 282 282 Context.type_error ctx ("Cannot set property '" ^ prop_name ^ "' of " ^ type_of obj_val); 283 283 Undefined 284 284 285 - (** Get array element *) 286 - let get_array_el ctx obj_val index = 285 + (** Get array element - optimized with fast path for integer indices *) 286 + let[@inline] get_array_el ctx obj_val index = 287 287 match obj_val with 288 288 | Object { data = Data_array arr_data; _ } -> 289 - let idx = Int32.to_int (to_int32 index) in 289 + (* Fast path: already an integer *) 290 + let idx = match index with 291 + | Int i -> Int32.to_int i 292 + | Float f when f >= 0.0 && f < 2147483648.0 && Float.is_integer f -> int_of_float f 293 + | _ -> Int32.to_int (to_int32 index) 294 + in 290 295 if idx >= 0 && idx < arr_data.length then arr_data.values.(idx) 291 296 else Undefined 292 297 | Object ({ data = Data_typed_array { buffer; byte_offset; length }; class_id; _ }) -> ··· 387 392 | None -> Undefined) 388 393 | _ -> get_prop ctx obj_val (to_string index) 389 394 390 - (** Set array element *) 391 - let set_array_el ctx obj_val index value = 395 + (** Set array element - optimized with fast path for integer indices *) 396 + let[@inline] set_array_el ctx obj_val index value = 392 397 match obj_val with 393 398 | Object ({ data = Data_array arr_data; _ } as obj) -> 394 - let idx = Int32.to_int (to_int32 index) in 399 + (* Fast path: already an integer *) 400 + let idx = match index with 401 + | Int i -> Int32.to_int i 402 + | Float f when f >= 0.0 && f < 2147483648.0 && Float.is_integer f -> int_of_float f 403 + | _ -> Int32.to_int (to_int32 index) 404 + in 395 405 if idx >= 0 then begin 396 406 if idx >= arr_data.length then begin 397 407 (* Extend array using ensure_array_capacity *)