From 9fd234d05edd9b6690fd9f14546f93b29e8379cf Mon Sep 17 00:00:00 2001 From: default Date: Sun, 12 Jan 2025 06:59:16 +0100 Subject: Backport from xs. --- xs.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'xs.h') diff --git a/xs.h b/xs.h index a6e40d5..2a90467 100644 --- a/xs.h +++ b/xs.h @@ -1061,14 +1061,15 @@ xs_keyval *xs_keyval_make(xs_keyval *keyval, const xs_str *key, const xs_val *va typedef struct { int value_offset; /* offset to value (from dict start) */ - int next; /* next node in sequential search */ + int next; /* next node in sequential scanning */ int child[4]; /* child nodes in hashed search */ char key[]; /* C string key */ } ditem_hdr; typedef struct { int size; /* size of full dict (_XS_TYPE_SIZE) */ - int first; /* first node for sequential search */ + int first; /* first node for sequential scanning */ + int last; /* last node for sequential scanning */ int root; /* root node for hashed search */ /* a bunch of ditem_hdr and value follows */ } dict_hdr; @@ -1153,8 +1154,15 @@ xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *value) memcpy(dict + di->value_offset, value, vsz); /* chain to the sequential list */ - di->next = dh->first; - dh->first = end; + if (dh->first == 0) + dh->first = end; + else { + /* chain this new element to the last one */ + ditem_hdr *dil = (ditem_hdr *)(dict + dh->last); + dil->next = end; + } + + dh->last = end; } else { /* ditem already exists */ -- cgit