Subject: [PATCH 7/9] st.c: use array for storing st_table_entry

From: Sokolov Yura aka funny_falcon <[email-masked]>
Date: Mon, 5 Sep 2016 14:53:11 +0300

- instead of allocating st_table_entry per element with malloc,
  store all elements in one array
- use order in the array as an insertion order instead of list
-- after array's last element is filled, hash is rebuilt and grow/shrunk if needed.
- openaddressing used for collision resolution instead of chaining
  and high bits of hash sum are mixed in erly steps
  (proposed and implemented first by Vladimir Makarov)
  (similar to python's dictionary)
- reduce sizeof(st_table_entry) to 24byte on 64bit platform
- reduce sizeof(st_table) to 32bytes (on x86_64) by couple of tricks:
-- alloc bins together with entries, so only 1 pointer needed
-- pack allocated size into 8bit and rebuilt count into 24bit
-- no need to count deleted elements, cause elements array is always
   smaller than bins
-- it limits hash size to ~1.5G elements
-- there is compile time option to use 64bit index to have ability to use huge hashes.
   then sizeof(st_table) is 48byte
- fix test_st_foreach
-- disable test_st_foreach(:unpack_delete) cause it behaves differently
   on already unpacked table (it may lead to segfault in previous st
   implementation)
-- remove checks for packed/unpacked
- bins are dinamically sized:
-- if array's size is less than 255, then bins are 8bit indices
-- if array is less than 65535 elements, then bins are 16bit indices
-- if array is less than 2**32 elements, then bins are 32bit indices
-- (proposed and implemented first by Vladimir Makarov)

Thanks for initiating issue-12142, discussion, ideas and alternative implementation
to Vladimir Makarov <[email-masked]>
I really count him as co-author of this patch.
---
 configure.in                    |   11 +
 ext/-test-/st/foreach/foreach.c |   34 +-
 include/ruby/st.h               |   40 +-
 st.c                            | 1400 ++++++++++++++++-----------------------
 st_4_4.inc                      |   18 +
 st_4_8.inc                      |   28 +
 st_8_8.inc                      |   34 +
 test/-ext-/st/test_foreach.rb   |    1 -
 8 files changed, 662 insertions(+), 904 deletions(-)
 create mode 100644 st_4_4.inc
 create mode 100644 st_4_8.inc
 create mode 100644 st_8_8.inc

diff --git a/configure.in b/configure.in
index 3734afa..22c10d9 100644
--- a/configure.in
+++ b/configure.in
@@ -4534,6 +4534,17 @@ else
 fi
 AC_SUBST(USE_RUBYGEMS)
 
+AC_ARG_ENABLE(hugehash,
+	AS_HELP_STRING([--enable-hugehash], [on 64bit platform enable hashes with more than 2**31 elements]),
+	[enable_hugehash="$enableval"], [enable_hugehash=no])
+if test x"$enable_hugehash" = xyes; then
+    AC_DEFINE(ENABLE_HUGEHASH, 1)
+    HUGEHASH=YES
+else
+    HUGEHASH=NO
+fi
+AC_SUBST(HUGEHASH)
+
 arch_hdrdir="${EXTOUT}/include/${arch}/ruby"
 AS_MKDIR_P("${arch_hdrdir}")
 config_h="${arch_hdrdir}/config.h"
diff --git a/ext/-test-/st/foreach/foreach.c b/ext/-test-/st/foreach/foreach.c
index 1e0fd51..d1d2abf 100644
--- a/ext/-test-/st/foreach/foreach.c
+++ b/ext/-test-/st/foreach/foreach.c
@@ -14,13 +14,9 @@ force_unpack_check(struct checker *c, st_data_t key, st_data_t val)
     if (c->nr == 0) {
 	st_data_t i;
 
-	if (!c->tbl->entries_packed) rb_bug("should be packed\n");
-
 	/* force unpacking during iteration: */
 	for (i = 1; i < expect_size; i++)
 	    st_add_direct(c->tbl, i, i);
-
-	if (c->tbl->entries_packed) rb_bug("should be unpacked\n");
     }
 
     if (key != c->nr) {
@@ -84,8 +80,6 @@ unp_fec(VALUE self, VALUE test)
 
     st_add_direct(tbl, 0, 0);
 
-    if (!tbl->entries_packed) rb_bug("should still be packed\n");
-
     st_foreach_check(tbl, unp_fec_i, (st_data_t)&c, -1);
 
     if (c.test == ID2SYM(rb_intern("delete2"))) {
@@ -98,8 +92,6 @@ unp_fec(VALUE self, VALUE test)
 		(VALUE)c.nr, (VALUE)expect_size);
     }
 
-    if (tbl->entries_packed) rb_bug("should be unpacked\n");
-
     st_free_table(tbl);
 
     return Qnil;
@@ -114,21 +106,6 @@ unp_fe_i(st_data_t key, st_data_t val, st_data_t args, int error)
     if (c->test == ID2SYM(rb_intern("unpacked"))) {
 	return ST_CONTINUE;
     }
-    else if (c->test == ID2SYM(rb_intern("unpack_delete"))) {
-	if (c->nr == 1) {
-	    st_data_t k = 0;
-	    st_data_t v;
-
-	    if (!st_delete(c->tbl, &k, &v)) {
-		rb_bug("failed to delete\n");
-	    }
-	    if (v != 0) {
-		rb_bug("unexpected value deleted: %"PRIuVALUE" (expected 0)", (VALUE)v);
-	    }
-	    return ST_CONTINUE;
-	}
-	rb_bug("should never get here\n");
-    }
 
     rb_raise(rb_eArgError, "unexpected arg: %+"PRIsVALUE, c->test);
 }
@@ -145,22 +122,13 @@ unp_fe(VALUE self, VALUE test)
 
     st_add_direct(tbl, 0, 0);
 
-    if (!tbl->entries_packed) rb_bug("should still be packed\n");
-
     st_foreach(tbl, unp_fe_i, (st_data_t)&c);
 
-    if (c.test == ID2SYM(rb_intern("unpack_delete"))) {
-	if (c.nr != 1) {
-	    rb_bug("mismatched iteration: %"PRIuVALUE" (expected 1)\n", (VALUE)c.nr);
-	}
-    }
-    else if (c.nr != expect_size) {
+    if (c.nr != expect_size) {
 	rb_bug("mismatched iteration: %"PRIuVALUE" (expected %"PRIuVALUE"o)\n",
 		(VALUE)c.nr, (VALUE)expect_size);
     }
 
-    if (tbl->entries_packed) rb_bug("should be unpacked\n");
-
     st_free_table(tbl);
 
     return Qnil;
diff --git a/include/ruby/st.h b/include/ruby/st.h
index 2e2d5a3..8763d9d 100644
--- a/include/ruby/st.h
+++ b/include/ruby/st.h
@@ -52,6 +52,14 @@ typedef st_index_t st_hash_func(st_data_t);
 typedef char st_check_for_sizeof_st_index_t[SIZEOF_VOIDP == (int)sizeof(st_index_t) ? 1 : -1];
 #define SIZEOF_ST_INDEX_T SIZEOF_VOIDP
 
+#if ENABLE_HUGEHASH
+typedef st_index_t st_idx_t;
+#define SIZEOF_ST_IDX_T SIZEOF_VOIDP
+#else /* force 32bit indices and hash sum even on 64bit platform */
+typedef uint32_t st_idx_t;
+#define SIZEOF_ST_IDX_T SIZEOF_UINT32_T
+#endif
+
 struct st_hash_type {
     int (*compare)(ANYARGS /*st_data_t, st_data_t*/); /* st_compare_func* */
     st_index_t (*hash)(ANYARGS /*st_data_t*/);        /* st_hash_func* */
@@ -68,31 +76,17 @@ struct st_hash_type {
 
 struct st_table {
     const struct st_hash_type *type;
-    st_index_t num_bins;
-    unsigned int entries_packed : 1;
-#ifdef __GNUC__
-    /*
-     * C spec says,
-     *   A bit-field shall have a type that is a qualified or unqualified
-     *   version of _Bool, signed int, unsigned int, or some other
-     *   implementation-defined type. It is implementation-defined whether
-     *   atomic types are permitted.
-     * In short, long and long long bit-field are implementation-defined
-     * feature. Therefore we want to suppress a warning explicitly.
-     */
-    __extension__
-#endif
-    st_index_t num_entries : ST_INDEX_BITS - 1;
     union {
-	struct {
-	    struct st_table_entry **bins;
-	    void *private_list_head[2];
-	} big;
-	struct {
-	    struct st_packed_entry *entries;
-	    st_index_t real_entries;
-	} packed;
+	struct st_table_entry* entries;
+	st_idx_t* bins;
+	uint8_t*  bins8;
+	uint16_t* bins16;
+	uint32_t* bins32;
     } as;
+    st_idx_t num_entries;
+    st_idx_t first, last;
+    unsigned sz : 8;
+    st_idx_t rebuild_num : sizeof(st_idx_t)*8-8;
 };
 
 #define st_is_member(table,key) st_lookup((table),(key),(st_data_t *)0)
diff --git a/st.c b/st.c
index f343a12..f48cdf3 100644
--- a/st.c
+++ b/st.c
@@ -5,6 +5,8 @@
 #ifdef NOT_RUBY
 #include "regint.h"
 #include "st.h"
+#define LIKELY(x) (x)
+#define UNLIKELY(x) (x)
 #else
 #include "internal.h"
 #endif
@@ -14,7 +16,21 @@
 #include <stdlib.h>
 #endif
 #include <string.h>
-#include "ccan/list/list.h"
+#include <assert.h>
+
+#ifndef ST_UNUSED
+#if defined(__GNUC__)
+#  define ST_UNUSED  __attribute__((unused))
+#else
+#  define ST_UNUSED
+#endif
+#endif
+
+#ifdef ST_DEBUG
+#  define st_assert assert
+#else
+#  define st_assert(...)
+#endif
 
 typedef struct st_table_entry st_table_entry;
 
@@ -22,28 +38,12 @@ struct st_table_entry {
     st_index_t hash;
     st_data_t key;
     st_data_t record;
-    st_table_entry *next;
-    struct list_node olist;
 };
 
-typedef struct st_packed_entry {
-    st_index_t hash;
-    st_data_t key, val;
-} st_packed_entry;
-
 #ifndef STATIC_ASSERT
 #define STATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[(expr) ? 1 : -1]
 #endif
 
-#define ST_DEFAULT_MAX_DENSITY 5
-#define ST_DEFAULT_INIT_TABLE_SIZE 16
-#define ST_DEFAULT_PACKED_TABLE_SIZE 18
-#define PACKED_UNIT (int)(sizeof(st_packed_entry) / sizeof(st_table_entry*))
-#define MAX_PACKED_HASH (int)(ST_DEFAULT_PACKED_TABLE_SIZE * sizeof(st_table_entry*) / sizeof(st_packed_entry))
-
-STATIC_ASSERT(st_packed_entry, sizeof(st_packed_entry) == sizeof(st_table_entry*[PACKED_UNIT]));
-STATIC_ASSERT(st_packed_bins, sizeof(st_packed_entry[MAX_PACKED_HASH]) <= sizeof(st_table_entry*[ST_DEFAULT_PACKED_TABLE_SIZE]));
-
     /*
      * DEFAULT_MAX_DENSITY is the default for the largest we allow the
      * average number of items per bin before increasing the number of
@@ -73,103 +73,160 @@ static const struct st_hash_type type_strcasehash = {
     strcasehash,
 };
 
-static void rehash(st_table *);
+static void st_rehash(st_table *);
 
 #ifdef RUBY
-#undef malloc
-#undef realloc
-#undef calloc
-#undef free
-#define malloc xmalloc
-#define calloc xcalloc
-#define realloc xrealloc
-#define free(x) xfree(x)
+#define malloc ruby_xmalloc
+#define calloc ruby_xcalloc
+#define sized_realloc(p, new, old) ruby_sized_xrealloc((p), (new), (old))
+#define sized_free(p, sz) ruby_sized_xfree((p), (sz))
+#else
+#error "NOT RUBY"
+#define sized_realloc(p, new, old) realloc((p), (new))
+#define sized_free(p, sz) free(p)
 #endif
 
-#define EQUAL(table,x,ent) ((x)==(ent)->key || (*(table)->type->compare)((x),(ent)->key) == 0)
+#define IDX_NULL (0)
+#define IDX_FILL (0x00)
+#define DELETED (0)
+/* real index */
+#define RI(x) ((x)-1)
+#define Z ((ssize_t)-1)
 
-#define do_hash(key,table) (st_index_t)(*(table)->type->hash)((key))
-#define hash_pos(h,n) ((h) & (n - 1))
+#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
 
 /* preparation for possible allocation improvements */
-#define st_alloc_entry() (st_table_entry *)malloc(sizeof(st_table_entry))
-#define st_free_entry(entry) free(entry)
-#define st_alloc_table() (st_table *)malloc(sizeof(st_table))
-#define st_dealloc_table(table) free(table)
-#define st_alloc_bins(size) (st_table_entry **)calloc(size, sizeof(st_table_entry *))
-#define st_free_bins(bins, size) free(bins)
-static inline st_table_entry**
-st_realloc_bins(st_table_entry **bins, st_index_t newsize, st_index_t oldsize)
-{
-    bins = (st_table_entry **)realloc(bins, newsize * sizeof(st_table_entry *));
-    MEMZERO(bins, st_table_entry*, newsize);
-    return bins;
-}
-
-/* Shortcut */
-#define bins as.big.bins
-#define real_entries as.packed.real_entries
-
-/* preparation for possible packing improvements */
-#define PACKED_BINS(table) ((table)->as.packed.entries)
-#define PACKED_ENT(table, i) PACKED_BINS(table)[i]
-#define PKEY(table, i) PACKED_ENT((table), (i)).key
-#define PVAL(table, i) PACKED_ENT((table), (i)).val
-#define PHASH(table, i) PACKED_ENT((table), (i)).hash
-#define PKEY_SET(table, i, v) (PKEY((table), (i)) = (v))
-#define PVAL_SET(table, i, v) (PVAL((table), (i)) = (v))
-#define PHASH_SET(table, i, v) (PHASH((table), (i)) = (v))
-
-/* this function depends much on packed layout, so that it placed here */
-static inline void
-remove_packed_entry(st_table *table, st_index_t i)
+#define st_alloc_table() (st_table *)calloc(1, sizeof(st_table))
+#define st_dealloc_table(table) sized_free(table, sizeof(st_table))
+
+/* this calculation to satisfy jemalloc/tcmalloc allocation sizes */
+static struct st_sizes {
+    st_idx_t nentries, nbins;
+} const st_sz[] = {
+    { 0, 0 }, { 4, 0 },
+/* include sizes generated by following script:
+   st_idx_t_sz = ARGV[0].to_i
+   st_data_t_sz = ARGV[1].to_i
+   size_t_log = st_data_t_sz < 8 ? 31 : 47 # ptrdiff_t should not overflow
+   entry_sz = 3 * st_data_t_sz
+   def memsizes(log); a = 32; while a+a/2 <= 2**log; yield a; yield a+a/2; a*=2; end; end
+   def pow2(v); [1,2,4,8,16].each{|i| v|=v>>i}; v+1; end
+   sizes = []
+   memsizes(size_t_log) do |v|
+      nentries = v/entry_sz
+      nbins = pow2(nentries)
+      nentries = (v - nbins * (nentries < 255 ? 1 : (nentries < ((1<<16)-1) ? 2 :
+				(nentries < ((1<<32)-1) ? 4 : st_idx_t_sz)))) / entry_sz
+      next if nentries <= 5 || nentries >= (2**(8*st_idx_t_sz)-1) || nbins >= (2**(8*st_idx_t_sz)-1)
+      sizes << [nentries, nbins]
+   end
+   (sizes.size-1).downto(1){|i| if sizes[i][0] * 0.85 < sizes[i-1][0] then sizes.delete_at(i) end }
+   sizes.each_slice(2){|a| s=a.map{|m,k| "{ #{m}, #{k} },"}.join(" "); print "    #{s}\n" }
+*/
+#if SIZEOF_VOIDP == 8 && SIZEOF_ST_IDX_T == 4
+#   include "st_4_8.inc"
+#elif SIZEOF_VOIDP < 8
+#   include "st_4_4.inc"
+#else
+#   include "st_8_8.inc"
+#endif
+    { 0, 0 }
+};
+
+static inline int
+need_shrink(st_idx_t num_entries, int sz)
 {
-    table->real_entries--;
-    table->num_entries--;
-    if (i < table->real_entries) {
-	MEMMOVE(&PACKED_ENT(table, i), &PACKED_ENT(table, i+1),
-		st_packed_entry, table->real_entries - i);
-    }
+    st_idx_t nen = st_sz[sz].nentries;
+    return sz > 1 && num_entries < nen / 2;
 }
 
-static inline void
-remove_safe_packed_entry(st_table *table, st_index_t i, st_data_t never)
+static inline st_index_t
+do_hash(st_data_t key, const st_table * table)
 {
-    table->num_entries--;
-    PKEY_SET(table, i, never);
-    PVAL_SET(table, i, never);
-    PHASH_SET(table, i, 0);
+    st_index_t h = (*table->type->hash)(key);
+    return h != DELETED ? h : 0x71fe900d;
 }
 
-static st_index_t
-next_pow2(st_index_t x)
-{
-    x |= x >> 1;
-    x |= x >> 2;
-    x |= x >> 4;
-    x |= x >> 8;
-    x |= x >> 16;
-#if SIZEOF_ST_INDEX_T == 8
-    x |= x >> 32;
+static inline ssize_t
+bin_size(int sz)
+{
+    st_idx_t nen = st_sz[sz].nentries;
+#if SIZEOF_ST_IDX_T > 4
+    return nen < 255 ? 1 : (nen < 65535 ? 2 : (nen < 0xffffffff ? 4 : sizeof(st_idx_t)));
+#else
+    return nen < 255 ? 1 : (nen < 65535 ? 2 : sizeof(st_idx_t));
 #endif
-    return x + 1;
 }
 
-static st_index_t
-new_size(st_index_t size)
+static inline ssize_t
+bins_size(int sz)
 {
-    st_index_t n;
+    return (ssize_t)st_sz[sz].nbins * bin_size(sz);
+}
 
-    if (size && (size & ~(size - 1)) == size) /* already a power-of-two? */
-	return size;
+static inline size_t
+entries_size(int sz)
+{
+    return bins_size(sz) + st_sz[sz].nentries*sizeof(st_table_entry);
+}
 
-    n = next_pow2(size);
-    if (n > size)
-	return n;
-#ifndef NOT_RUBY
-    rb_raise(rb_eRuntimeError, "st_table too big");
+static inline char*
+base_ptr(st_table_entry* e, int sz)
+{
+    return (char*)e - bins_size(sz);
+}
+
+static inline st_table_entry*
+entries_ptr(char* e, int sz)
+{
+    return (st_table_entry*)(e + bins_size(sz));
+}
+
+static st_idx_t fake_bins[2] = {IDX_FILL , IDX_FILL };
+
+static inline void
+st_free_data(st_table_entry* entries, int sz)
+{
+    if (sz != 0) {
+	sized_free(base_ptr(entries, sz), entries_size(sz));
+    }
+}
+
+static inline st_table_entry*
+st_grow_data(st_table_entry *e, int newsz, int oldsz)
+{
+    if (oldsz == 0) {
+	if (newsz != 0) {
+	    char* bins = calloc(1, entries_size(newsz));
+#ifdef NOTRUBY
+	    if (bins == NULL) abort();
 #endif
-    return -1;			/* should raise exception */
+	    memset(bins, IDX_FILL, bins_size(newsz));
+	    return entries_ptr(bins, newsz);
+	} else {
+	    return (st_table_entry*)(fake_bins + 1);
+	}
+    } else if (newsz == 0) {
+	sized_free(base_ptr(e, oldsz), entries_size(oldsz));
+	return (st_table_entry*)(fake_bins + 1);
+    } else {
+	char* vbins = base_ptr(e, oldsz);
+	ssize_t bins_old = bins_size(oldsz);
+	ssize_t bins_new = bins_size(newsz);
+	if (bins_old > bins_new) {
+	    MEMMOVE(vbins + bins_new, vbins + bins_old, st_table_entry, st_sz[newsz].nentries);
+	    memset(vbins, IDX_FILL, bins_new);
+	}
+	vbins = sized_realloc(vbins, entries_size(newsz), entries_size(oldsz));
+#ifdef NOTRUBY
+	if (bins == NULL) abort();
+#endif
+	if (bins_old < bins_new) {
+	    MEMMOVE(vbins + bins_new, vbins + bins_old, st_table_entry, st_sz[oldsz].nentries);
+	    memset(vbins, IDX_FILL, bins_new);
+	}
+	return entries_ptr(vbins, newsz);
+    }
 }
 
 #ifdef HASH_LOG
@@ -178,6 +235,7 @@ new_size(st_index_t size)
 #endif
 static struct {
     int all, total, num, str, strcase;
+    int max;
 }  collision;
 static int init_st = 0;
 
@@ -195,11 +253,22 @@ stat_col(void)
 }
 #endif
 
-static struct list_head *
-st_head(const st_table *tbl)
+static int
+new_sz(st_index_t size)
 {
-    uintptr_t addr = (uintptr_t)&tbl->as.big.private_list_head;
-    return (struct list_head *)addr;
+    int i;
+    for (i = 1; st_sz[i].nentries != 0; i++)
+        if (st_sz[i].nentries >= size)
+            return i;
+#ifndef NOT_RUBY
+    rb_raise(rb_eRuntimeError, "st_table too big");
+#else
+    {
+	    int max = i-1;
+	    assert(size <= st_sz[max].nentries);
+    }
+#endif
+    return -1;			/* should raise exception */
 }
 
 st_table*
@@ -220,21 +289,14 @@ st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
     }
 #endif
 
-
     tbl = st_alloc_table();
     tbl->type = type;
     tbl->num_entries = 0;
-    tbl->entries_packed = size <= MAX_PACKED_HASH;
-    if (tbl->entries_packed) {
-	size = ST_DEFAULT_PACKED_TABLE_SIZE;
-	tbl->real_entries = 0;
-    }
-    else {
-	size = new_size(size);	/* round up to power-of-two */
-	list_head_init(st_head(tbl));
-    }
-    tbl->num_bins = size;
-    tbl->bins = st_alloc_bins(size);
+    tbl->sz = new_sz(size);	/* round up to power-of-two */
+    tbl->as.entries = st_grow_data(NULL, tbl->sz, 0);
+    tbl->rebuild_num = 0;
+    tbl->first = 0;
+    tbl->last = 0;
 
     return tbl;
 }
@@ -284,44 +346,63 @@ st_init_strcasetable_with_size(st_index_t size)
 void
 st_clear(st_table *table)
 {
-    register st_table_entry *ptr = 0, *next;
-
-    if (table->entries_packed) {
-        table->num_entries = 0;
-        table->real_entries = 0;
-        return;
-    }
-
-    list_for_each_safe(st_head(table), ptr, next, olist) {
-	/* list_del is not needed */
-	st_free_entry(ptr);
-    }
+    st_free_data(table->as.entries, table->sz);
+    table->sz = 0;
     table->num_entries = 0;
-    MEMZERO(table->bins, st_table_entry*, table->num_bins);
-    list_head_init(st_head(table));
+    table->as.bins = &fake_bins[1];
+    table->rebuild_num++;
+    table->first = 0;
+    table->last = 0;
 }
 
 void
 st_free_table(st_table *table)
 {
     st_clear(table);
-    st_free_bins(table->bins, table->num_bins);
     st_dealloc_table(table);
 }
 
 size_t
 st_memsize(const st_table *table)
 {
-    if (table->entries_packed) {
-	return table->num_bins * sizeof (void *) + sizeof(st_table);
-    }
-    else {
-	return table->num_entries * sizeof(struct st_table_entry) + table->num_bins * sizeof (void *) + sizeof(st_table);
+    return sizeof(st_table) + entries_size(table->sz);
+}
+
+static inline st_idx_t
+bin_get(const void *bins, int sz, st_idx_t bin_pos)
+{
+    st_idx_t idx;
+    st_idx_t nentries = st_sz[sz].nentries;
+    if (LIKELY(nentries < 255)) {
+	idx = ((uint8_t*)bins)[Z-bin_pos];
+    } else if (nentries < 65535) {
+	idx = ((uint16_t*)bins)[Z-bin_pos];
+#if SIZEOF_ST_IDX_T > 4
+    } else if (nentries < ((st_idx_t)1 << 32)-1) {
+	idx = ((uint32_t*)bins)[Z-bin_pos];
+#endif
+    } else {
+	idx = ((st_idx_t*)bins)[Z-bin_pos];
     }
+    return idx;
 }
 
-#define PTR_NOT_EQUAL(table, ptr, hash_val, key) \
-((ptr) != 0 && ((ptr)->hash != (hash_val) || !EQUAL((table), (key), (ptr))))
+static inline void
+bin_set(const void *bins, int sz, st_idx_t bin_pos, st_idx_t idx)
+{
+    st_idx_t nentries = st_sz[sz].nentries;
+    if (LIKELY(nentries < 255)) {
+	((uint8_t*)bins)[Z-bin_pos] = (uint8_t)idx;
+    } else if (LIKELY(nentries < 65535)) {
+	((uint16_t*)bins)[Z-bin_pos] = (uint16_t)idx;
+#if SIZEOF_ST_IDX_T > 4
+    } else if (LIKELY(nentries < ((st_idx_t)1 << 32)-1)) {
+	((uint32_t*)bins)[Z-bin_pos] = (uint32_t)idx;
+#endif
+    } else {
+	((st_idx_t*)bins)[Z-bin_pos] = idx;
+    }
+}
 
 #ifdef HASH_LOG
 static void
@@ -340,211 +421,170 @@ count_collision(const struct st_hash_type *type)
 }
 #define COLLISION (collision_check ? count_collision(table->type) : (void)0)
 #define FOUND_ENTRY (collision_check ? collision.total++ : (void)0)
-#define collision_check 0
+#define collision_check 1
 #else
 #define COLLISION
 #define FOUND_ENTRY
 #endif
 
-#define FIND_ENTRY(table, ptr, hash_val, bin_pos) \
-    ((ptr) = find_entry((table), key, (hash_val), ((bin_pos) = hash_pos(hash_val, (table)->num_bins))))
+static inline int
+EQUAL(const st_table *table, st_data_t key, const st_table_entry* ptr) {
+    return key == ptr->key || (*table->type->compare)(key, ptr->key) == 0;
+}
+
+/* Find Loop */
+#define FL_INIT(hash) st_idx_t flpos = (st_idx_t)(hash), fldlt = 1; \
+		      st_index_t flmix = ROTL((hash), 17)
+#define FL_INC() (flpos += fldlt++), (fldlt += (st_idx_t)(flmix >>= 7))
+#define FL_POS(table) ((flpos) & (st_sz[(table)->sz].nbins-1))
 
-static st_table_entry *
-find_entry(const st_table *table, st_data_t key, st_index_t hash_val,
-           st_index_t bin_pos)
+static inline st_table_entry*
+find_entry(const st_table *table, st_data_t key, st_index_t hash_val)
 {
-    register st_table_entry *ptr = table->bins[bin_pos];
-    FOUND_ENTRY;
-    if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {
-	COLLISION;
-	while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {
-	    ptr = ptr->next;
+    st_table_entry* ptr;
+    if (table->sz <= 1) {
+	st_table_entry *end = table->as.entries + table->last;
+	ptr = table->as.entries + table->first;
+	for (; ptr < end; ptr++)
+	    if (ptr->hash == hash_val && EQUAL(table, key, ptr))
+		return ptr;
+	return NULL;
+    } else {
+	const void* bins = table->as.bins;
+	st_table_entry* entries = table->as.entries - 1;
+	int sz = table->sz;
+	FL_INIT(hash_val);
+	FOUND_ENTRY;
+	for (;;FL_INC()) {
+	    st_idx_t idx = bin_get(bins, sz, FL_POS(table));
+	    if (idx == IDX_NULL) break;
+	    ptr = entries + idx;
+	    if (ptr->hash == hash_val && EQUAL(table, key, ptr))
+		return ptr;
+	    COLLISION;
 	}
-	ptr = ptr->next;
+	return NULL;
     }
-    return ptr;
 }
 
-static inline st_index_t
-find_packed_index_from(const st_table *table, st_index_t hash_val,
-		       st_data_t key, st_index_t i)
+static st_table_entry*
+find_exact_key(const st_table *table, st_data_t key, st_index_t hash_val)
 {
-    while (i < table->real_entries &&
-	   (PHASH(table, i) != hash_val || !EQUAL(table, key, &PACKED_ENT(table, i)))) {
-	i++;
+    st_table_entry* ptr;
+    if (table->sz <= 1) {
+	st_table_entry *end = table->as.entries + table->last;
+	ptr = table->as.entries + table->first;
+	for (; ptr < end; ptr++) {
+	    if (ptr->hash == hash_val && ptr->key == key)
+		return ptr;
+	}
+	return NULL;
+    } else {
+	const void* bins = table->as.bins;
+	st_table_entry* entries = table->as.entries - 1;
+	int sz = table->sz;
+	FL_INIT(hash_val);
+	for (;;FL_INC()) {
+	    st_idx_t idx = bin_get(bins, sz, FL_POS(table));
+	    if (idx == IDX_NULL) break;
+	    ptr = entries + idx;
+	    if (ptr->hash == hash_val && ptr->key == key)
+		return ptr;
+	}
+	return NULL;
     }
-    return i;
 }
 
-static inline st_index_t
-find_packed_index(const st_table *table, st_index_t hash_val, st_data_t key)
+static inline void
+insert_entry_simple(st_table* table, st_index_t hash_val, st_idx_t idx)
 {
-    return find_packed_index_from(table, hash_val, key, 0);
+    const void* bins = table->as.bins;
+    int sz = table->sz;
+    st_idx_t bin_pos;
+    FL_INIT(hash_val);
+    for (;;FL_INC()) {
+	bin_pos = FL_POS(table);
+	if (bin_get(bins, sz, bin_pos) == IDX_NULL)
+	    break;
+    }
+    bin_set(bins, sz, bin_pos, idx+1);
 }
 
-int
-st_lookup(st_table *table, register st_data_t key, st_data_t *value)
+static inline void
+insert_entry_reuse(st_table* table, st_index_t hash_val, st_idx_t idx)
 {
-    st_index_t hash_val;
-    register st_table_entry *ptr;
-
-    hash_val = do_hash(key, table);
-
-    if (table->entries_packed) {
-	st_index_t i = find_packed_index(table, hash_val, key);
-	if (i < table->real_entries) {
-	    if (value != 0) *value = PVAL(table, i);
-	    return 1;
-	}
-        return 0;
+    const void* bins = table->as.bins;
+    const st_table_entry* entries = table->as.entries;
+    int sz = table->sz;
+    st_idx_t bin_pos, old;
+    FL_INIT(hash_val);
+    for (;;FL_INC()) {
+	bin_pos = FL_POS(table);
+	old = bin_get(bins, sz, bin_pos);
+	if (old == IDX_NULL || entries[RI(old)].hash == DELETED)
+	    break;
     }
+    bin_set(bins, sz, bin_pos, idx+1);
+}
 
-    ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins));
+int
+st_lookup(st_table *table, register st_data_t key, st_data_t *value)
+{
+    st_table_entry* ptr = find_entry(table, key, do_hash(key, table));
 
-    if (ptr == 0) {
-	return 0;
-    }
-    else {
+    if (ptr != NULL) {
 	if (value != 0) *value = ptr->record;
 	return 1;
     }
+    return 0;
 }
 
 int
 st_get_key(st_table *table, register st_data_t key, st_data_t *result)
 {
-    st_index_t hash_val;
-    register st_table_entry *ptr;
-
-    hash_val = do_hash(key, table);
-
-    if (table->entries_packed) {
-	st_index_t i = find_packed_index(table, hash_val, key);
-	if (i < table->real_entries) {
-	    if (result != 0) *result = PKEY(table, i);
-	    return 1;
-	}
-        return 0;
-    }
+    st_table_entry* ptr = find_entry(table, key, do_hash(key, table));
 
-    ptr = find_entry(table, key, hash_val, hash_pos(hash_val, table->num_bins));
-
-    if (ptr == 0) {
-	return 0;
-    }
-    else {
-	if (result != 0)  *result = ptr->key;
+    if (ptr != NULL) {
+	if (result != 0) *result = ptr->key;
 	return 1;
     }
-}
-
-static inline st_table_entry *
-new_entry(st_table * table, st_data_t key, st_data_t value,
-	st_index_t hash_val, register st_index_t bin_pos)
-{
-    register st_table_entry *entry = st_alloc_entry();
-
-    entry->next = table->bins[bin_pos];
-    table->bins[bin_pos] = entry;
-    entry->hash = hash_val;
-    entry->key = key;
-    entry->record = value;
-
-    return entry;
+    return 0;
 }
 
 static inline void
 add_direct(st_table *table, st_data_t key, st_data_t value,
-	   st_index_t hash_val, register st_index_t bin_pos)
+	   st_index_t hash_val)
 {
     register st_table_entry *entry;
-    if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) {
-	rehash(table);
-        bin_pos = hash_pos(hash_val, table->num_bins);
+    st_idx_t en_idx;
+    if (table->last == st_sz[table->sz].nentries) {
+        st_rehash(table);
     }
 
-    entry = new_entry(table, key, value, hash_val, bin_pos);
-    list_add_tail(st_head(table), &entry->olist);
+    en_idx = table->last;
+    table->last++;
     table->num_entries++;
-}
-
-static void
-unpack_entries(register st_table *table)
-{
-    st_index_t i;
-    st_packed_entry packed_bins[MAX_PACKED_HASH];
-    register st_table_entry *entry;
-    st_table tmp_table = *table;
-
-    MEMCPY(packed_bins, PACKED_BINS(table), st_packed_entry, MAX_PACKED_HASH);
-    table->as.packed.entries = packed_bins;
-    tmp_table.entries_packed = 0;
-#if ST_DEFAULT_INIT_TABLE_SIZE == ST_DEFAULT_PACKED_TABLE_SIZE
-    MEMZERO(tmp_table.bins, st_table_entry*, tmp_table.num_bins);
-#else
-    tmp_table.bins = st_realloc_bins(tmp_table.bins, ST_DEFAULT_INIT_TABLE_SIZE, tmp_table.num_bins);
-    tmp_table.num_bins = ST_DEFAULT_INIT_TABLE_SIZE;
-#endif
-
-    /*
-     * order is important here, we need to keep the original table
-     * walkable during GC (GC may be triggered by new_entry call)
-     */
-    i = 0;
-    list_head_init(st_head(&tmp_table));
-    do {
-	st_data_t key = packed_bins[i].key;
-	st_data_t val = packed_bins[i].val;
-	st_index_t hash = packed_bins[i].hash;
-	entry = new_entry(&tmp_table, key, val, hash,
-			  hash_pos(hash, ST_DEFAULT_INIT_TABLE_SIZE));
-	list_add_tail(st_head(&tmp_table), &entry->olist);
-    } while (++i < MAX_PACKED_HASH);
-    *table = tmp_table;
-    list_head_init(st_head(table));
-    list_append_list(st_head(table), st_head(&tmp_table));
-}
-
-static void
-add_packed_direct(st_table *table, st_data_t key, st_data_t value, st_index_t hash_val)
-{
-    if (table->real_entries < MAX_PACKED_HASH) {
-	st_index_t i = table->real_entries++;
-	PKEY_SET(table, i, key);
-	PVAL_SET(table, i, value);
-	PHASH_SET(table, i, hash_val);
-	table->num_entries++;
-    }
-    else {
-	unpack_entries(table);
-	add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
+    entry = &table->as.entries[en_idx];
+    entry->hash = hash_val;
+    entry->key = key;
+    entry->record = value;
+    if (st_sz[table->sz].nbins != 0) {
+	if (table->num_entries == table->last) {
+	    insert_entry_simple(table, hash_val, en_idx);
+	} else {
+	    insert_entry_reuse(table, hash_val, en_idx);
+	}
     }
 }
 
-
 int
 st_insert(register st_table *table, register st_data_t key, st_data_t value)
 {
-    st_index_t hash_val;
-    register st_index_t bin_pos;
-    register st_table_entry *ptr;
-
-    hash_val = do_hash(key, table);
+    st_index_t hash_val = do_hash(key, table);
+    st_table_entry* ptr = find_entry(table, key, hash_val);
 
-    if (table->entries_packed) {
-	st_index_t i = find_packed_index(table, hash_val, key);
-	if (i < table->real_entries) {
-	    PVAL_SET(table, i, value);
-	    return 1;
-        }
-	add_packed_direct(table, key, value, hash_val);
-	return 0;
-    }
-
-    FIND_ENTRY(table, ptr, hash_val, bin_pos);
-
-    if (ptr == 0) {
-	add_direct(table, key, value, hash_val, bin_pos);
+    if (ptr == NULL) {
+	add_direct(table, key, value, hash_val);
 	return 0;
     }
     else {
@@ -558,27 +598,16 @@ st_insert2(register st_table *table, register st_data_t key, st_data_t value,
 	   st_data_t (*func)(st_data_t))
 {
     st_index_t hash_val;
-    register st_index_t bin_pos;
-    register st_table_entry *ptr;
+    st_table_entry* ptr;
+    st_idx_t rebuild_num ST_UNUSED = table->rebuild_num;
 
     hash_val = do_hash(key, table);
+    ptr = find_entry(table, key, hash_val);
 
-    if (table->entries_packed) {
-	st_index_t i = find_packed_index(table, hash_val, key);
-	if (i < table->real_entries) {
-	    PVAL_SET(table, i, value);
-	    return 1;
-	}
-	key = (*func)(key);
-	add_packed_direct(table, key, value, hash_val);
-	return 0;
-    }
-
-    FIND_ENTRY(table, ptr, hash_val, bin_pos);
-
-    if (ptr == 0) {
+    if (ptr == NULL) {
 	key = (*func)(key);
-	add_direct(table, key, value, hash_val, bin_pos);
+	st_assert(rebuild_num == table->rebuild_num);
+	add_direct(table, key, value, hash_val);
 	return 0;
     }
     else {
@@ -590,109 +619,160 @@ st_insert2(register st_table *table, register st_data_t key, st_data_t value,
 void
 st_add_direct(st_table *table, st_data_t key, st_data_t value)
 {
-    st_index_t hash_val;
+    add_direct(table, key, value, do_hash(key, table));
+}
 
-    hash_val = do_hash(key, table);
-    if (table->entries_packed) {
-	add_packed_direct(table, key, value, hash_val);
-	return;
+#ifdef ST_DEBUG
+static void
+st_check_table(st_table *table) {
+    if (st_sz[table->sz].nbins > 0) {
+	st_idx_t idx = table->first;
+	st_table_entry* ptr = table->as.entries + table->first;
+	st_table_entry* end = table->as.entries + table->last;
+	for (; ptr != end; idx++, ptr++) {
+	    if (ptr->hash != DELETED) {
+		FL_INIT(ptr->hash);
+		for (;;FL_INC()) {
+		    st_idx_t cidx = bin_get(table, FL_POS(table));
+		    if (RI(cidx) == idx) break;
+		    assert(cidx != IDX_NULL);
+		}
+	    }
+	}
     }
-
-    add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
 }
+#else
+#define st_check_table(x)
+#endif
 
 static void
-rehash(register st_table *table)
+st_reclaim_without_bins(register st_table *table)
 {
-    register st_table_entry *ptr = 0, **new_bins;
-    st_index_t new_num_bins, hash_val;
+    st_idx_t i;
+    st_table_entry *ptr, *optr;
+    
+    ptr = table->as.entries;
+    optr = &table->as.entries[table->first];
+    for (i = table->last - table->first; i; i--, optr++) {
+	if (optr->hash != DELETED) {
+	    if (ptr != optr) *ptr = *optr;
+	    ptr++;
+	}
+    }
 
-    new_num_bins = new_size(table->num_bins+1);
-    new_bins = st_realloc_bins(table->bins, new_num_bins, table->num_bins);
-    table->num_bins = new_num_bins;
-    table->bins = new_bins;
+    table->first = 0;
+    table->last = (st_idx_t)(ptr - table->as.entries); /* no overflow here cause size is checked */
+}
 
-    list_for_each(st_head(table), ptr, olist) {
-	hash_val = hash_pos(ptr->hash, new_num_bins);
-	ptr->next = new_bins[hash_val];
-	new_bins[hash_val] = ptr;
+static void
+st_fix_bins(st_table *table)
+{
+    if (st_sz[table->sz].nbins != 0) {
+	st_idx_t i;
+	st_table_entry* ptr;
+
+	memset(base_ptr(table->as.entries, table->sz), IDX_FILL, bins_size(table->sz));
+	ptr = table->as.entries + table->first;
+	for (i = table->first; i < table->last; i++, ptr++) {
+	    if (ptr->hash != DELETED) {
+		insert_entry_simple(table, ptr->hash, i);
+	    }
+	}
+	st_check_table(table);
     }
 }
 
-st_table*
-st_copy(st_table *old_table)
+static void
+st_shrink(st_table *table)
 {
-    st_table *new_table;
-    st_table_entry *ptr = 0, *entry;
-    st_index_t num_bins = old_table->num_bins;
+    unsigned sz = table->sz;
+    while (need_shrink(table->num_entries, sz))
+	sz--;
+    sz++;
+    table->as.entries = st_grow_data(table->as.entries, sz, table->sz);
+    table->sz = sz;
+}
 
-    new_table = st_alloc_table();
-    if (new_table == 0) {
-	return 0;
+static void
+st_rehash(register st_table *table)
+{
+    table->rebuild_num++;
+    if (need_shrink(table->num_entries, table->sz)) {
+	st_reclaim_without_bins(table);
+	if (need_shrink(table->num_entries, table->sz-1)) {
+	    st_shrink(table);
+	}
+	if (st_sz[table->sz].nbins != 0) {
+	    st_fix_bins(table);
+	}
+	return;
     }
 
-    *new_table = *old_table;
-    new_table->bins = st_alloc_bins(num_bins);
-
-    if (new_table->bins == 0) {
-	st_dealloc_table(new_table);
-	return 0;
+    /* else grow table */
+    if (st_sz[table->sz + 1].nentries == 0) {
+#ifndef NOT_RUBY
+	rb_raise(rb_eRuntimeError, "hash is too big");
+#else
+	abort();
+#endif
     }
 
-    if (old_table->entries_packed) {
-        MEMCPY(new_table->bins, old_table->bins, st_table_entry*, old_table->num_bins);
-        return new_table;
+    table->as.entries = st_grow_data(table->as.entries, table->sz+1, table->sz);
+    table->sz++;
+
+    if (bins_size(table->sz) != bins_size(table->sz-1)) {
+	st_fix_bins(table);
     }
+}
 
-    list_head_init(st_head(new_table));
+st_table*
+st_copy(st_table *old_table)
+{
+    st_table *new_table;
 
-    list_for_each(st_head(old_table), ptr, olist) {
-	entry = new_entry(new_table, ptr->key, ptr->record, ptr->hash,
-			    hash_pos(ptr->hash, num_bins));
-	list_add_tail(st_head(new_table), &entry->olist);
+    new_table = st_alloc_table();
+    *new_table = *old_table;
+
+    if (old_table->as.entries) {
+	new_table->as.entries = malloc(entries_size(new_table->sz));
+	memcpy(new_table->as.entries, base_ptr(old_table->as.entries, old_table->sz),
+		entries_size(new_table->sz));
+	new_table->as.entries = entries_ptr((char*)new_table->as.entries, new_table->sz);
     }
 
     return new_table;
 }
 
 static inline void
-remove_entry(st_table *table, st_table_entry *ptr)
+remove_entry(st_table *table, st_table_entry* ptr)
 {
-    list_del(&ptr->olist);
     table->num_entries--;
+    ptr->hash = DELETED;
+    ptr->key = ptr->record = 0;
+    if (table->as.entries + table->first == ptr) {
+	do {
+	    table->first++;
+	    ptr++;
+	} while (table->first < table->last && ptr->hash == DELETED);
+    }
 }
 
 int
 st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
 {
     st_index_t hash_val;
-    st_table_entry **prev;
     register st_table_entry *ptr;
+    st_idx_t rebuild_num ST_UNUSED = table->rebuild_num;
 
     hash_val = do_hash(*key, table);
+    ptr = find_entry(table, *key, hash_val);
 
-    if (table->entries_packed) {
-	st_index_t i = find_packed_index(table, hash_val, *key);
-	if (i < table->real_entries) {
-	    if (value != 0) *value = PVAL(table, i);
-	    *key = PKEY(table, i);
-	    remove_packed_entry(table, i);
-	    return 1;
-        }
-        if (value != 0) *value = 0;
-        return 0;
-    }
-
-    prev = &table->bins[hash_pos(hash_val, table->num_bins)];
-    for (;(ptr = *prev) != 0; prev = &ptr->next) {
-	if (EQUAL(table, *key, ptr)) {
-	    *prev = ptr->next;
-	    remove_entry(table, ptr);
-	    if (value != 0) *value = ptr->record;
-	    *key = ptr->key;
-	    st_free_entry(ptr);
-	    return 1;
-	}
+    if (ptr != NULL) {
+	if (value != 0) *value = ptr->record;
+	*key = ptr->key;
+	remove_entry(table, ptr);
+	st_assert(rebuild_num == table->rebuild_num);
+	return 1;
     }
 
     if (value != 0) *value = 0;
@@ -700,270 +780,135 @@ st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
 }
 
 int
-st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *value, st_data_t never)
+st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *value, st_data_t never ST_UNUSED)
 {
-    st_index_t hash_val;
-    register st_table_entry *ptr;
-
-    hash_val = do_hash(*key, table);
-
-    if (table->entries_packed) {
-	st_index_t i = find_packed_index(table, hash_val, *key);
-	if (i < table->real_entries) {
-	    if (value != 0) *value = PVAL(table, i);
-	    *key = PKEY(table, i);
-	    remove_safe_packed_entry(table, i, never);
-	    return 1;
-	}
-	if (value != 0) *value = 0;
-	return 0;
-    }
-
-    ptr = table->bins[hash_pos(hash_val, table->num_bins)];
-
-    for (; ptr != 0; ptr = ptr->next) {
-	if ((ptr->key != never) && EQUAL(table, *key, ptr)) {
-	    remove_entry(table, ptr);
-	    *key = ptr->key;
-	    if (value != 0) *value = ptr->record;
-	    ptr->key = ptr->record = never;
-	    return 1;
-	}
-    }
-
-    if (value != 0) *value = 0;
-    return 0;
+    return st_delete(table, key, value);
 }
 
 int
 st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
 {
-    st_table_entry *old;
-    st_table_entry **prev;
-    register st_table_entry *ptr;
+    st_table_entry *ptr;
+    st_idx_t idx;
 
     if (table->num_entries == 0) {
         if (value != 0) *value = 0;
         return 0;
     }
 
-    if (table->entries_packed) {
-        if (value != 0) *value = PVAL(table, 0);
-        *key = PKEY(table, 0);
-        remove_packed_entry(table, 0);
-        return 1;
-    }
-
-    old = list_pop(st_head(table), st_table_entry, olist);
-    table->num_entries--;
-    prev = &table->bins[hash_pos(old->hash, table->num_bins)];
-    while ((ptr = *prev) != old) prev = &ptr->next;
-    *prev = ptr->next;
+    idx = table->first;
+    ptr = &table->as.entries[idx];
+    st_assert(ptr->hash != DELETED);
     if (value != 0) *value = ptr->record;
     *key = ptr->key;
-    st_free_entry(ptr);
+    remove_entry(table, ptr);
     return 1;
 }
 
 void
-st_cleanup_safe(st_table *table, st_data_t never)
+st_cleanup_safe(st_table *table, st_data_t never ST_UNUSED)
 {
-    st_table_entry *ptr, **last, *tmp;
-    st_index_t i;
-
-    if (table->entries_packed) {
-	st_index_t i = 0, j = 0;
-	while (PKEY(table, i) != never) {
-	    if (i++ == table->real_entries) return;
-	}
-	for (j = i; ++i < table->real_entries;) {
-	    if (PKEY(table, i) == never) continue;
-	    PACKED_ENT(table, j) = PACKED_ENT(table, i);
-	    j++;
-	}
-	table->real_entries = j;
-	/* table->num_entries really should be equal j at this moment, but let set it anyway */
-	table->num_entries = j;
+    st_idx_t head;
+    if (table->sz == 0)
+	return;
+    if (need_shrink(table->num_entries, table->sz-1)) {
+	st_rehash(table);
 	return;
     }
-
-    for (i = 0; i < table->num_bins; i++) {
-	ptr = *(last = &table->bins[i]);
-	while (ptr != 0) {
-	    if (ptr->key == never) {
-		tmp = ptr;
-		*last = ptr = ptr->next;
-		st_free_entry(tmp);
-	    }
-	    else {
-		ptr = *(last = &ptr->next);
-	    }
-	}
-    }
+    head = table->first;
+    while (head < table->last && table->as.entries[head].hash == DELETED)
+	head++;
+    table->first = head;
 }
 
 int
 st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
 {
-    st_index_t hash_val, bin_pos;
-    register st_table_entry *ptr, **last, *tmp;
+    st_index_t hash_val;
+    register st_table_entry *ptr;
+    st_idx_t rebuild_num ST_UNUSED = table->rebuild_num;
     st_data_t value = 0, old_key;
     int retval, existing = 0;
 
     hash_val = do_hash(key, table);
+    ptr = find_entry(table, key, hash_val);
 
-    if (table->entries_packed) {
-	st_index_t i = find_packed_index(table, hash_val, key);
-	if (i < table->real_entries) {
-	    key = PKEY(table, i);
-	    value = PVAL(table, i);
-	    existing = 1;
-	}
-	{
-	    old_key = key;
-	    retval = (*func)(&key, &value, arg, existing);
-	    if (!table->entries_packed) {
-		FIND_ENTRY(table, ptr, hash_val, bin_pos);
-		goto unpacked;
-	    }
-	    switch (retval) {
-	      case ST_CONTINUE:
-		if (!existing) {
-		    add_packed_direct(table, key, value, hash_val);
-		    break;
-		}
-		if (old_key != key) {
-		    PKEY(table, i) = key;
-		}
-		PVAL_SET(table, i, value);
-		break;
-	      case ST_DELETE:
-		if (!existing) break;
-		remove_packed_entry(table, i);
-	    }
-	}
-	return existing;
-    }
-
-    FIND_ENTRY(table, ptr, hash_val, bin_pos);
-
-    if (ptr != 0) {
+    if (ptr != NULL) {
 	key = ptr->key;
 	value = ptr->record;
 	existing = 1;
     }
-    {
-	old_key = key;
-	retval = (*func)(&key, &value, arg, existing);
-      unpacked:
-	switch (retval) {
-	  case ST_CONTINUE:
-	    if (!existing) {
-		add_direct(table, key, value, hash_val, hash_pos(hash_val, table->num_bins));
-		break;
-	    }
-	    if (old_key != key) {
-		ptr->key = key;
-	    }
-	    ptr->record = value;
-	    break;
-	  case ST_DELETE:
-	    if (!existing) break;
-	    last = &table->bins[bin_pos];
-	    for (; (tmp = *last) != 0; last = &tmp->next) {
-		if (ptr == tmp) {
-		    *last = ptr->next;
-		    remove_entry(table, ptr);
-		    st_free_entry(ptr);
-		    break;
-		}
-	    }
+    old_key = key;
+    retval = (*func)(&key, &value, arg, existing);
+    /*
+     * Note, that previously this code wrote into deallocated
+     * memory (valgrind see it), but usually still worked.
+	   a = {}; 10.times{|i| a[i] = i}; b = a.dup
+	   a.merge!(b){|k,v,w| a.delete[k]; a[k+10]=v}
+     * now this code will fail
+     */
+    assert(rebuild_num == table->rebuild_num);
+    switch (retval) {
+      case ST_CONTINUE:
+	if (!existing) {
+	    add_direct(table, key, value, hash_val);
 	    break;
 	}
-	return existing;
+	if (old_key != key) {
+	    ptr->key = key;
+	}
+	ptr->record = value;
+	break;
+      case ST_DELETE:
+	if (existing) {
+	    remove_entry(table, ptr);
+	}
+	break;
     }
+    return existing;
 }
 
 int
-st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never)
+st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
 {
-    st_table_entry *ptr = 0, **last, *tmp, *next;
-    struct list_head *head;
+    st_table_entry *ptr = 0;
     enum st_retval retval;
-    st_index_t i;
-
-    if (table->entries_packed) {
-	for (i = 0; i < table->real_entries; i++) {
-	    st_data_t key, val;
-	    st_index_t hash;
-	    key = PKEY(table, i);
-	    val = PVAL(table, i);
-	    hash = PHASH(table, i);
-	    if (key == never) continue;
-	    retval = (*func)(key, val, arg, 0);
-	    if (!table->entries_packed) {
-		FIND_ENTRY(table, ptr, hash, i);
-		if (retval == ST_CHECK) {
-		    if (!ptr) goto deleted;
+    st_idx_t rebuild_num = table->rebuild_num;
+    st_idx_t idx;
+    st_index_t hash;
+    st_data_t key;
+
+    ptr = table->as.entries + table->first;
+    for (idx = table->first; idx < table->last; idx++, ptr++) {
+	if (ptr->hash != DELETED) {
+	    key = ptr->key;
+	    hash = ptr->hash;
+	    retval = (*func)(key, ptr->record, arg, 0);
+	    if (rebuild_num != table->rebuild_num) {
+		/* call func with error notice */
+		ptr = find_exact_key(table, key, hash);
+		if (ptr == NULL) {
+		    /* case for st_foreach_safe */
+		    assert(retval == ST_CHECK); /* explicitely not st_assert */
+		    (*func)(0, 0, arg, 1);
+		    st_check_table(table);
+		    return 1;
+		} else {
+		    rebuild_num = table->rebuild_num;
+		    idx = ptr - table->as.entries;;
 		}
-		if (table->num_entries == 0) return 0;
-		head = st_head(table);
-		next = list_entry(ptr->olist.next, st_table_entry, olist);
-		goto unpacked;
 	    }
 	    switch (retval) {
 	      case ST_CHECK:	/* check if hash is modified during iteration */
-		if (PHASH(table, i) == 0 && PKEY(table, i) == never) {
-		    break;
-		}
-		i = find_packed_index_from(table, hash, key, i);
-		if (i >= table->real_entries) {
-		    i = find_packed_index(table, hash, key);
-		    if (i >= table->real_entries) goto deleted;
-		}
-		/* fall through */
+	        break;
 	      case ST_CONTINUE:
 		break;
 	      case ST_STOP:
 		return 0;
 	      case ST_DELETE:
-		remove_safe_packed_entry(table, i, never);
-		break;
-	    }
-	}
-	return 0;
-    }
-
-    head = st_head(table);
-    list_for_each_safe(head, ptr, next, olist) {
-	if (ptr->key != never) {
-	    i = hash_pos(ptr->hash, table->num_bins);
-	    retval = (*func)(ptr->key, ptr->record, arg, 0);
-	  unpacked:
-	    switch (retval) {
-	      case ST_CHECK:	/* check if hash is modified during iteration */
-		for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
-		    if (!tmp) {
-		      deleted:
-			/* call func with error notice */
-			retval = (*func)(0, 0, arg, 1);
-			return 1;
-		    }
-		}
-		/* fall through */
-	      case ST_CONTINUE:
-		break;
-	      case ST_STOP:
-		return 0;
-	      case ST_DELETE:
-		last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
-		for (; (tmp = *last) != 0; last = &tmp->next) {
-		    if (ptr == tmp) {
-			remove_entry(table, ptr);
-			ptr->key = ptr->record = never;
-			ptr->hash = 0;
-			break;
-		    }
+		st_assert(table->rebuild_num == rebuild_num);
+		if (ptr->hash != DELETED) {
+		    remove_entry(table, ptr);
 		}
 		if (table->num_entries == 0) return 0;
 	    }
@@ -972,97 +917,26 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
     return 0;
 }
 
+
 int
-st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
+st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never ST_UNUSED)
 {
-    st_table_entry *ptr = 0, **last, *tmp, *next;
-    enum st_retval retval;
-    struct list_head *head;
-    st_index_t i;
-
-    if (table->entries_packed) {
-	for (i = 0; i < table->real_entries; i++) {
-	    st_data_t key, val;
-	    st_index_t hash;
-	    key = PKEY(table, i);
-	    val = PVAL(table, i);
-	    hash = PHASH(table, i);
-	    retval = (*func)(key, val, arg, 0);
-	    if (!table->entries_packed) {
-		FIND_ENTRY(table, ptr, hash, i);
-		if (!ptr) return 0;
-		head = st_head(table);
-		next = list_entry(ptr->olist.next, st_table_entry, olist);
-		goto unpacked;
-	    }
-	    switch (retval) {
-	      case ST_CONTINUE:
-		break;
-	      case ST_CHECK:
-	      case ST_STOP:
-		return 0;
-	      case ST_DELETE:
-		remove_packed_entry(table, i);
-		i--;
-		break;
-	    }
-	}
-	return 0;
-    }
-
-    head = st_head(table);
-    list_for_each_safe(head, ptr, next, olist) {
-	i = hash_pos(ptr->hash, table->num_bins);
-	retval = (*func)(ptr->key, ptr->record, arg, 0);
-      unpacked:
-	switch (retval) {
-	  case ST_CONTINUE:
-	    break;
-	  case ST_CHECK:
-	  case ST_STOP:
-	    return 0;
-	  case ST_DELETE:
-	    last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
-	    for (; (tmp = *last) != 0; last = &tmp->next) {
-		if (ptr == tmp) {
-		    *last = ptr->next;
-		    remove_entry(table, ptr);
-		    st_free_entry(ptr);
-		    break;
-		}
-	    }
-	    if (table->num_entries == 0) return 0;
-	}
-    }
-    return 0;
+    return st_foreach(table, func, arg);
 }
 
 static st_index_t
-get_keys(const st_table *table, st_data_t *keys, st_index_t size,
-         int check, st_data_t never)
+get_keys(const st_table *table, st_data_t *keys, st_index_t size)
 {
-    st_data_t key;
     st_data_t *keys_start = keys;
+    st_data_t *keys_end = keys + size;
+    
+    st_table_entry *ptr = table->as.entries + table->first;
+    st_table_entry *end = table->as.entries + table->last;
 
-    if (table->entries_packed) {
-	st_index_t i;
-
-	if (size > table->real_entries) size = table->real_entries;
-	for (i = 0; i < size; i++) {
-	    key = PKEY(table, i);
-	    if (check && key == never) continue;
-	    *keys++ = key;
-	}
-    }
-    else {
-	st_table_entry *ptr = 0;
-	st_data_t *keys_end = keys + size;
-
-	list_for_each(st_head(table), ptr, olist) {
+    for (; ptr != end; ptr++) {
+	if (ptr->hash != DELETED) {
 	    if (keys >= keys_end) break;
-	    key = ptr->key;
-	    if (check && key == never) continue;
-	    *keys++ = key;
+	    *keys++ = ptr->key;
 	}
     }
 
@@ -1072,40 +946,28 @@ get_keys(const st_table *table, st_data_t *keys, st_index_t size,
 st_index_t
 st_keys(st_table *table, st_data_t *keys, st_index_t size)
 {
-    return get_keys(table, keys, size, 0, 0);
+    return get_keys(table, keys, size);
 }
 
 st_index_t
-st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never)
+st_keys_check(st_table *table, st_data_t *keys, st_index_t size,
+	st_data_t never ST_UNUSED)
 {
-    return get_keys(table, keys, size, 1, never);
+    return get_keys(table, keys, size);
 }
 
 static st_index_t
-get_values(const st_table *table, st_data_t *values, st_index_t size,
-           int check, st_data_t never)
+get_values(const st_table *table, st_data_t *values, st_index_t size)
 {
-    st_data_t key;
     st_data_t *values_start = values;
+    st_data_t *values_end = values + size;
 
-    if (table->entries_packed) {
-	st_index_t i;
+    st_table_entry *ptr = table->as.entries + table->first;
+    st_table_entry *end = table->as.entries + table->last;
 
-	if (size > table->real_entries) size = table->real_entries;
-	for (i = 0; i < size; i++) {
-	    key = PKEY(table, i);
-	    if (check && key == never) continue;
-	    *values++ = PVAL(table, i);
-	}
-    }
-    else {
-	st_table_entry *ptr = 0;
-	st_data_t *values_end = values + size;
-
-	list_for_each(st_head(table), ptr, olist) {
+    for (; ptr != end; ptr++) {
+	if (ptr->hash != DELETED) {
 	    if (values >= values_end) break;
-	    key = ptr->key;
-	    if (check && key == never) continue;
 	    *values++ = ptr->record;
 	}
     }
@@ -1116,170 +978,16 @@ get_values(const st_table *table, st_data_t *values, st_index_t size,
 st_index_t
 st_values(st_table *table, st_data_t *values, st_index_t size)
 {
-    return get_values(table, values, size, 0, 0);
+    return get_values(table, values, size);
 }
 
 st_index_t
-st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never)
+st_values_check(st_table *table, st_data_t *values, st_index_t size,
+	st_data_t never ST_UNUSED)
 {
-    return get_values(table, values, size, 1, never);
+    return get_values(table, values, size);
 }
 
-#if 0  /* unused right now */
-int
-st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never)
-{
-    st_table_entry *ptr, **last, *tmp, *next;
-    struct list_head *head;
-    enum st_retval retval;
-    st_index_t i;
-
-    if (table->entries_packed) {
-	for (i = table->real_entries; 0 < i;) {
-	    st_data_t key, val;
-	    st_index_t hash;
-	    --i;
-	    key = PKEY(table, i);
-	    val = PVAL(table, i);
-	    hash = PHASH(table, i);
-	    if (key == never) continue;
-	    retval = (*func)(key, val, arg, 0);
-	    if (!table->entries_packed) {
-		FIND_ENTRY(table, ptr, hash, i);
-		if (retval == ST_CHECK) {
-		    if (!ptr) goto deleted;
-		}
-		if (table->num_entries == 0) return 0;
-		head = st_head(table);
-		next = list_entry(ptr->olist.next, st_table_entry, olist);
-		goto unpacked;
-	    }
-	    switch (retval) {
-	      case ST_CHECK:	/* check if hash is modified during iteration */
-		if (PHASH(table, i) == 0 && PKEY(table, i) == never) {
-		    break;
-		}
-		i = find_packed_index_from(table, hash, key, i);
-		if (i >= table->real_entries) {
-		    i = find_packed_index(table, hash, key);
-		    if (i >= table->real_entries) goto deleted;
-		}
-		/* fall through */
-	      case ST_CONTINUE:
-		break;
-	      case ST_STOP:
-		return 0;
-	      case ST_DELETE:
-		remove_safe_packed_entry(table, i, never);
-		break;
-	    }
-	}
-	return 0;
-    }
-
-    head = st_head(table);
-    list_for_each_rev_safe(head, ptr, next, olist) {
-	if (ptr->key != never) {
-	    i = hash_pos(ptr->hash, table->num_bins);
-	    retval = (*func)(ptr->key, ptr->record, arg, 0);
-	  unpacked:
-	    switch (retval) {
-	      case ST_CHECK:	/* check if hash is modified during iteration */
-		for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
-		    if (!tmp) {
-		      deleted:
-			/* call func with error notice */
-			retval = (*func)(0, 0, arg, 1);
-			return 1;
-		    }
-		}
-		/* fall through */
-	      case ST_CONTINUE:
-		break;
-	      case ST_STOP:
-		return 0;
-	      case ST_DELETE:
-		last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
-		for (; (tmp = *last) != 0; last = &tmp->next) {
-		    if (ptr == tmp) {
-			remove_entry(table, ptr);
-			ptr->key = ptr->record = never;
-			ptr->hash = 0;
-			break;
-		    }
-		}
-		if (table->num_entries == 0) return 0;
-	    }
-	}
-    }
-    return 0;
-}
-
-int
-st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
-{
-    st_table_entry *ptr, **last, *tmp, *next;
-    enum st_retval retval;
-    struct list_head *head;
-    st_index_t i;
-
-    if (table->entries_packed) {
-	for (i = table->real_entries; 0 < i;) {
-	    st_data_t key, val;
-	    st_index_t hash;
-	    --i;
-	    key = PKEY(table, i);
-	    val = PVAL(table, i);
-	    hash = PHASH(table, i);
-	    retval = (*func)(key, val, arg, 0);
-	    if (!table->entries_packed) {
-		FIND_ENTRY(table, ptr, hash, i);
-		if (!ptr) return 0;
-		head = st_head(table);
-		next = list_entry(ptr->olist.next, st_table_entry, olist);
-		goto unpacked;
-	    }
-	    switch (retval) {
-	      case ST_CONTINUE:
-		break;
-	      case ST_CHECK:
-	      case ST_STOP:
-		return 0;
-	      case ST_DELETE:
-		remove_packed_entry(table, i);
-		break;
-	    }
-	}
-	return 0;
-    }
-
-    head = st_head(table);
-    list_for_each_rev_safe(head, ptr, next, olist) {
-	i = hash_pos(ptr->hash, table->num_bins);
-	retval = (*func)(ptr->key, ptr->record, arg, 0);
-      unpacked:
-	switch (retval) {
-	  case ST_CONTINUE:
-	    break;
-	  case ST_CHECK:
-	  case ST_STOP:
-	    return 0;
-	  case ST_DELETE:
-	    last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
-	    for (; (tmp = *last) != 0; last = &tmp->next) {
-		if (ptr == tmp) {
-		    *last = ptr->next;
-		    remove_entry(table, ptr);
-		    st_free_entry(ptr);
-		    break;
-		}
-	    }
-	    if (table->num_entries == 0) return 0;
-	}
-    }
-    return 0;
-}
-#endif
 
 static st_index_t st_seed[3];
 static int st_need_seed = 1;
@@ -1316,7 +1024,6 @@ st_hash_seed(st_index_t seed[3])
  * Simplification is legal, cause most of magic still happens in finalizator.
  * And finalizator is almost the same as in MurmurHash3 */
 #define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
-#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
 
 #if ST_INDEX_BITS <= 32
 #define C1 0xcc9e2d51
@@ -1636,6 +1343,5 @@ st_numcmp(st_data_t x, st_data_t y)
 st_index_t
 st_numhash(st_data_t n)
 {
-    enum {s1 = 11, s2 = 3};
-    return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
+    return (n << 3) ^ (n >> 11) ^ (n >> 3);
 }
diff --git a/st_4_4.inc b/st_4_4.inc
new file mode 100644
index 0000000..427882c
--- /dev/null
+++ b/st_4_4.inc
@@ -0,0 +1,18 @@
+    { 6, 16 }, { 9, 16 },
+    { 13, 32 }, { 18, 32 },
+    { 26, 64 }, { 37, 64 },
+    { 53, 128 }, { 74, 128 },
+    { 106, 256 }, { 149, 256 },
+    { 256, 512 }, { 341, 1024 },
+    { 512, 1024 }, { 682, 2048 },
+    { 1024, 2048 }, { 1365, 4096 },
+    { 2048, 4096 }, { 2730, 8192 },
+    { 4096, 8192 }, { 5461, 16384 },
+    { 8192, 16384 }, { 10922, 32768 },
+    { 16384, 32768 }, { 21845, 65536 },
+    { 32768, 65536 }, { 43690, 131072 },
+    { 87381, 262144 }, { 174762, 524288 },
+    { 349525, 1048576 }, { 699050, 2097152 },
+    { 1398101, 4194304 }, { 2796202, 8388608 },
+    { 5592405, 16777216 }, { 11184810, 33554432 },
+    { 22369621, 67108864 }, { 44739242, 134217728 },
diff --git a/st_4_8.inc b/st_4_8.inc
new file mode 100644
index 0000000..06c5103
--- /dev/null
+++ b/st_4_8.inc
@@ -0,0 +1,28 @@
+    { 7, 16 }, { 10, 16 },
+    { 14, 32 }, { 20, 32 },
+    { 29, 64 }, { 40, 64 },
+    { 58, 128 }, { 80, 128 },
+    { 117, 256 }, { 160, 256 },
+    { 213, 512 }, { 298, 512 },
+    { 426, 1024 }, { 597, 1024 },
+    { 853, 2048 }, { 1194, 2048 },
+    { 1706, 4096 }, { 2389, 4096 },
+    { 3413, 8192 }, { 4778, 8192 },
+    { 6826, 16384 }, { 9557, 16384 },
+    { 13653, 32768 }, { 19114, 32768 },
+    { 27306, 65536 }, { 38229, 65536 },
+    { 65536, 131072 }, { 87381, 262144 },
+    { 131072, 262144 }, { 174762, 524288 },
+    { 262144, 524288 }, { 349525, 1048576 },
+    { 524288, 1048576 }, { 699050, 2097152 },
+    { 1048576, 2097152 }, { 1398101, 4194304 },
+    { 2097152, 4194304 }, { 2796202, 8388608 },
+    { 4194304, 8388608 }, { 5592405, 16777216 },
+    { 8388608, 16777216 }, { 11184810, 33554432 },
+    { 16777216, 33554432 }, { 22369621, 67108864 },
+    { 33554432, 67108864 }, { 44739242, 134217728 },
+    { 67108864, 134217728 }, { 89478485, 268435456 },
+    { 134217728, 268435456 }, { 178956970, 536870912 },
+    { 268435456, 536870912 }, { 357913941, 1073741824 },
+    { 536870912, 1073741824 }, { 715827882, 2147483648 },
+    { 1073741824, 2147483648 },
diff --git a/st_8_8.inc b/st_8_8.inc
new file mode 100644
index 0000000..890d360
--- /dev/null
+++ b/st_8_8.inc
@@ -0,0 +1,34 @@
+    { 7, 16 }, { 10, 16 },
+    { 14, 32 }, { 20, 32 },
+    { 29, 64 }, { 40, 64 },
+    { 58, 128 }, { 80, 128 },
+    { 117, 256 }, { 160, 256 },
+    { 213, 512 }, { 298, 512 },
+    { 426, 1024 }, { 597, 1024 },
+    { 853, 2048 }, { 1194, 2048 },
+    { 1706, 4096 }, { 2389, 4096 },
+    { 3413, 8192 }, { 4778, 8192 },
+    { 6826, 16384 }, { 9557, 16384 },
+    { 13653, 32768 }, { 19114, 32768 },
+    { 27306, 65536 }, { 38229, 65536 },
+    { 65536, 131072 }, { 87381, 262144 },
+    { 131072, 262144 }, { 174762, 524288 },
+    { 262144, 524288 }, { 349525, 1048576 },
+    { 524288, 1048576 }, { 699050, 2097152 },
+    { 1048576, 2097152 }, { 1398101, 4194304 },
+    { 2097152, 4194304 }, { 2796202, 8388608 },
+    { 4194304, 8388608 }, { 5592405, 16777216 },
+    { 8388608, 16777216 }, { 11184810, 33554432 },
+    { 16777216, 33554432 }, { 22369621, 67108864 },
+    { 33554432, 67108864 }, { 44739242, 134217728 },
+    { 67108864, 134217728 }, { 89478485, 268435456 },
+    { 134217728, 268435456 }, { 178956970, 536870912 },
+    { 268435456, 536870912 }, { 357913941, 1073741824 },
+    { 536870912, 1073741824 }, { 715827882, 2147483648 },
+    { 1073741824, 2147483648 }, { 1431655765, 4294967296 },
+    { 2147483648, 4294967296 }, { 2863311530, 8589934592 },
+    { 5726623061, 17179869184 }, { 11453246122, 34359738368 },
+    { 22906492245, 68719476736 }, { 45812984490, 137438953472 },
+    { 91625968981, 274877906944 }, { 183251937962, 549755813888 },
+    { 366503875925, 1099511627776 }, { 733007751850, 2199023255552 },
+    { 1466015503701, 4398046511104 },
diff --git a/test/-ext-/st/test_foreach.rb b/test/-ext-/st/test_foreach.rb
index 4b3eb87..216cee8 100644
--- a/test/-ext-/st/test_foreach.rb
+++ b/test/-ext-/st/test_foreach.rb
@@ -11,6 +11,5 @@ def test_st_foreach_check_unpack
 
   def test_st_foreach_unpack
     assert_nil Bug.unp_st_foreach(:unpacked), "goto unpacked"
-    assert_nil Bug.unp_st_foreach(:unpack_delete), "if (!ptr) return 0"
   end
 end
-- 
2.7.4