3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
17 /* -- Object hashing ------------------------------------------------------ */
19 /* Hash an arbitrary key and return its anchor position in the hash table. */
20 static Node
*hashkey(const GCtab
*t
, cTValue
*key
)
22 lj_assertX(!tvisint(key
), "attempt to hash integer");
24 return hashstr(t
, strV(key
));
25 else if (tvisnum(key
))
26 return hashnum(t
, key
);
27 else if (tvisbool(key
))
28 return hashmask(t
, boolV(key
));
30 return hashgcref(t
, key
->gcr
);
31 /* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */
34 /* -- Table creation and destruction -------------------------------------- */
36 /* Create new hash part for table. */
37 static LJ_AINLINE
void newhpart(lua_State
*L
, GCtab
*t
, uint32_t hbits
)
41 lj_assertL(hbits
!= 0, "zero hash size");
42 if (hbits
> LJ_MAX_HBITS
)
43 lj_err_msg(L
, LJ_ERR_TABOV
);
45 node
= lj_mem_newvec(L
, hsize
, Node
);
46 setmref(t
->node
, node
);
47 setfreetop(t
, node
, &node
[hsize
]);
52 ** Q: Why all of these copies of t->hmask, t->node etc. to local variables?
53 ** A: Because alias analysis for C is _really_ tough.
54 ** Even state-of-the-art C compilers won't produce good code without this.
57 /* Clear hash part of table. */
58 static LJ_AINLINE
void clearhpart(GCtab
*t
)
60 uint32_t i
, hmask
= t
->hmask
;
61 Node
*node
= noderef(t
->node
);
62 lj_assertX(t
->hmask
!= 0, "empty hash part");
63 for (i
= 0; i
<= hmask
; i
++) {
65 setmref(n
->next
, NULL
);
71 /* Clear array part of table. */
72 static LJ_AINLINE
void clearapart(GCtab
*t
)
74 uint32_t i
, asize
= t
->asize
;
75 TValue
*array
= tvref(t
->array
);
76 for (i
= 0; i
< asize
; i
++)
80 /* Create a new table. Note: the slots are not initialized (yet). */
81 static GCtab
*newtab(lua_State
*L
, uint32_t asize
, uint32_t hbits
)
84 /* First try to colocate the array part. */
85 if (LJ_MAX_COLOSIZE
!= 0 && asize
> 0 && asize
<= LJ_MAX_COLOSIZE
) {
87 lj_assertL((sizeof(GCtab
) & 7) == 0, "bad GCtab size");
88 t
= (GCtab
*)lj_mem_newgco(L
, sizetabcolo(asize
));
90 t
->nomm
= (uint8_t)~0;
91 t
->colo
= (int8_t)asize
;
92 setmref(t
->array
, (TValue
*)((char *)t
+ sizeof(GCtab
)));
93 setgcrefnull(t
->metatable
);
96 nilnode
= &G(L
)->nilnode
;
97 setmref(t
->node
, nilnode
);
99 setmref(t
->freetop
, nilnode
);
101 } else { /* Otherwise separately allocate the array part. */
103 t
= lj_mem_newobj(L
, GCtab
);
105 t
->nomm
= (uint8_t)~0;
107 setmref(t
->array
, NULL
);
108 setgcrefnull(t
->metatable
);
109 t
->asize
= 0; /* In case the array allocation fails. */
111 nilnode
= &G(L
)->nilnode
;
112 setmref(t
->node
, nilnode
);
114 setmref(t
->freetop
, nilnode
);
117 if (asize
> LJ_MAX_ASIZE
)
118 lj_err_msg(L
, LJ_ERR_TABOV
);
119 setmref(t
->array
, lj_mem_newvec(L
, asize
, TValue
));
124 newhpart(L
, t
, hbits
);
128 /* Create a new table.
130 ** IMPORTANT NOTE: The API differs from lua_createtable()!
132 ** The array size is non-inclusive. E.g. asize=128 creates array slots
133 ** for 0..127, but not for 128. If you need slots 1..128, pass asize=129
134 ** (slot 0 is wasted in this case).
136 ** The hash size is given in hash bits. hbits=0 means no hash part.
137 ** hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on.
139 GCtab
*lj_tab_new(lua_State
*L
, uint32_t asize
, uint32_t hbits
)
141 GCtab
*t
= newtab(L
, asize
, hbits
);
143 if (t
->hmask
> 0) clearhpart(t
);
147 /* The API of this function conforms to lua_createtable(). */
148 GCtab
*lj_tab_new_ah(lua_State
*L
, int32_t a
, int32_t h
)
150 return lj_tab_new(L
, (uint32_t)(a
> 0 ? a
+1 : 0), hsize2hbits(h
));
154 GCtab
* LJ_FASTCALL
lj_tab_new1(lua_State
*L
, uint32_t ahsize
)
156 GCtab
*t
= newtab(L
, ahsize
& 0xffffff, ahsize
>> 24);
158 if (t
->hmask
> 0) clearhpart(t
);
163 /* Duplicate a table. */
164 GCtab
* LJ_FASTCALL
lj_tab_dup(lua_State
*L
, const GCtab
*kt
)
167 uint32_t asize
, hmask
;
168 t
= newtab(L
, kt
->asize
, kt
->hmask
> 0 ? lj_fls(kt
->hmask
)+1 : 0);
169 lj_assertL(kt
->asize
== t
->asize
&& kt
->hmask
== t
->hmask
,
170 "mismatched size of table and template");
171 t
->nomm
= 0; /* Keys with metamethod names may be present. */
174 TValue
*array
= tvref(t
->array
);
175 TValue
*karray
= tvref(kt
->array
);
176 if (asize
< 64) { /* An inlined loop beats memcpy for < 512 bytes. */
178 for (i
= 0; i
< asize
; i
++)
179 copyTV(L
, &array
[i
], &karray
[i
]);
181 memcpy(array
, karray
, asize
*sizeof(TValue
));
187 Node
*node
= noderef(t
->node
);
188 Node
*knode
= noderef(kt
->node
);
189 ptrdiff_t d
= (char *)node
- (char *)knode
;
190 setfreetop(t
, node
, (Node
*)((char *)getfreetop(kt
, knode
) + d
));
191 for (i
= 0; i
<= hmask
; i
++) {
192 Node
*kn
= &knode
[i
];
194 Node
*next
= nextnode(kn
);
195 /* Don't use copyTV here, since it asserts on a copy of a dead key. */
196 n
->val
= kn
->val
; n
->key
= kn
->key
;
197 setmref(n
->next
, next
== NULL
? next
: (Node
*)((char *)next
+ d
));
204 void LJ_FASTCALL
lj_tab_clear(GCtab
*t
)
208 Node
*node
= noderef(t
->node
);
209 setfreetop(t
, node
, &node
[t
->hmask
+1]);
215 void LJ_FASTCALL
lj_tab_free(global_State
*g
, GCtab
*t
)
218 lj_mem_freevec(g
, noderef(t
->node
), t
->hmask
+1, Node
);
219 if (t
->asize
> 0 && LJ_MAX_COLOSIZE
!= 0 && t
->colo
<= 0)
220 lj_mem_freevec(g
, tvref(t
->array
), t
->asize
, TValue
);
221 if (LJ_MAX_COLOSIZE
!= 0 && t
->colo
)
222 lj_mem_free(g
, t
, sizetabcolo((uint32_t)t
->colo
& 0x7f));
227 /* -- Table resizing ------------------------------------------------------ */
229 /* Resize a table to fit the new array/hash part sizes. */
230 void lj_tab_resize(lua_State
*L
, GCtab
*t
, uint32_t asize
, uint32_t hbits
)
232 Node
*oldnode
= noderef(t
->node
);
233 uint32_t oldasize
= t
->asize
;
234 uint32_t oldhmask
= t
->hmask
;
235 if (asize
> oldasize
) { /* Array part grows? */
238 if (asize
> LJ_MAX_ASIZE
)
239 lj_err_msg(L
, LJ_ERR_TABOV
);
240 if (LJ_MAX_COLOSIZE
!= 0 && t
->colo
> 0) {
241 /* A colocated array must be separated and copied. */
242 TValue
*oarray
= tvref(t
->array
);
243 array
= lj_mem_newvec(L
, asize
, TValue
);
244 t
->colo
= (int8_t)(t
->colo
| 0x80); /* Mark as separated (colo < 0). */
245 for (i
= 0; i
< oldasize
; i
++)
246 copyTV(L
, &array
[i
], &oarray
[i
]);
248 array
= (TValue
*)lj_mem_realloc(L
, tvref(t
->array
),
249 oldasize
*sizeof(TValue
), asize
*sizeof(TValue
));
251 setmref(t
->array
, array
);
253 for (i
= oldasize
; i
< asize
; i
++) /* Clear newly allocated slots. */
256 /* Create new (empty) hash part. */
258 newhpart(L
, t
, hbits
);
261 global_State
*g
= G(L
);
262 setmref(t
->node
, &g
->nilnode
);
264 setmref(t
->freetop
, &g
->nilnode
);
268 if (asize
< oldasize
) { /* Array part shrinks? */
269 TValue
*array
= tvref(t
->array
);
271 t
->asize
= asize
; /* Note: This 'shrinks' even colocated arrays. */
272 for (i
= asize
; i
< oldasize
; i
++) /* Reinsert old array values. */
273 if (!tvisnil(&array
[i
]))
274 copyTV(L
, lj_tab_setinth(L
, t
, (int32_t)i
), &array
[i
]);
275 /* Physically shrink only separated arrays. */
276 if (LJ_MAX_COLOSIZE
!= 0 && t
->colo
<= 0)
277 setmref(t
->array
, lj_mem_realloc(L
, array
,
278 oldasize
*sizeof(TValue
), asize
*sizeof(TValue
)));
280 if (oldhmask
> 0) { /* Reinsert pairs from old hash part. */
283 for (i
= 0; i
<= oldhmask
; i
++) {
284 Node
*n
= &oldnode
[i
];
285 if (!tvisnil(&n
->val
))
286 copyTV(L
, lj_tab_set(L
, t
, &n
->key
), &n
->val
);
289 lj_mem_freevec(g
, oldnode
, oldhmask
+1, Node
);
293 static uint32_t countint(cTValue
*key
, uint32_t *bins
)
295 lj_assertX(!tvisint(key
), "bad integer key");
297 lua_Number nk
= numV(key
);
298 int32_t k
= lj_num2int(nk
);
299 if ((uint32_t)k
< LJ_MAX_ASIZE
&& nk
== (lua_Number
)k
) {
300 bins
[(k
> 2 ? lj_fls((uint32_t)(k
-1)) : 0)]++;
307 static uint32_t countarray(const GCtab
*t
, uint32_t *bins
)
310 if (t
->asize
== 0) return 0;
311 for (na
= i
= b
= 0; b
< LJ_MAX_ABITS
; b
++) {
312 uint32_t n
, top
= 2u << b
;
314 if (top
>= t
->asize
) {
319 array
= tvref(t
->array
);
320 for (n
= 0; i
<= top
; i
++)
321 if (!tvisnil(&array
[i
]))
329 static uint32_t counthash(const GCtab
*t
, uint32_t *bins
, uint32_t *narray
)
331 uint32_t total
, na
, i
, hmask
= t
->hmask
;
332 Node
*node
= noderef(t
->node
);
333 for (total
= na
= 0, i
= 0; i
<= hmask
; i
++) {
335 if (!tvisnil(&n
->val
)) {
336 na
+= countint(&n
->key
, bins
);
344 static uint32_t bestasize(uint32_t bins
[], uint32_t *narray
)
346 uint32_t b
, sum
, na
= 0, sz
= 0, nn
= *narray
;
347 for (b
= 0, sum
= 0; 2*nn
> (1u<<b
) && sum
!= nn
; b
++)
348 if (bins
[b
] > 0 && 2*(sum
+= bins
[b
]) > (1u<<b
)) {
356 static void rehashtab(lua_State
*L
, GCtab
*t
, cTValue
*ek
)
358 uint32_t bins
[LJ_MAX_ABITS
];
359 uint32_t total
, asize
, na
, i
;
360 for (i
= 0; i
< LJ_MAX_ABITS
; i
++) bins
[i
] = 0;
361 asize
= countarray(t
, bins
);
363 total
+= counthash(t
, bins
, &asize
);
364 asize
+= countint(ek
, bins
);
365 na
= bestasize(bins
, &asize
);
367 lj_tab_resize(L
, t
, asize
, hsize2hbits(total
));
371 void lj_tab_rehash(lua_State
*L
, GCtab
*t
)
373 rehashtab(L
, t
, niltv(L
));
377 void lj_tab_reasize(lua_State
*L
, GCtab
*t
, uint32_t nasize
)
379 lj_tab_resize(L
, t
, nasize
+1, t
->hmask
> 0 ? lj_fls(t
->hmask
)+1 : 0);
382 /* -- Table getters ------------------------------------------------------- */
384 cTValue
* LJ_FASTCALL
lj_tab_getinth(GCtab
*t
, int32_t key
)
388 k
.n
= (lua_Number
)key
;
391 if (tvisnum(&n
->key
) && n
->key
.n
== k
.n
)
393 } while ((n
= nextnode(n
)));
397 cTValue
*lj_tab_getstr(GCtab
*t
, const GCstr
*key
)
399 Node
*n
= hashstr(t
, key
);
401 if (tvisstr(&n
->key
) && strV(&n
->key
) == key
)
403 } while ((n
= nextnode(n
)));
407 cTValue
*lj_tab_get(lua_State
*L
, GCtab
*t
, cTValue
*key
)
410 cTValue
*tv
= lj_tab_getstr(t
, strV(key
));
413 } else if (tvisint(key
)) {
414 cTValue
*tv
= lj_tab_getint(t
, intV(key
));
417 } else if (tvisnum(key
)) {
418 lua_Number nk
= numV(key
);
419 int32_t k
= lj_num2int(nk
);
420 if (nk
== (lua_Number
)k
) {
421 cTValue
*tv
= lj_tab_getint(t
, k
);
425 goto genlookup
; /* Else use the generic lookup. */
427 } else if (!tvisnil(key
)) {
432 if (lj_obj_equal(&n
->key
, key
))
434 } while ((n
= nextnode(n
)));
439 /* -- Table setters ------------------------------------------------------- */
441 /* Insert new key. Use Brent's variation to optimize the chain length. */
442 TValue
*lj_tab_newkey(lua_State
*L
, GCtab
*t
, cTValue
*key
)
444 Node
*n
= hashkey(t
, key
);
445 if (!tvisnil(&n
->val
) || t
->hmask
== 0) {
446 Node
*nodebase
= noderef(t
->node
);
447 Node
*collide
, *freenode
= getfreetop(t
, nodebase
);
448 lj_assertL(freenode
>= nodebase
&& freenode
<= nodebase
+t
->hmask
+1,
451 if (freenode
== nodebase
) { /* No free node found? */
452 rehashtab(L
, t
, key
); /* Rehash table. */
453 return lj_tab_set(L
, t
, key
); /* Retry key insertion. */
455 } while (!tvisnil(&(--freenode
)->key
));
456 setfreetop(t
, nodebase
, freenode
);
457 lj_assertL(freenode
!= &G(L
)->nilnode
, "store to fallback hash");
458 collide
= hashkey(t
, &n
->key
);
459 if (collide
!= n
) { /* Colliding node not the main node? */
460 while (noderef(collide
->next
) != n
) /* Find predecessor. */
461 collide
= nextnode(collide
);
462 setmref(collide
->next
, freenode
); /* Relink chain. */
463 /* Copy colliding node into free node and free main node. */
464 freenode
->val
= n
->val
;
465 freenode
->key
= n
->key
;
466 freenode
->next
= n
->next
;
467 setmref(n
->next
, NULL
);
469 /* Rechain pseudo-resurrected string keys with colliding hashes. */
470 while (nextnode(freenode
)) {
471 Node
*nn
= nextnode(freenode
);
472 if (!tvisnil(&nn
->val
) && hashkey(t
, &nn
->key
) == n
) {
473 freenode
->next
= nn
->next
;
475 setmref(n
->next
, nn
);
477 ** Rechaining a resurrected string key creates a new dilemma:
478 ** Another string key may have originally been resurrected via
479 ** _any_ of the previous nodes as a chain anchor. Including
480 ** a node that had to be moved, which makes them unreachable.
481 ** It's not feasible to check for all previous nodes, so rechain
482 ** any string key that's currently in a non-main positions.
484 while ((nn
= nextnode(freenode
))) {
485 if (!tvisnil(&nn
->val
)) {
486 Node
*mn
= hashkey(t
, &nn
->key
);
487 if (mn
!= freenode
&& mn
!= nn
) {
488 freenode
->next
= nn
->next
;
490 setmref(mn
->next
, nn
);
503 } else { /* Otherwise use free node. */
504 setmrefr(freenode
->next
, n
->next
); /* Insert into chain. */
505 setmref(n
->next
, freenode
);
509 n
->key
.u64
= key
->u64
;
510 if (LJ_UNLIKELY(tvismzero(&n
->key
)))
512 lj_gc_anybarriert(L
, t
);
513 lj_assertL(tvisnil(&n
->val
), "new hash slot is not empty");
517 TValue
*lj_tab_setinth(lua_State
*L
, GCtab
*t
, int32_t key
)
521 k
.n
= (lua_Number
)key
;
524 if (tvisnum(&n
->key
) && n
->key
.n
== k
.n
)
526 } while ((n
= nextnode(n
)));
527 return lj_tab_newkey(L
, t
, &k
);
530 TValue
*lj_tab_setstr(lua_State
*L
, GCtab
*t
, const GCstr
*key
)
533 Node
*n
= hashstr(t
, key
);
535 if (tvisstr(&n
->key
) && strV(&n
->key
) == key
)
537 } while ((n
= nextnode(n
)));
539 return lj_tab_newkey(L
, t
, &k
);
542 TValue
*lj_tab_set(lua_State
*L
, GCtab
*t
, cTValue
*key
)
545 t
->nomm
= 0; /* Invalidate negative metamethod cache. */
547 return lj_tab_setstr(L
, t
, strV(key
));
548 } else if (tvisint(key
)) {
549 return lj_tab_setint(L
, t
, intV(key
));
550 } else if (tvisnum(key
)) {
551 lua_Number nk
= numV(key
);
552 int32_t k
= lj_num2int(nk
);
553 if (nk
== (lua_Number
)k
)
554 return lj_tab_setint(L
, t
, k
);
556 lj_err_msg(L
, LJ_ERR_NANIDX
);
557 /* Else use the generic lookup. */
558 } else if (tvisnil(key
)) {
559 lj_err_msg(L
, LJ_ERR_NILIDX
);
563 if (lj_obj_equal(&n
->key
, key
))
565 } while ((n
= nextnode(n
)));
566 return lj_tab_newkey(L
, t
, key
);
569 /* -- Table traversal ----------------------------------------------------- */
571 /* Table traversal indexes:
573 ** Array key index: [0 .. t->asize-1]
574 ** Hash key index: [t->asize .. t->asize+t->hmask]
578 /* Get the successor traversal index of a key. */
579 uint32_t LJ_FASTCALL
lj_tab_keyindex(GCtab
*t
, cTValue
*key
)
583 int32_t k
= intV(key
);
584 if ((uint32_t)k
< t
->asize
)
585 return (uint32_t)k
+ 1;
586 setnumV(&tmp
, (lua_Number
)k
);
588 } else if (tvisnum(key
)) {
589 lua_Number nk
= numV(key
);
590 int32_t k
= lj_num2int(nk
);
591 if ((uint32_t)k
< t
->asize
&& nk
== (lua_Number
)k
)
592 return (uint32_t)k
+ 1;
595 Node
*n
= hashkey(t
, key
);
597 if (lj_obj_equal(&n
->key
, key
))
598 return t
->asize
+ (uint32_t)((n
+1) - noderef(t
->node
));
599 } while ((n
= nextnode(n
)));
600 if (key
->u32
.hi
== LJ_KEYINDEX
) /* Despecialized ITERN while running. */
602 return ~0u; /* Invalid key to next. */
604 return 0; /* A nil key starts the traversal. */
607 /* Get the next key/value pair of a table traversal. */
608 int lj_tab_next(GCtab
*t
, cTValue
*key
, TValue
*o
)
610 uint32_t idx
= lj_tab_keyindex(t
, key
); /* Find successor index of key. */
611 /* First traverse the array part. */
612 for (; idx
< t
->asize
; idx
++) {
613 cTValue
*a
= arrayslot(t
, idx
);
614 if (LJ_LIKELY(!tvisnil(a
))) {
621 /* Then traverse the hash part. */
622 for (; idx
<= t
->hmask
; idx
++) {
623 Node
*n
= &noderef(t
->node
)[idx
];
624 if (!tvisnil(&n
->val
)) {
630 return (int32_t)idx
< 0 ? -1 : 0; /* Invalid key or end of traversal. */
633 /* -- Table length calculation -------------------------------------------- */
635 /* Compute table length. Slow path with mixed array/hash lookups. */
636 LJ_NOINLINE
static MSize
tab_len_slow(GCtab
*t
, size_t hi
)
641 /* Widening search for an upper bound. */
642 while ((tv
= lj_tab_getint(t
, (int32_t)hi
)) && !tvisnil(tv
)) {
645 if (hi
> (size_t)(INT_MAX
-2)) { /* Punt and do a linear search. */
647 while ((tv
= lj_tab_getint(t
, (int32_t)lo
)) && !tvisnil(tv
)) lo
++;
648 return (MSize
)(lo
- 1);
651 /* Binary search to find a non-nil to nil transition. */
652 while (hi
- lo
> 1) {
653 size_t mid
= (lo
+hi
) >> 1;
654 cTValue
*tvb
= lj_tab_getint(t
, (int32_t)mid
);
655 if (tvb
&& !tvisnil(tvb
)) lo
= mid
; else hi
= mid
;
660 /* Compute table length. Fast path. */
661 MSize LJ_FASTCALL
lj_tab_len(GCtab
*t
)
663 size_t hi
= (size_t)t
->asize
;
665 /* In a growing array the last array element is very likely nil. */
666 if (hi
> 0 && LJ_LIKELY(tvisnil(arrayslot(t
, hi
)))) {
667 /* Binary search to find a non-nil to nil transition in the array. */
669 while (hi
- lo
> 1) {
670 size_t mid
= (lo
+hi
) >> 1;
671 if (tvisnil(arrayslot(t
, mid
))) hi
= mid
; else lo
= mid
;
675 /* Without a hash part, there's an implicit nil after the last element. */
676 return t
->hmask
? tab_len_slow(t
, hi
) : (MSize
)hi
;
680 /* Verify hinted table length or compute it. */
681 MSize LJ_FASTCALL
lj_tab_len_hint(GCtab
*t
, size_t hint
)
683 size_t asize
= (size_t)t
->asize
;
684 cTValue
*tv
= arrayslot(t
, hint
);
685 if (LJ_LIKELY(hint
+1 < asize
)) {
686 if (LJ_LIKELY(!tvisnil(tv
) && tvisnil(tv
+1))) return (MSize
)hint
;
687 } else if (hint
+1 <= asize
&& LJ_LIKELY(t
->hmask
== 0) && !tvisnil(tv
)) {
690 return lj_tab_len(t
);