Advertisement
funny_falcon

lj_str.diff

Apr 27th, 2016
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 1.99 KB | None | 0 0
  1. diff --git a/src/lj_str.c b/src/lj_str.c
  2. index a6a9364..37d2398 100644
  3. --- a/src/lj_str.c
  4. +++ b/src/lj_str.c
  5. @@ -130,7 +130,19 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
  6.      lj_err_msg(L, LJ_ERR_STROV);
  7.    g = G(L);
  8.    /* Compute string hash. Constants taken from lookup3 hash by Bob Jenkins. */
  9. -  if (len >= 4) {  /* Caveat: unaligned access! */
  10. +  if (len >= 12) {  /* Caveat: unaligned access! */
  11. +    MSize i = (len-1)/8;
  12. +    const char *ss;
  13. +    a = lj_getu32(str+len-8);
  14. +    b = lj_getu32(str+len-4);
  15. +    ss = str;
  16. +    for (; i; i--, ss += 8) {
  17. +      MSize v = lj_getu32(ss), vv = lj_getu32(ss+4);
  18. +      h = lj_rol(h ^ a, 17) + (b ^ 0xdeadbeef);
  19. +      a -= v; a = lj_rol(a, 13);
  20. +      b -= vv; b = lj_rol(b, 11);
  21. +    }
  22. +  } else if (len >= 4) {  /* Caveat: unaligned access! */
  23.      a = lj_getu32(str);
  24.      h ^= lj_getu32(str+len-4);
  25.      b = lj_getu32(str+(len>>1)-2);
  26. @@ -152,7 +164,7 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
  27.    if (LJ_LIKELY((((uintptr_t)str+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4)) {
  28.      while (o != NULL) {
  29.        GCstr *sx = gco2str(o);
  30. -      if (sx->len == len && str_fastcmp(str, strdata(sx), len) == 0) {
  31. +      if (sx->hash == h && sx->len == len && str_fastcmp(str, strdata(sx), len) == 0) {
  32.         /* Resurrect if dead. Can only happen with fixstring() (keywords). */
  33.         if (isdead(g, o)) flipwhite(o);
  34.         return sx;  /* Return existing string. */
  35. @@ -162,7 +174,7 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
  36.    } else {  /* Slow path: end of string is too close to a page boundary. */
  37.      while (o != NULL) {
  38.        GCstr *sx = gco2str(o);
  39. -      if (sx->len == len && memcmp(str, strdata(sx), len) == 0) {
  40. +      if (sx->hash == h && sx->len == len && memcmp(str, strdata(sx), len) == 0) {
  41.         /* Resurrect if dead. Can only happen with fixstring() (keywords). */
  42.         if (isdead(g, o)) flipwhite(o);
  43.         return sx;  /* Return existing string. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement