From 1656f3aedc48bb0f3b378e5003641504a35408bd Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 30 Jun 2015 23:27:45 -0400 Subject: [PATCH] Terminate search early when the distance-to-initial-bucket is shorter than i --- treetrie.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/treetrie.c b/treetrie.c index 0861b2d..29a5db6 100644 --- a/treetrie.c +++ b/treetrie.c @@ -453,12 +453,22 @@ tt_node_ptr_t tt_arena_cons(tt_arena_t *a, tt_node_idx_t candidate_i = tt_ptr_idx(candidate); /* printf("cons at %d candidate %d\n", i, candidate); */ - /* TODO: perhaps also bail early if we detect that the hash code changes */ if (candidate == TT_NO_PTR) { /* printf("cons empty cell\n"); */ break; } + { + tt_hash_t candidate_h = a->table[index].hash; + int distance = index - (candidate_h % a->table_length); + if (distance < 0) distance += a->table_length; + + if (i > distance) { + /* printf("early exit as we found a slot that would have been used instead\n"); */ + break; + } + } + /* printf("tag %d %d\n", tt_ptr_tag(candidate), tag); */ /* printf("index %d %d\n", a->headers[candidate].inuse.index, nindex); */ /* printf("a %d %d\n", a->nodes[candidate].a, na); */