Skip to content

Commit d9ce4a0

Browse files
Peter Neissclaude
andcommitted
Phase 135: Table Rehashing Variables - Identifier Modernization
Renamed cryptic table rehashing variables to descriptive names for improved readability in complex hash table resizing algorithms. **Counters Struct (3 variables):** - `na` → `arrayCount` (~15 uses) - Number of array indices - `nh` → `hashCount` (function param) - Number of hash nodes **Hash Chain Navigation (3 uses):** - `nx` → `nextIndex` - Next index in collision chain **Resize Parameters (20+ uses):** - `nasize` → `newArraySize` - New array part size - `nhsize` → `newHashSize` - New hash part size - `oldasize` → `oldArraySize` - Old array part size **Functions Updated:** - `arrayXhash()` - Memory efficiency comparison - `computesizes()` - Optimal array size calculation - `countint()`, `numusearray()`, `numusehash()` - Key counting - `rehash()` - Main rehashing logic - `resizearray()` - Array part resizing - `reinsertOldSlice()`, `clearNewSlice()` - Slice management - `resize()`, `resizeArray()` - Public Table methods - `getgeneric()`, `getintfromhash()`, `HgetShortStr()` - Hash chain traversal **Impact**: Table rehashing is notoriously complex; these descriptive names make the dual-representation optimization algorithm much clearer. **Files Changed**: 2 files - src/objects/ltable.cpp (~85 identifier renames + comments) - src/objects/ltable.h (2 function signatures) **Testing**: All tests pass ✅ (2.31s first run) **Benchmark**: 5-run average = 2.13s ✅ (range 2.09-2.19s, maintained!) **Risk**: LOW (all local/parameter scope, no hot-path changes) **Clarity**: ⭐⭐ → ⭐⭐⭐⭐⭐ (cryptic 2-letter names → self-documenting) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ac1b5b6 commit d9ce4a0

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

src/objects/ltable.cpp

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@ static TValue *getgeneric (const Table& t, const TValue *key, int deadok) {
418418
if (equalkey(key, n, deadok))
419419
return gval(n); /* that's it */
420420
else {
421-
int nx = gnext(n);
422-
if (nx == 0)
421+
int nextIndex = gnext(n);
422+
if (nextIndex == 0)
423423
return &absentkey; /* not found */
424-
n += nx;
424+
n += nextIndex;
425425
lua_assert(n >= base && n < limit); /* ensure we stay in bounds */
426426
}
427427
}
@@ -524,36 +524,36 @@ static void newcheckedkey (Table& t, const TValue *key, TValue *value);
524524
/*
525525
** Structure to count the keys in a table.
526526
** 'total' is the total number of keys in the table.
527-
** 'na' is the number of *array indices* in the table (see 'arrayindex').
527+
** 'arrayCount' is the number of *array indices* in the table (see 'arrayindex').
528528
** 'deleted' is true if there are deleted nodes in the hash part.
529529
** 'nums' is a "count array" where 'nums[i]' is the number of integer
530-
** keys between 2^(i - 1) + 1 and 2^i. Note that 'na' is the summation
530+
** keys between 2^(i - 1) + 1 and 2^i. Note that 'arrayCount' is the summation
531531
** of 'nums'.
532532
*/
533533
typedef struct {
534534
unsigned total;
535-
unsigned na;
535+
unsigned arrayCount;
536536
int deleted;
537537
unsigned nums[MAXABITS + 1];
538538
} Counters;
539539

540540

541541
/*
542-
** Check whether it is worth to use 'na' array entries instead of 'nh'
542+
** Check whether it is worth to use 'arrayCount' array entries instead of 'hashCount'
543543
** hash nodes. (A hash node uses ~3 times more memory than an array
544544
** entry: Two values plus 'next' versus one value.) Evaluate with size_t
545545
** to avoid overflows.
546546
*/
547-
inline bool arrayXhash(unsigned na, unsigned nh) noexcept {
548-
return cast_sizet(na) <= cast_sizet(nh) * 3;
547+
inline bool arrayXhash(unsigned arrayCount, unsigned hashCount) noexcept {
548+
return cast_sizet(arrayCount) <= cast_sizet(hashCount) * 3;
549549
}
550550

551551
/*
552552
** Compute the optimal size for the array part of table 't'.
553553
** This size maximizes the number of elements going to the array part
554554
** while satisfying the condition 'arrayXhash' with the use of memory if
555555
** all those elements went to the hash part.
556-
** 'ct->na' enters with the total number of array indices in the table
556+
** 'ct->arrayCount' enters with the total number of array indices in the table
557557
** and leaves with the number of keys that will go to the array part;
558558
** return the optimal size for the array part.
559559
*/
@@ -564,7 +564,7 @@ static unsigned computesizes (Counters *ct) {
564564
/* traverse slices while 'powerOfTwo' does not overflow and total of array
565565
indices still can satisfy 'arrayXhash' against the array size */
566566
for (unsigned int i = 0, powerOfTwo = 1; /* 2^i (candidate for optimal size) */
567-
powerOfTwo > 0 && arrayXhash(powerOfTwo, ct->na);
567+
powerOfTwo > 0 && arrayXhash(powerOfTwo, ct->arrayCount);
568568
i++, powerOfTwo *= 2) {
569569
unsigned elementCount = ct->nums[i];
570570
accumulatedCount += elementCount;
@@ -574,7 +574,7 @@ static unsigned computesizes (Counters *ct) {
574574
arrayCount = accumulatedCount; /* all elements up to 'optimalSize' will go to array part */
575575
}
576576
}
577-
ct->na = arrayCount;
577+
ct->arrayCount = arrayCount;
578578
return optimalSize;
579579
}
580580

@@ -583,7 +583,7 @@ static void countint (lua_Integer key, Counters *ct) {
583583
unsigned int k = arrayindex(key);
584584
if (k != 0) { /* is 'key' an array index? */
585585
ct->nums[luaO_ceillog2(k)]++; /* count as such */
586-
ct->na++;
586+
ct->arrayCount++;
587587
}
588588
}
589589

@@ -619,7 +619,7 @@ static void numusearray (const Table& t, Counters *ct) {
619619
arrayUseCount += sliceCount;
620620
}
621621
ct->total += arrayUseCount;
622-
ct->na += arrayUseCount;
622+
ct->arrayCount += arrayUseCount;
623623
}
624624

625625

@@ -677,13 +677,13 @@ static size_t concretesize (unsigned int size) {
677677
** new size is double the old one (the most common case).
678678
*/
679679
static Value *resizearray (lua_State *L , Table& t,
680-
unsigned oldasize,
680+
unsigned oldArraySize,
681681
unsigned newasize) {
682-
if (oldasize == newasize)
682+
if (oldArraySize == newasize)
683683
return t.getArray(); /* nothing to be done */
684684
else if (newasize == 0) { /* erasing array? */
685-
Value *op = t.getArray() - oldasize; /* original array's real address */
686-
luaM_freemem(L, op, concretesize(oldasize)); /* free it */
685+
Value *op = t.getArray() - oldArraySize; /* original array's real address */
686+
luaM_freemem(L, op, concretesize(oldArraySize)); /* free it */
687687
return nullptr;
688688
}
689689
else {
@@ -693,18 +693,18 @@ static Value *resizearray (lua_State *L , Table& t,
693693
if (np == nullptr) /* allocation error? */
694694
return nullptr;
695695
np += newasize; /* shift pointer to the end of value segment */
696-
if (oldasize > 0) {
696+
if (oldArraySize > 0) {
697697
/* move common elements to new position */
698-
size_t oldasizeb = concretesize(oldasize);
698+
size_t oldasizeb = concretesize(oldArraySize);
699699
Value *op = t.getArray(); /* original array */
700-
unsigned tomove = (oldasize < newasize) ? oldasize : newasize;
701-
size_t tomoveb = (oldasize < newasize) ? oldasizeb : newasizeb;
700+
unsigned tomove = (oldArraySize < newasize) ? oldArraySize : newasize;
701+
size_t tomoveb = (oldArraySize < newasize) ? oldasizeb : newasizeb;
702702
lua_assert(tomoveb > 0);
703703
lua_assert(tomove <= newasize); /* ensure destination bounds */
704-
lua_assert(tomove <= oldasize); /* ensure source bounds */
704+
lua_assert(tomove <= oldArraySize); /* ensure source bounds */
705705
lua_assert(tomoveb <= newasizeb); /* verify size calculation */
706706
memcpy(np - tomove, op - tomove, tomoveb);
707-
luaM_freemem(L, op - oldasize, oldasizeb); /* free old block */
707+
luaM_freemem(L, op - oldArraySize, oldasizeb); /* free old block */
708708
}
709709
return np;
710710
}
@@ -787,9 +787,9 @@ static void exchangehashpart (Table& t1, Table& t2) {
787787
** Re-insert into the new hash part of a table the elements from the
788788
** vanishing slice of the array part.
789789
*/
790-
static void reinsertOldSlice (Table& t, unsigned oldasize,
790+
static void reinsertOldSlice (Table& t, unsigned oldArraySize,
791791
unsigned newasize) {
792-
for (unsigned i = newasize; i < oldasize; i++) { /* traverse vanishing slice */
792+
for (unsigned i = newasize; i < oldArraySize; i++) { /* traverse vanishing slice */
793793
LuaT tag = *t.getArrayTag(i);
794794
if (!tagisempty(tag)) { /* a non-empty entry? */
795795
TValue key, aux;
@@ -804,9 +804,9 @@ static void reinsertOldSlice (Table& t, unsigned oldasize,
804804
/*
805805
** Clear new slice of the array.
806806
*/
807-
static void clearNewSlice (Table& t, unsigned oldasize, unsigned newasize) {
808-
for (; oldasize < newasize; oldasize++)
809-
*t.getArrayTag(oldasize) = LuaT::EMPTY;
807+
static void clearNewSlice (Table& t, unsigned oldArraySize, unsigned newasize) {
808+
for (; oldArraySize < newasize; oldArraySize++)
809+
*t.getArrayTag(oldArraySize) = LuaT::EMPTY;
810810
}
811811

812812

@@ -822,16 +822,16 @@ static void clearNewSlice (Table& t, unsigned oldasize, unsigned newasize) {
822822
** into the table, initializes the new part of the array (if any) with
823823
** nils and reinserts the elements of the old hash back into the new
824824
** parts of the table.
825-
** Note that if the new size for the array part ('newasize') is equal to
826-
** the old one ('oldasize'), this function will do nothing with that
825+
** Note that if the new size for the array part ('newArraySize') is equal to
826+
** the old one ('oldArraySize'), this function will do nothing with that
827827
** part.
828828
*/
829829
/*
830830
** Resize a table to the given array and hash sizes.
831831
**
832832
** PARAMETERS:
833-
** - newasize: New size for the array part
834-
** - nhsize: New size for the hash part (number of hash nodes)
833+
** - newArraySize: New size for the array part
834+
** - newHashSize: New size for the hash part (number of hash nodes)
835835
**
836836
** ALGORITHM:
837837
** 1. Allocate new hash part (into temporary 'newt')
@@ -889,14 +889,14 @@ static void rehash (lua_State *L, Table& t, const TValue *extraKey) {
889889
Counters counters;
890890
/* reset counts */
891891
std::fill_n(counters.nums, MAXABITS + 1, 0);
892-
counters.na = 0;
892+
counters.arrayCount = 0;
893893
counters.deleted = 0;
894894
counters.total = 1; /* count extra key */
895895
if (ttisinteger(extraKey))
896896
countint(ivalue(extraKey), &counters); /* extra key may go to array */
897897
numusehash(t, &counters); /* count keys in hash part */
898898
unsigned arraySize; /* optimal size for array part */
899-
if (counters.na == 0) {
899+
if (counters.arrayCount == 0) {
900900
/* no new keys to enter array part; keep it with the same size */
901901
arraySize = t.arraySize();
902902
}
@@ -905,7 +905,7 @@ static void rehash (lua_State *L, Table& t, const TValue *extraKey) {
905905
arraySize = computesizes(&counters); /* compute new size for array part */
906906
}
907907
/* all keys not in the array part go to the hash part */
908-
unsigned hashSize = counters.total - counters.na; /* size for the hash part */
908+
unsigned hashSize = counters.total - counters.arrayCount; /* size for the hash part */
909909
if (counters.deleted) { /* table has deleted entries? */
910910
/* insertion-deletion-insertion: give hash some extra size to
911911
avoid repeated resizings */
@@ -1027,9 +1027,9 @@ static TValue *getintfromhash (const Table& t, lua_Integer key) {
10271027
if (n->isKeyInteger() && n->getKeyIntValue() == key)
10281028
return gval(n); /* that's it */
10291029
else {
1030-
int nx = gnext(n);
1031-
if (nx == 0) break;
1032-
n += nx;
1030+
int nextIndex = gnext(n);
1031+
if (nextIndex == 0) break;
1032+
n += nextIndex;
10331033
}
10341034
}
10351035
return &absentkey;
@@ -1249,10 +1249,10 @@ TValue* Table::HgetShortStr(TString* key) const {
12491249
if (n->isKeyShrStr() && shortStringsEqual(n->getKeyStrValue(), key))
12501250
return gval(n); /* that's it */
12511251
else {
1252-
int nx = gnext(n);
1253-
if (nx == 0)
1252+
int nextIndex = gnext(n);
1253+
if (nextIndex == 0)
12541254
return &absentkey; /* not found */
1255-
n += nx;
1255+
n += nextIndex;
12561256
}
12571257
}
12581258
}
@@ -1373,41 +1373,41 @@ void Table::finishSet(lua_State* L, const TValue* key, TValue* value, int hres)
13731373
}
13741374
}
13751375

1376-
void Table::resize(lua_State* L, unsigned nasize, unsigned nhsize) {
1377-
if (nasize > MAXASIZE)
1376+
void Table::resize(lua_State* L, unsigned newArraySize, unsigned newHashSize) {
1377+
if (newArraySize > MAXASIZE)
13781378
luaG_runerror(L, "table overflow");
13791379
/* create new hash part with appropriate size into 'newt' */
13801380
Table newt; /* to keep the new hash part */
13811381
newt.setFlags(0);
1382-
setnodevector(*L, newt, nhsize);
1383-
unsigned oldasize = this->arraySize();
1384-
if (nasize < oldasize) { /* will array shrink? */
1382+
setnodevector(*L, newt, newHashSize);
1383+
unsigned oldArraySize = this->arraySize();
1384+
if (newArraySize < oldArraySize) { /* will array shrink? */
13851385
/* re-insert into the new hash the elements from vanishing slice */
13861386
exchangehashpart(*this, newt); /* pretend table has new hash */
1387-
reinsertOldSlice(*this, oldasize, nasize);
1387+
reinsertOldSlice(*this, oldArraySize, newArraySize);
13881388
exchangehashpart(*this, newt); /* restore old hash (in case of errors) */
13891389
}
13901390
/* allocate new array */
1391-
Value *newarray = resizearray(L, *this, oldasize, nasize);
1392-
if (l_unlikely(newarray == nullptr && nasize > 0)) { /* allocation failed? */
1391+
Value *newarray = resizearray(L, *this, oldArraySize, newArraySize);
1392+
if (l_unlikely(newarray == nullptr && newArraySize > 0)) { /* allocation failed? */
13931393
freehash(*L, newt); /* release new hash part */
13941394
luaM_error(L); /* raise error (with array unchanged) */
13951395
}
13961396
/* allocation ok; initialize new part of the array */
13971397
exchangehashpart(*this, newt); /* 't' has the new hash ('newt' has the old) */
13981398
this->setArray(newarray); /* set new array part */
1399-
this->setArraySize(nasize);
1399+
this->setArraySize(newArraySize);
14001400
if (newarray != nullptr)
1401-
*this->getLenHint() = nasize / 2u; /* set an initial hint */
1402-
clearNewSlice(*this, oldasize, nasize);
1401+
*this->getLenHint() = newArraySize / 2u; /* set an initial hint */
1402+
clearNewSlice(*this, oldArraySize, newArraySize);
14031403
/* re-insert elements from old hash part into new parts */
14041404
reinserthash(*L, newt, *this); /* 'newt' now has the old hash */
14051405
freehash(*L, newt); /* free old hash part */
14061406
}
14071407

1408-
void Table::resizeArray(lua_State* L, unsigned nasize) {
1408+
void Table::resizeArray(lua_State* L, unsigned newArraySize) {
14091409
unsigned nsize = (this->isDummy()) ? 0 : this->nodeSize();
1410-
this->resize(L, nasize, nsize);
1410+
this->resize(L, newArraySize, nsize);
14111411
}
14121412

14131413
lu_mem Table::size() const {

src/objects/ltable.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ class Table : public GCBase<Table> {
279279
void setInt(lua_State* L, lua_Integer key, TValue* value);
280280
void finishSet(lua_State* L, const TValue* key, TValue* value, int hres);
281281

282-
void resize(lua_State* L, unsigned nasize, unsigned nhsize);
283-
void resizeArray(lua_State* L, unsigned nasize);
282+
void resize(lua_State* L, unsigned newArraySize, unsigned newHashSize);
283+
void resizeArray(lua_State* L, unsigned newArraySize);
284284
[[nodiscard]] lu_mem size() const;
285285
[[nodiscard]] int tableNext(lua_State* L, StkId key) const; // renamed from next() to avoid conflict with GC field
286286
[[nodiscard]] lua_Unsigned getn(lua_State* L);

0 commit comments

Comments
 (0)