351 Commits

Author SHA1 Message Date
Roberto I
962f444a75 Details
In an assignment like 'a = &b', is looks suspicious if 'a' has a scope
larger than 'b'.
2026-01-04 16:27:54 -03:00
Roberto I
c4e2c91973 Details
Some comments still talked about bit 'isrealasize', which has been
removed.
2025-12-30 10:50:49 -03:00
Roberto I
5b179eaf6a Details 2025-08-09 15:08:53 -03:00
Roberto Ierusalimschy
303f415559 Randomness added to table length computation
A bad actor could fill only a few entries in a table (power of twos in
decreasing order, see tests) and produce a small table with a huge
length. If your program builds a table with external data and iterates
over its length, this behavior could be an issue.
2025-07-18 16:18:30 -03:00
Roberto Ierusalimschy
60b6599e83 Short strings can be external, too
That complicates a little object equality (and therefore table access
for long strings), but the old behavior was somewhat weird. (Short
strings, a concept otherwise absent from the manual, could not be
external.)
2025-07-15 14:40:27 -03:00
Roberto Ierusalimschy
ef5d171cc8 New macro 'l_numbits' 2025-03-27 12:38:29 -03:00
Roberto Ierusalimschy
2d8d5c74b5 Details
New year (2024->2025), typos in comments
2025-01-16 11:51:16 -03:00
Roberto Ierusalimschy
3cdd49c94a Fixed conversion warnings from clang
Plus some other details. (Option '-Wuninitialized' was removed from
the makefile because it is already enabled by -Wall.)
2025-01-14 16:24:46 -03:00
Roberto Ierusalimschy
1ec251e091 Detail (debugging aid)
When compiling with option HARDMEMTESTS, every creation of a new key
in a table forces an emergency GC.
2025-01-06 12:41:39 -03:00
Roberto Ierusalimschy
abf8b1cd4a Small optimization in 'luaH_psetshortstr'
Do not optimize only for table updates (key already present).
Creation of new short keys in new tables can be quite common in
programs that create lots of small tables, for instance with
constructors like {x=e1,y=e2}.
2024-12-28 15:05:01 -03:00
Roberto Ierusalimschy
1c40ff9faa Scanner and parser use different tables for constants
Moreover, each function being parsed has its own table.

The code is cleaner when each table is used for one specific purpose:
The scanner uses its table to anchor and unify strings, mapping strings
to themselves; the parser uses it to reuse constants in the code,
mapping constants to their indices in the constant table. A different
table for each task avoids false collisions.
2024-12-17 11:23:22 -03:00
Roberto Ierusalimschy
b4b616bdf2 Rehash reinserts elements with "lighter" functions
When reinserting elements into a table during a rehash, the code does
not need to invoke all the complexity of a full 'luaH_set':

- The table has space for all keys.
- The key cannot exist in the new hash.
- The keys are valid (not NaN nor nil).
- The keys are normalized (1.0 -> 1).
- The values cannot be nil.
- No barrier needed (the table already pointed to the key and value).
2024-12-05 17:34:08 -03:00
Roberto Ierusalimschy
bb93f04d87 Refactoring of 'luaH_newkey'
Function broke in two and some checks moved to the caller. (We may
want to call it without the checks.)
2024-12-05 14:27:58 -03:00
Roberto Ierusalimschy
002beeebe7 New way to keep hints for table length
Instead of using 'alimit' for keeping the size of the array and at
the same time being a hint for '#t', a table now keeps these two
values separate. The Table structure has a field 'asize' with the
size of the array, while the length hint is kept in the array itself.
That way, tables with no array part waste no space with that field.
Moreover, the space for the hint may have zero cost for small arrays,
if the array of tags plus the hint still fits in a single word.
2024-11-29 17:26:20 -03:00
Roberto Ierusalimschy
9329eeac3b Avoid an extra call to 'concretesize' in 'resizearray' 2024-11-27 18:37:29 -03:00
Roberto Ierusalimschy
a4762b6ffe 'objsize' returns 'l_mem'
Sums of size_t may not fit in a size_t.
2024-11-15 12:04:53 -03:00
Roberto Ierusalimschy
8a4419b119 Dummy node has a non-nil key
That allows 'getfreepos' to treat it like a regular hash part that has
a deleted entry.
2024-11-15 10:48:52 -03:00
Roberto Ierusalimschy
9a91fe1640 Add extra size when resizing tables with deleted keys
Without this extra space, sequences of insertions/deletions (and
some other uses) can have unpexpected low performances.  See the
added tests for an example, and *Mathematical Models to Analyze Lua
Hybrid Tables and Why They Need a Fix* (Martínez, Nicaud, Rotondo;
arXiv:2208.13602v2) for detais.
2024-11-14 11:48:25 -03:00
Roberto Ierusalimschy
2491b87c10 New rule for size of array part
Array part needs 1/3 of its elements filled, instead of 1/2.
Array entries use ~1/3 the memory of hash entries, so this new rule
still ensures that array parts do not use more memory than keeping
the values in the hash, while allowing more uses of the array part,
which is more efficient than the hash.
2024-11-13 13:37:24 -03:00
Roberto Ierusalimschy
0de8191152 New structure to count keys in a table for rehashing 2024-10-28 14:15:21 -03:00
Roberto Ierusalimschy
853311e5b1 Table rehash can resize only the hash part
If there are no integer keys outside the array part, there is no
reason to resize it, saving the time to count its elements.  Moreover,
assignments to non-integer keys will not collapse a table created with
'table.create'.
2024-10-28 10:54:36 -03:00
Roberto Ierusalimschy
25a2dac2bc Always use unsigned int for indexing table-arrays 2024-10-24 15:33:25 -03:00
Roberto Ierusalimschy
3d54b42d59 'objsize' broke in smaller pieces 2024-09-30 14:01:42 -03:00
Roberto Ierusalimschy
8fac494509 Avoid Microsoft warning
> warning C4334: '<<': result of 32-bit shift implicitly converted to
> 64 bits (was 64-bit shift intended?)
2024-09-19 19:09:35 -03:00
Roberto Ierusalimschy
0acd55898d Added gcc option '-Wconversion'
No warnings for standard numerical types. Still pending alternative
numerical types.
2024-07-27 13:32:59 -03:00
Roberto Ierusalimschy
6ac7219da3 'isIT'/'isOT' turned from macros to functions 2024-06-27 15:01:57 -03:00
Roberto Ierusalimschy
94b503d95e Encoding of table indices (hres) must use C indices
As the encoding of array indices is (~index), 0 is encoded as -1 and
INT_MAX is encoded as INT_MIN.
2024-06-10 12:09:35 -03:00
Roberto Ierusalimschy
5edacafcfa Yet another representation for arrays
This "linear" representation (see ltable.h for details) has worse
locality than cells, but the simpler access code seems to compensate
that.
2024-04-05 15:35:11 -03:00
Roberto Ierusalimschy
9fa63a6268 Some 'unsigned int' changed to 'unsigned'
'unsigned int' is too long sometimes. (We already write 'long' instead
of 'long int'...)
2024-03-22 14:06:11 -03:00
Roberto Ierusalimschy
0593256707 'luaH_get' functions return tag of the result
Undoing previous commit. Returning TValue increases code size without
any visible gains. Returning the tag is a little simpler than returning
a special code (HOK/HNOTFOUND) and the tag is useful by itself in
some cases.
2024-03-21 11:23:21 -03:00
Roberto Ierusalimschy
ce6f5502c9 'luaH_get' functions return 'TValue'
Instead of receiving a parameter telling them where to put the result
of the query, these functions return the TValue directly. (That is,
they return a structure.)
2024-03-18 15:56:32 -03:00
Roberto Ierusalimschy
ba71060381 Removed "bulk operations"
Negligible performance gains don't justify extra complexity.
2024-03-15 11:23:35 -03:00
Roberto Ierusalimschy
3823fc6c81 Added "bulk operations" to arrays
A few operations on arrays can be performed "in bulk", treating all
tags of a cell as a simple (or a few) word(s).
2024-03-15 11:01:34 -03:00
Roberto Ierusalimschy
7237eb3f1c Fixed warnings from different compilers 2024-02-15 11:18:34 -03:00
Roberto Ierusalimschy
6063c47031 Field 'lastfree' changed (back) to 'Node *'
Due to allignment, it is already using the space of a pointer, and
a pointer generates slightly simpler code.
2024-02-07 13:56:39 -03:00
Roberto Ierusalimschy
0c9bec0d38 Better handling of size limit when resizing a table
Avoid silent conversions from int to unsigned int when calling
'luaH_resize'; avoid silent conversions from lua_Integer to int in
'table.create'; MAXASIZE corrected for the new implementation of arrays;
'luaH_resize' checks explicitly whether new size respects MAXASIZE.
(Even constructors were bypassing that check.)
2024-02-07 13:39:54 -03:00
Roberto Ierusalimschy
b34a97a4af Small optimization in 'luaH_psetint'
It is quite common to write to empty but existing cells in the array
part of a table, so 'luaH_psetint' checks for the common case that
the table doesn't have a newindex metamethod to complete the write.
2024-01-25 13:44:49 -03:00
Roberto Ierusalimschy
d862da6d04 Optimizations for 'lua_rawgeti' and 'lua_rawseti'
'lua_rawgeti' now uses "fast track"; 'lua_rawseti' still calls
'luaH_setint', but the latter was recoded to avoid extra overhead
when writing to the array part after 'alimit'.
2024-01-12 15:50:51 -03:00
Roberto Ierusalimschy
52b899d60d Simpler coding for new representation for arrays
With the tags comming first in a cell, we can define the whole cell
as a C type and let C do part of the address computations.
2023-11-24 14:41:07 -03:00
Roberto Ierusalimschy
37c215b43f Merge branch 'newarray' into nextversion 2023-11-07 17:26:15 -03:00
Roberto Ierusalimschy
9e99f3071d Merge branch 'master' into nextversion 2023-11-07 17:25:46 -03:00
Roberto Ierusalimschy
fa075b7953 Merge branch 'master' into newarray 2023-11-03 15:39:14 -03:00
Roberto Ierusalimschy
08a077d673 Full implementation of new representation for arrays 2023-11-03 15:26:13 -03:00
Roberto Ierusalimschy
43c8e5bded Full abstraction for representation of array values 2023-10-30 14:25:59 -03:00
Roberto Ierusalimschy
b8b709b6d4 Avoid direct accesses to the array part of a table 2023-10-27 16:32:49 -03:00
Roberto Ierusalimschy
81e4fce530 Simpler test in 'luaH_getint'
The test whether key is inside the array part of a table uses a bit
trick to avoid computing the real size of the array part.
2023-10-26 16:12:25 -03:00
Roberto Ierusalimschy
ab6a949522 Merge branch 'master' into nextversion 2023-06-22 11:41:48 -03:00
Roberto Ierusalimschy
819bd51d87 Some cleaning in the new table API 2023-05-16 16:53:29 -03:00
Roberto Ierusalimschy
f8d30826dd New table API for 'set' functions 2023-05-16 14:55:49 -03:00
Roberto Ierusalimschy
351ccd7332 Towards a new implementation of arrays
The array part of a table wastes too much space, due to padding.
To avoid that, we need to store values in the array as something
different from a TValue. Therefore, the API for table access
should not assume that any value in a table lives in a *TValue.
This commit is the first step to remove that assumption: functions
luaH_get*, instead of returning a *TValue where the value lives,
receive a *TValue where to put the value being accessed.
(We still have to change the luaH_set* functions.)
2023-05-15 17:56:25 -03:00