Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,9 @@ LJLIB_O= lib_base.o lib_math.o lib_bit.o lib_string.o lib_table.o \
LJLIB_C= $(LJLIB_O:.o=.c)

LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \
lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \
lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \
lj_strfmt.o lj_strfmt_num.o lj_api.o lj_profile.o \
lj_str.o lj_str_hash.o lj_tab.o lj_func.o lj_udata.o lj_meta.o \
lj_debug.o lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o \
lj_strscan.o lj_strfmt.o lj_strfmt_num.o lj_api.o lj_profile.o \
lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \
lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \
lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \
Expand Down
9 changes: 9 additions & 0 deletions src/lj_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,13 @@ extern void *LJ_WIN_LOADLIBA(const char *path);
#define LJ_52 0
#endif

/* Optimized string hashing, added by OpenResty. */
#if LUAJIT_TARGET == LUAJIT_ARCH_X64 && defined(__GNUC__) && defined(__SSE4_2__)
#ifndef LJ_OR_DISABLE_STRHASHCRC32
#define LJ_OR_STRHASHCRC32 1
#endif
#else
#define LJ_OR_STRHASHCRC32 0
#endif

#endif
8 changes: 8 additions & 0 deletions src/lj_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ typedef struct SBuf {
MRef L; /* lua_State, used for buffer resizing. */
} SBuf;

#if LJ_OR_STRHASHCRC32
/* String hashing functions, added by OpenResty. */
typedef MSize (*StrHashFunction)(const char *, size_t);
#endif

/* -- Tags and values ----------------------------------------------------- */

/* Frame link. */
Expand Down Expand Up @@ -622,6 +627,9 @@ typedef struct global_State {
MRef saved_jit_base; /* saved jit_base for lj_err_throw */
MRef ctype_state; /* Pointer to C type state. */
GCRef gcroot[GCROOT_MAX]; /* GC roots. */
#if LJ_OR_STRHASHCRC32
StrHashFunction strhashfn; /* String hashing function, added by OpenResty */
#endif
} global_State;

#define mainthread(g) (&gcref(g->mainthref)->th)
Expand Down
3 changes: 3 additions & 0 deletions src/lj_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
setgcref(g->uvhead.prev, obj2gco(&g->uvhead));
setgcref(g->uvhead.next, obj2gco(&g->uvhead));
g->strmask = ~(MSize)0;
#if LJ_OR_STRHASHCRC32
lj_init_strhashfn(g);
#endif
setnilV(registry(L));
setnilV(&g->nilnode.val);
setnilV(&g->nilnode.key);
Expand Down
50 changes: 6 additions & 44 deletions src/lj_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,49 +130,6 @@ void lj_str_resize(lua_State *L, MSize newmask)
g->strhash = newhash;
}

static MSize
lj_str_original_hash(const char *str, size_t lenx)
{
MSize len = (MSize)lenx;
MSize a, b, h = len;

/* Compute string hash. Constants taken from lookup3 hash by Bob Jenkins. */
if (len >= 4) { /* Caveat: unaligned access! */
a = lj_getu32(str);
h ^= lj_getu32(str+len-4);
b = lj_getu32(str+(len>>1)-2);
h ^= b; h -= lj_rol(b, 14);
b += lj_getu32(str+(len>>2)-1);
} else if (len > 0) {
a = *(const uint8_t *)str;
h ^= *(const uint8_t *)(str+len-1);
b = *(const uint8_t *)(str+(len>>1));
h ^= b; h -= lj_rol(b, 14);
} else {
return 0;
}

a ^= h; a -= lj_rol(h, 11);
b ^= a; b -= lj_rol(a, 25);
h ^= b; h -= lj_rol(b, 16);

return h;
}

MSize
lj_str_indep_hash(GCstr *str)
{
return lj_str_original_hash(strdata(str), str->len);
}

#include "x64/src/lj_str_hash_x64.h"

#if defined(LJ_ARCH_STR_HASH)
#define LJ_STR_HASH LJ_ARCH_STR_HASH
#else
#define LJ_STR_HASH lj_str_original_hash
#endif

/* Intern a string and return string object. */
GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
{
Expand All @@ -189,7 +146,12 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
return &g->strempty;
}

h = LJ_STR_HASH(str, lenx);
#if LJ_OR_STRHASHCRC32
lua_assert(g->strhashfn != NULL);
h = g->strhashfn(str, lenx);
#else
h = lj_str_hash_orig(str, lenx);
#endif

/* Check if the string has already been interned. */
o = gcref(g->strhash[h & g->strmask]);
Expand Down
3 changes: 1 addition & 2 deletions src/lj_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdarg.h>

#include "lj_obj.h"
#include "lj_str_hash.h"

/* String helpers. */
LJ_FUNC int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b);
Expand All @@ -24,6 +25,4 @@ LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s);
#define lj_str_newz(L, s) (lj_str_new(L, s, strlen(s)))
#define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1))

MSize lj_str_indep_hash(GCstr *str);

#endif
Loading