luaS_newlstr: use hash also for checking strings

taken from raptorjit
we can check the hash first to skip quite a bunch of memcmps which should be faster in most cases (hopefully but quick timedemo tests confirmed that)
This commit is contained in:
Alug 2026-02-22 01:06:15 +01:00 committed by NepDisk
parent c582767603
commit ead56b83e3

View file

@ -90,9 +90,7 @@ static unsigned hash_sparse(const char *str, size_t len)
{
/* Constants taken from lookup3 hash by Bob Jenkins. */
unsigned int a, b, h = cast(unsigned int, len);
if (len == 0) return 0;
#define rol(x, n) (((x)<<(n)) | ((x)>>(-cast(int, n)&(8*sizeof(x)-1))))
if (len >= 4) { /* Caveat: unaligned access! */
a = getu32(str);
@ -136,7 +134,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
o != NULL;
o = o->gch.next) {
TString *ts = rawgco2ts(o);
if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
if (ts->tsv.hash == h &&
ts->tsv.len == l &&
(memcmp(str, getstr(ts), l) == 0)) {
/* string may be dead */
if (isdead(G(L), o)) changewhite(o);
return ts;