Your reports is a bit strange, but lets give it a try....
On Sun, 4 May 2025 at 12:14, 最萌 小汐 <
sum...@hotmail.com> wrote:
> I discovered a strange performance issue. Once I added the line "cache[uid] = nil," the time it takes to run this code becomes significantly longer.
You mean EXACTLY THIS CODE? Because time to run a code tends to get
longer as you add lines. If you have a problem with similar but
different code you should probably elaborate a bit more.
> local cache = {}
> for x = 1, 1000 do
> for i = x * 10000, (x + 1) * 10000 do
> local uid = i * 1000 + 100000000
> cache[uid] = 1
> end
>
> -- remove some garbages for some reasons
> for i = 1, 1000 do
> local uid = next(cache)
> if uid then
> cache[uid] = nil -- delete this line to make it faster
> end
> end
> end
This repeats, 1000 times.
1.- Add 10k entries to a table.
2.- Delete the first 1k entries from it.
Note you are not deleting the recently added, but allways searching
from the start of the table. And with a test which in the code is
superflous ( next(cache) is only nil ( it cannot be false there ) on
empty tables.
IIRC lua tables do some kind of chaining of deleted entries ( it is
needed for proper working of some things ), so I suspect your second
loop is creating longer and longer chains of deleted elements and you
are invoking the infamous quadratic behaviour in the deleting loops,
some one more knowledgeable of the internals may confirm or deny that.
> After running for several hours,
I assume a similar but different code....
> I encountered a strange performance issue: the assignment operation for adding temporary data to the table sometimes takes several seconds. Moreover, as the runtime increases, this operation's duration grows longer, eventually taking several minutes. The time taken to traverse the table also increases from around ten milliseconds to several minutes.
You may be building some kind of pathologically bad hash table there.
Lots of pointer chains.
> During this period, the table size remains around 500k. I suspect that the table may be experiencing some erroneous resizing behavior. I have not yet found a way to reproduce this situation with a demonstration code, but I discovered the aforementioned issues while searching. These problems occur in both Lua 5.3 and Lua 5.4.
Is your code really doing that, deleting some kind of cache entries by
always swapping a chunk of the table from the start? Be sure you are
not having a problem but reporting a different one ( your sample does
no make much sense TO ME, you generate keys with an increasing
sequence, this I have seen, but then your "garbage deletion" just
grabs the first "random" key the hash gives you in next(cache)).
Francisco Olarte.