Skip to content

Commit fe3f95b

Browse files
committed
Add precomputed arridx to duk_hstring
1 parent 9bbca14 commit fe3f95b

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

src-input/duk_bi_string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ DUK_INTERNAL duk_ret_t duk_bi_string_prototype_repeat(duk_context *ctx) {
12951295
duk_uint8_t *p;
12961296
duk_double_t d;
12971297
#if !defined(DUK_USE_PREFER_SIZE)
1298-
duk_uint_t copy_size;
1298+
duk_size_t copy_size;
12991299
duk_uint8_t *p_end;
13001300
#endif
13011301

src-input/duk_heap_stringtable.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ duk_hstring *duk__alloc_init_hstring(duk_heap *heap,
3737
duk_hstring *res = NULL;
3838
duk_uint8_t *data;
3939
duk_size_t alloc_size;
40+
#if !defined(DUK_USE_HSTRING_ARRIDX)
4041
duk_uarridx_t dummy;
42+
#endif
4143
duk_uint32_t clen;
4244

4345
#if defined(DUK_USE_STRLEN16)
@@ -80,7 +82,11 @@ duk_hstring *duk__alloc_init_hstring(duk_heap *heap,
8082
}
8183

8284
DUK_ASSERT(!DUK_HSTRING_HAS_ARRIDX(res));
85+
#if defined(DUK_USE_HSTRING_ARRIDX)
86+
if (duk_js_to_arrayindex_raw_string(str, blen, &res->arridx)) {
87+
#else
8388
if (duk_js_to_arrayindex_raw_string(str, blen, &dummy)) {
89+
#endif
8490
DUK_HSTRING_SET_ARRIDX(res);
8591
}
8692

src-input/duk_hstring.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,20 @@
134134
/* marker value; in E5 2^32-1 is not a valid array index (2^32-2 is highest valid) */
135135
#define DUK_HSTRING_NO_ARRAY_INDEX (0xffffffffUL)
136136

137-
/* get array index related to string (or return DUK_HSTRING_NO_ARRAY_INDEX);
137+
#if defined(DUK_USE_HSTRING_ARRIDX)
138+
#define DUK_HSTRING_GET_ARRIDX_FAST(h) ((h)->arridx)
139+
#define DUK_HSTRING_GET_ARRIDX_SLOW(h) ((h)->arridx)
140+
#else
141+
/* Get array index related to string (or return DUK_HSTRING_NO_ARRAY_INDEX);
138142
* avoids helper call if string has no array index value.
139143
*/
140144
#define DUK_HSTRING_GET_ARRIDX_FAST(h) \
141145
(DUK_HSTRING_HAS_ARRIDX((h)) ? duk_js_to_arrayindex_string_helper((h)) : DUK_HSTRING_NO_ARRAY_INDEX)
142146

143-
/* slower but more compact variant */
147+
/* Slower but more compact variant. */
144148
#define DUK_HSTRING_GET_ARRIDX_SLOW(h) \
145149
(duk_js_to_arrayindex_string_helper((h)))
150+
#endif
146151

147152
/*
148153
* Misc
@@ -167,6 +172,11 @@ struct duk_hstring {
167172
duk_uint32_t hash;
168173
#endif
169174

175+
/* precomputed array index (or DUK_HSTRING_NO_ARRAY_INDEX) */
176+
#if defined(DUK_USE_HSTRING_ARRIDX)
177+
duk_uarridx_t arridx;
178+
#endif
179+
170180
/* length in bytes (not counting NUL term) */
171181
#if defined(DUK_USE_STRLEN16)
172182
/* placed in duk_heaphdr_string */

src-input/duk_js_ops.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,8 +1277,10 @@ DUK_INTERNAL duk_small_uint_t duk_js_typeof_stridx(duk_tval *tv_x) {
12771277
* Array index: E5 Section 15.4
12781278
* Array length: E5 Section 15.4.5.1 steps 3.c - 3.d (array length write)
12791279
*
1280-
* The DUK_HSTRING_GET_ARRIDX_SLOW() and DUK_HSTRING_GET_ARRIDX_FAST() macros
1281-
* call duk_js_to_arrayindex_string_helper().
1280+
* duk_js_to_arrayindex_string_helper() computes the array index from
1281+
* string contents alone. Depending on options it's only called during
1282+
* string intern (and value stored to duk_hstring) or it's called also
1283+
* at runtime.
12821284
*/
12831285

12841286
DUK_INTERNAL duk_small_int_t duk_js_to_arrayindex_raw_string(const duk_uint8_t *str, duk_uint32_t blen, duk_uarridx_t *out_idx) {

0 commit comments

Comments
 (0)