point2uint for blua interning string cache

This commit is contained in:
alufolie91 committed 2026-09-21 17:02:14 +02:00
1 parent cc9a24e979
commit 6039dae85b
2 files changed
+20 -1

No files matched your search

+19
View File
@@ -41,6 +41,25 @@ typedef unsigned char lu_byte;
*/
#define IntPoint(p) ((unsigned int)(lu_mem)(p))
/*
** conversion of pointer to unsigned integer: this is for hashing only;
** there is no problem if the integer cannot hold the whole pointer
** value. (In strict ISO C this may cause undefined behavior, but no
** actual machine seems to bother.)
*/
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
__STDC_VERSION__ >= 199901L
#include <stdint.h>
#if defined(UINTPTR_MAX) /* even in C99 this type is optional */
#define L_P2I uintptr_t
#else /* no 'intptr'? */
#define L_P2I uintmax_t /* use the largest available integer */
#endif
#else /* C89 option */
#define L_P2I size_t
#endif
#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX))
/* type to ensure maximum alignment */
+1 -1
View File
@@ -191,7 +191,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
** check hits.
*/
TString *luaS_new (lua_State *L, const char *str) {
unsigned int i = IntPoint(str) % STRCACHE_N; /* hash */
unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
int j;
TString **p = G(L)->strcache[i];
for (j = 0; j < STRCACHE_M; j++) {