CCAN: import strset.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2021-11-15 04:29:46 +10:30
parent 663c8c6c02
commit 7382616513
12 changed files with 1753 additions and 0 deletions

View file

@ -132,6 +132,7 @@ CCAN_OBJS := \
ccan-str-hex.o \
ccan-str.o \
ccan-strmap.o \
ccan-strset.o \
ccan-take.o \
ccan-tal-grab_file.o \
ccan-tal-link.o \
@ -200,6 +201,7 @@ CCAN_HEADERS := \
$(CCANDIR)/ccan/str/str.h \
$(CCANDIR)/ccan/str/str_debug.h \
$(CCANDIR)/ccan/strmap/strmap.h \
$(CCANDIR)/ccan/strset/strset.h \
$(CCANDIR)/ccan/structeq/structeq.h \
$(CCANDIR)/ccan/take/take.h \
$(CCANDIR)/ccan/tal/grab_file/grab_file.h \
@ -815,6 +817,8 @@ ccan-cdump.o: $(CCANDIR)/ccan/cdump/cdump.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-strmap.o: $(CCANDIR)/ccan/strmap/strmap.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-strset.o: $(CCANDIR)/ccan/strset/strset.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-crypto-siphash24.o: $(CCANDIR)/ccan/crypto/siphash24/siphash24.c
@$(call VERBOSE, "cc $<", $(CC) $(CFLAGS) -c -o $@ $<)
ccan-htable.o: $(CCANDIR)/ccan/htable/htable.c

72
ccan/ccan/strset/_info Normal file
View file

@ -0,0 +1,72 @@
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* strset - an ordered set of strings
*
* This code implements an ordered set of string as a critbit tree. See:
*
* http://cr.yp.to/critbit.html
* http://github.com/agl/critbit (which this code is based on)
*
* Note that ccan/htable is faster and uses less memory, but doesn't provide
* ordered or prefix operations.
*
* Example:
* // Print all words in order.
* #include <ccan/strset/strset.h>
* #include <ccan/tal/grab_file/grab_file.h>
* #include <err.h>
* #include <string.h>
*
* static bool dump(const char *member, void *unused)
* {
* printf("%s ", member);
* return true; // Keep going with iteration.
* }
*
* int main(void)
* {
* struct strset words;
* char *file, *word;
*
* strset_init(&words);
* file = grab_fd(NULL, 0);
* if (!file)
* err(1, "Reading stdin");
*
* for (word = strtok(file, " \t\r\n");
* word;
* word = strtok(NULL, " \t\r\n")) {
* strset_add(&words, word);
* }
* strset_iterate(&words, dump, NULL);
* printf("\n");
* return 0;
* }
* // Given "foo bar" outputs "bar foo \n"
* // Given "foo foo bar" outputs "bar foo \n"
*
* License: CC0 (but some dependencies are LGPL!)
* Author: Rusty Russell <rusty@rustcorp.com.au>
* Ccanlint:
* license_depends_compat FAIL
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/ilog\n"
"ccan/likely\n"
"ccan/short_types\n"
"ccan/str\n"
"ccan/typesafe_cb\n");
return 0;
}
return 1;
}

309
ccan/ccan/strset/strset.c Normal file
View file

@ -0,0 +1,309 @@
/* This code is based on the public domain code at
* http://github.com/agl/critbit writtem by Adam Langley
* <agl@imperialviolet.org>.
*
* Here are the main implementation differences:
* (1) We don't strdup the string on insert; we use the pointer we're given.
* (2) We use a straight bit number rather than a mask; it's simpler.
* (3) We don't use the bottom bit of the pointer, but instead use a leading
* zero to distinguish nodes from strings.
* (4) The empty string (which would look like a node) is handled
* using a special "empty node".
* (5) Delete returns the string, so you can free it if you want to.
* (6) Unions instead of void *, bool instead of int.
*/
#include <ccan/strset/strset.h>
#include <ccan/short_types/short_types.h>
#include <ccan/likely/likely.h>
#include <ccan/str/str.h>
#include <ccan/ilog/ilog.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
struct node {
/* To differentiate us from strings. */
char nul_byte;
/* The bit where these children differ. */
u8 bit_num;
/* The byte number where first bit differs (-1 == empty string node). */
size_t byte_num;
/* These point to strings or nodes. */
struct strset child[2];
};
/* Closest member to this in a non-empty set. */
static const char *closest(struct strset n, const char *member)
{
size_t len = strlen(member);
const u8 *bytes = (const u8 *)member;
/* Anything with first byte 0 is a node. */
while (!n.u.s[0]) {
u8 direction = 0;
/* Special node which represents the empty string. */
if (unlikely(n.u.n->byte_num == (size_t)-1)) {
n = n.u.n->child[0];
break;
}
if (n.u.n->byte_num < len) {
u8 c = bytes[n.u.n->byte_num];
direction = (c >> n.u.n->bit_num) & 1;
}
n = n.u.n->child[direction];
}
return n.u.s;
}
char *strset_get(const struct strset *set, const char *member)
{
const char *str;
/* Non-empty set? */
if (set->u.n) {
str = closest(*set, member);
if (streq(member, str))
return (char *)str;
}
errno = ENOENT;
return NULL;
}
static bool set_string(struct strset *set,
struct strset *n, const char *member)
{
/* Substitute magic empty node if this is the empty string */
if (unlikely(!member[0])) {
n->u.n = malloc(sizeof(*n->u.n));
if (unlikely(!n->u.n)) {
errno = ENOMEM;
return false;
}
n->u.n->nul_byte = '\0';
n->u.n->byte_num = (size_t)-1;
/* Attach the string to child[0] */
n = &n->u.n->child[0];
}
n->u.s = member;
return true;
}
bool strset_add(struct strset *set, const char *member)
{
size_t len = strlen(member);
const u8 *bytes = (const u8 *)member;
struct strset *np;
const char *str;
struct node *newn;
size_t byte_num;
u8 bit_num, new_dir;
/* Empty set? */
if (!set->u.n) {
return set_string(set, set, member);
}
/* Find closest existing member. */
str = closest(*set, member);
/* Find where they differ. */
for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
if (member[byte_num] == '\0') {
/* All identical! */
errno = EEXIST;
return false;
}
}
/* Find which bit differs (if we had ilog8, we'd use it) */
bit_num = ilog32_nz((u8)str[byte_num] ^ bytes[byte_num]) - 1;
assert(bit_num < CHAR_BIT);
/* Which direction do we go at this bit? */
new_dir = ((bytes[byte_num]) >> bit_num) & 1;
/* Allocate new node. */
newn = malloc(sizeof(*newn));
if (!newn) {
errno = ENOMEM;
return false;
}
newn->nul_byte = '\0';
newn->byte_num = byte_num;
newn->bit_num = bit_num;
if (unlikely(!set_string(set, &newn->child[new_dir], member))) {
free(newn);
return false;
}
/* Find where to insert: not closest, but first which differs! */
np = set;
while (!np->u.s[0]) {
u8 direction = 0;
/* Special node which represents the empty string will
* break here too! */
if (np->u.n->byte_num > byte_num)
break;
/* Subtle: bit numbers are "backwards" for comparison */
if (np->u.n->byte_num == byte_num && np->u.n->bit_num < bit_num)
break;
if (np->u.n->byte_num < len) {
u8 c = bytes[np->u.n->byte_num];
direction = (c >> np->u.n->bit_num) & 1;
}
np = &np->u.n->child[direction];
}
newn->child[!new_dir]= *np;
np->u.n = newn;
return true;
}
char *strset_del(struct strset *set, const char *member)
{
size_t len = strlen(member);
const u8 *bytes = (const u8 *)member;
struct strset *parent = NULL, *n;
const char *ret = NULL;
u8 direction = 0; /* prevent bogus gcc warning. */
/* Empty set? */
if (!set->u.n) {
errno = ENOENT;
return NULL;
}
/* Find closest, but keep track of parent. */
n = set;
/* Anything with first byte 0 is a node. */
while (!n->u.s[0]) {
u8 c = 0;
/* Special node which represents the empty string. */
if (unlikely(n->u.n->byte_num == (size_t)-1)) {
const char *empty_str = n->u.n->child[0].u.s;
if (member[0]) {
errno = ENOENT;
return NULL;
}
/* Sew empty string back so remaining logic works */
free(n->u.n);
n->u.s = empty_str;
break;
}
parent = n;
if (n->u.n->byte_num < len) {
c = bytes[n->u.n->byte_num];
direction = (c >> n->u.n->bit_num) & 1;
} else
direction = 0;
n = &n->u.n->child[direction];
}
/* Did we find it? */
if (!streq(member, n->u.s)) {
errno = ENOENT;
return NULL;
}
ret = n->u.s;
if (!parent) {
/* We deleted last node. */
set->u.n = NULL;
} else {
struct node *old = parent->u.n;
/* Raise other node to parent. */
*parent = old->child[!direction];
free(old);
}
return (char *)ret;
}
static bool iterate(struct strset n,
bool (*handle)(const char *, void *), const void *data)
{
if (n.u.s[0])
return handle(n.u.s, (void *)data);
if (unlikely(n.u.n->byte_num == (size_t)-1))
return handle(n.u.n->child[0].u.s, (void *)data);
return iterate(n.u.n->child[0], handle, data)
&& iterate(n.u.n->child[1], handle, data);
}
void strset_iterate_(const struct strset *set,
bool (*handle)(const char *, void *), const void *data)
{
/* Empty set? */
if (!set->u.n)
return;
iterate(*set, handle, data);
}
const struct strset *strset_prefix(const struct strset *set, const char *prefix)
{
const struct strset *n, *top;
size_t len = strlen(prefix);
const u8 *bytes = (const u8 *)prefix;
/* Empty set -> return empty set. */
if (!set->u.n)
return set;
top = n = set;
/* We walk to find the top, but keep going to check prefix matches. */
while (!n->u.s[0]) {
u8 c = 0, direction;
/* Special node which represents the empty string. */
if (unlikely(n->u.n->byte_num == (size_t)-1)) {
n = &n->u.n->child[0];
break;
}
if (n->u.n->byte_num < len)
c = bytes[n->u.n->byte_num];
direction = (c >> n->u.n->bit_num) & 1;
n = &n->u.n->child[direction];
if (c)
top = n;
}
if (!strstarts(n->u.s, prefix)) {
/* Convenient return for prefixes which do not appear in set. */
static const struct strset empty_set;
return &empty_set;
}
return top;
}
static void clear(struct strset n)
{
if (!n.u.s[0]) {
if (likely(n.u.n->byte_num != (size_t)-1)) {
clear(n.u.n->child[0]);
clear(n.u.n->child[1]);
}
free(n.u.n);
}
}
void strset_clear(struct strset *set)
{
if (set->u.n)
clear(*set);
set->u.n = NULL;
}

167
ccan/ccan/strset/strset.h Normal file
View file

@ -0,0 +1,167 @@
#ifndef CCAN_STRSET_H
#define CCAN_STRSET_H
#include "config.h"
#include <ccan/typesafe_cb/typesafe_cb.h>
#include <stdlib.h>
#include <stdbool.h>
/**
* struct strset - representation of a string set
*
* It's exposed here to allow you to embed it and so we can inline the
* trivial functions.
*/
struct strset {
union {
struct node *n;
const char *s;
} u;
};
/**
* strset_init - initialize a string set (empty)
*
* For completeness; if you've arranged for it to be NULL already you don't
* need this.
*
* Example:
* struct strset set;
*
* strset_init(&set);
*/
static inline void strset_init(struct strset *set)
{
set->u.n = NULL;
}
/**
* strset_empty - is this string set empty?
* @set: the set.
*
* Example:
* if (!strset_empty(&set))
* abort();
*/
static inline bool strset_empty(const struct strset *set)
{
return set->u.n == NULL;
}
/**
* strset_get - is this a member of this string set?
* @set: the set.
* @member: the string to search for.
*
* Returns the member, or NULL if it isn't in the set (and sets errno
* = ENOENT).
*
* Example:
* if (strset_get(&set, "hello"))
* printf("hello is in the set\n");
*/
char *strset_get(const struct strset *set, const char *member);
/**
* strset_add - place a member in the string set.
* @set: the set.
* @member: the string to place in the set.
*
* This returns false if we run out of memory (errno = ENOMEM), or
* (more normally) if that string already appears in the set (EEXIST).
*
* Note that the pointer is placed in the set, the string is not copied. If
* you want a copy in the set, use strdup().
*
* Example:
* if (!strset_add(&set, "goodbye"))
* printf("goodbye was already in the set\n");
*/
bool strset_add(struct strset *set, const char *member);
/**
* strset_del - remove a member from the string set.
* @set: the set.
* @member: the string to remove from the set.
*
* This returns the string which was passed to strset_add(), or NULL if
* the string was not in the map (in which case it sets errno = ENOENT).
*
* This means that if you allocated a string (eg. using strdup()), you can
* free it here.
*
* Example:
* if (!strset_del(&set, "goodbye"))
* printf("goodbye was not in the set?\n");
*/
char *strset_del(struct strset *set, const char *member);
/**
* strset_clear - remove every member from the set.
* @set: the set.
*
* The set will be empty after this.
*
* Example:
* strset_clear(&set);
*/
void strset_clear(struct strset *set);
/**
* strset_iterate - ordered iteration over a set
* @set: the set.
* @handle: the function to call.
* @arg: the argument for the function (types should match).
*
* You should not alter the set within the @handle function! If it returns
* false, the iteration will stop.
*
* Example:
* static bool dump_some(const char *member, int *num)
* {
* // Only dump out num nodes.
* if (*(num--) == 0)
* return false;
* printf("%s\n", member);
* return true;
* }
*
* static void dump_set(const struct strset *set)
* {
* int max = 100;
* strset_iterate(set, dump_some, &max);
* if (max < 0)
* printf("... (truncated to 100 entries)\n");
* }
*/
#define strset_iterate(set, handle, arg) \
strset_iterate_((set), typesafe_cb_preargs(bool, void *, \
(handle), (arg), \
const char *), \
(arg))
void strset_iterate_(const struct strset *set,
bool (*handle)(const char *, void *), const void *data);
/**
* strset_prefix - return a subset matching a prefix
* @set: the set.
* @prefix: the prefix.
*
* This returns a pointer into @set, so don't alter @set while using
* the return value. You can use strset_iterate(), strset_test() or
* strset_empty() on the returned pointer.
*
* Example:
* static void dump_prefix(const struct strset *set, const char *prefix)
* {
* int max = 100;
* printf("Nodes with prefix %s:\n", prefix);
* strset_iterate(strset_prefix(set, prefix), dump_some, &max);
* if (max < 0)
* printf("... (truncated to 100 entries)\n");
* }
*/
const struct strset *strset_prefix(const struct strset *set,
const char *prefix);
#endif /* CCAN_STRSET_H */

View file

@ -0,0 +1,82 @@
/* Test high bit handling. */
#include <ccan/strset/strset.h>
#include <ccan/strset/strset.c>
#include <ccan/tap/tap.h>
#define NUM 1000
static void encode(char template[3], unsigned int val)
{
assert(val < 255 * 255);
template[0] = (val / 255) + 1;
template[1] = (val % 255) + 1;
template[2] = '\0';
}
static bool in_order(const char *value, unsigned int *count)
{
char template[3];
encode(template, *count);
ok1(streq(value, template));
(*count)++;
return true;
}
static bool in_order_by_2(const char *value, unsigned int *count)
{
char template[3];
encode(template, *count);
ok1(streq(value, template));
(*count) += 2;
return true;
}
static bool dump(const char *value, void *unused)
{
diag("%s", value);
return true;
}
int main(void)
{
struct strset set;
unsigned int i;
char *str[NUM];
plan_tests(NUM + 3 * NUM / 2);
strset_init(&set);
for (i = 0; i < NUM; i++) {
char template[3];
encode(template, i);
str[i] = strdup(template);
}
for (i = 0; i < NUM; i++)
strset_add(&set, str[i]);
strset_iterate(&set, dump, NULL);
/* Iterate. */
i = 0;
strset_iterate(&set, in_order, &i);
/* Preserve order after deletion. */
for (i = 0; i < NUM; i += 2)
ok1(strset_del(&set, str[i]) == str[i]);
i = 1;
strset_iterate(&set, in_order_by_2, &i);
for (i = 1; i < NUM; i += 2)
ok1(strset_del(&set, str[i]) == str[i]);
/* empty traverse. */
strset_iterate(&set, in_order_by_2, (unsigned int *)NULL);
for (i = 0; i < NUM; i++)
free(str[i]);
/* This exits depending on whether all tests passed */
return exit_status();
}

View file

@ -0,0 +1,30 @@
#include <ccan/strset/strset.h>
#include <ccan/strset/strset.c>
#include <ccan/tap/tap.h>
static bool found = false;
/* Make sure const args work. */
static bool find_string(const char *str, const char *cmp)
{
if (strcmp(str, cmp) == 0)
found = true;
return true;
}
int main(void)
{
struct strset set;
plan_tests(3);
strset_init(&set);
ok1(strset_add(&set, "hello"));
ok1(strset_add(&set, "world"));
strset_iterate(&set, find_string, (const char *)"hello");
ok1(found);
strset_clear(&set);
/* This exits depending on whether all tests passed */
return exit_status();
}

View file

@ -0,0 +1,81 @@
#include <ccan/strset/strset.h>
#include <ccan/strset/strset.c>
#include <ccan/tap/tap.h>
#include <stdio.h>
#define NUM 1000
static bool in_order(const char *value, unsigned int *count)
{
int i = atoi(value);
ok1(*count == i);
(*count)++;
return true;
}
static bool in_order_by_2(const char *value, unsigned int *count)
{
int i = atoi(value);
ok1(*count == i);
(*count) += 2;
return true;
}
static bool dump(const char *value, void *unused)
{
diag("%s", value);
return true;
}
int main(void)
{
struct strset set;
unsigned int i;
char *str[NUM];
plan_tests(NUM * 2 + 3 * NUM / 2);
strset_init(&set);
for (i = 0; i < NUM; i++) {
char template[10];
sprintf(template, "%08u", i);
str[i] = strdup(template);
}
for (i = 0; i < NUM; i++)
strset_add(&set, str[i]);
strset_iterate(&set, dump, NULL);
/* Iterate. */
i = 0;
strset_iterate(&set, in_order, &i);
/* Preserve order after deletion. */
for (i = 0; i < NUM; i += 2)
ok1(strset_del(&set, str[i]) == str[i]);
i = 1;
strset_iterate(&set, in_order_by_2, &i);
for (i = 1; i < NUM; i += 2)
ok1(strset_del(&set, str[i]) == str[i]);
/* empty traverse. */
strset_iterate(&set, in_order_by_2, (unsigned int *)NULL);
/* insert backwards, should be fine. */
for (i = 0; i < NUM; i++)
strset_add(&set, str[NUM-1-i]);
i = 0;
strset_iterate(&set, in_order, &i);
strset_clear(&set);
for (i = 0; i < NUM; i++)
free(str[i]);
/* This exits depending on whether all tests passed */
return exit_status();
}

View file

@ -0,0 +1,87 @@
#include <ccan/strset/strset.h>
#include <ccan/strset/strset.c>
#include <ccan/tap/tap.h>
#include <stdio.h>
/* Must be > 100, see below. */
#define NUM 200
static bool in_order(const char *value, unsigned int *count)
{
int i = atoi(value);
ok1(*count == i);
(*count)++;
return true;
}
static bool find_empty(const char *value, char *empty)
{
if (value == empty)
pass("Found empty entry!");
return true;
}
int main(void)
{
struct strset set;
const struct strset *sub;
unsigned int i;
char *str[NUM], *empty;
plan_tests(7 + 1 + 10 + 100);
strset_init(&set);
for (i = 0; i < NUM; i++) {
char template[10];
sprintf(template, "%08u", i);
str[i] = strdup(template);
}
for (i = 0; i < NUM; i++)
strset_add(&set, str[i]);
/* Nothing */
sub = strset_prefix(&set, "a");
ok1(strset_empty(sub));
/* Everything */
sub = strset_prefix(&set, "0");
ok1(sub->u.n == set.u.n);
sub = strset_prefix(&set, "");
ok1(sub->u.n == set.u.n);
/* Singleton. */
sub = strset_prefix(&set, "00000000");
i = 0;
strset_iterate(sub, in_order, &i);
ok1(i == 1);
/* First 10. */
sub = strset_prefix(&set, "0000000");
i = 0;
strset_iterate(sub, in_order, &i);
ok1(i == 10);
/* First 100. */
sub = strset_prefix(&set, "000000");
i = 0;
strset_iterate(sub, in_order, &i);
ok1(i == 100);
/* Everything, *plus* empty string. */
empty = strdup("");
strset_add(&set, empty);
sub = strset_prefix(&set, "");
/* Check we get *our* empty string back! */
strset_iterate(sub, find_empty, empty);
strset_clear(&set);
for (i = 0; i < NUM; i++)
free(str[i]);
free(empty);
/* This exits depending on whether all tests passed */
return exit_status();
}

View file

@ -0,0 +1,70 @@
#include <ccan/strset/strset.h>
#include <ccan/strset/strset.c>
#include <ccan/tap/tap.h>
int main(void)
{
struct strset set;
const char str[] = "hello";
const char none[] = "";
char *dup = strdup(str);
/* This is how many tests you plan to run */
plan_tests(36);
strset_init(&set);
ok1(!strset_get(&set, str));
ok1(errno == ENOENT);
ok1(!strset_get(&set, none));
ok1(errno == ENOENT);
ok1(!strset_del(&set, str));
ok1(errno == ENOENT);
ok1(!strset_del(&set, none));
ok1(errno == ENOENT);
ok1(strset_add(&set, str));
ok1(strset_get(&set, str));
/* We compare the string, not the pointer. */
ok1(strset_get(&set, dup));
ok1(!strset_get(&set, none));
ok1(errno == ENOENT);
/* Add of duplicate should fail. */
ok1(!strset_add(&set, dup));
ok1(errno == EEXIST);
/* Delete should return original string. */
ok1(strset_del(&set, dup) == str);
ok1(!strset_get(&set, str));
ok1(errno == ENOENT);
ok1(!strset_get(&set, none));
ok1(errno == ENOENT);
/* Try insert and delete of empty string. */
ok1(strset_add(&set, none));
ok1(strset_get(&set, none));
ok1(!strset_get(&set, str));
ok1(errno == ENOENT);
/* Delete should return original string. */
ok1(strset_del(&set, "") == none);
ok1(!strset_get(&set, str));
ok1(errno == ENOENT);
ok1(!strset_get(&set, none));
ok1(errno == ENOENT);
/* Both at once... */
ok1(strset_add(&set, none));
ok1(strset_add(&set, str));
ok1(strset_get(&set, str));
ok1(strset_get(&set, none));
ok1(strset_del(&set, "") == none);
ok1(strset_del(&set, dup) == str);
ok1(set.u.n == NULL);
free(dup);
/* This exits depending on whether all tests passed */
return exit_status();
}

View file

@ -0,0 +1,31 @@
CCANDIR=../../..
CFLAGS=-Wall -Werror -O3 -I$(CCANDIR)
#CFLAGS=-Wall -Werror -g -I$(CCANDIR)
all: cbspeed speed
CCAN_OBJS:=ccan-tal.o ccan-tal-str.o ccan-tal-grab_file.o ccan-take.o ccan-time.o ccan-str.o ccan-noerr.o ccan-list.o
cbspeed: cbspeed.o $(CCAN_OBJS)
speed: speed.o $(CCAN_OBJS)
clean:
rm -f cbspeed speed speed.o cbspeed.o *.o
ccan-tal.o: $(CCANDIR)/ccan/tal/tal.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-tal-str.o: $(CCANDIR)/ccan/tal/str/str.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-take.o: $(CCANDIR)/ccan/take/take.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-tal-grab_file.o: $(CCANDIR)/ccan/tal/grab_file/grab_file.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-time.o: $(CCANDIR)/ccan/time/time.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-list.o: $(CCANDIR)/ccan/list/list.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-str.o: $(CCANDIR)/ccan/str/str.c
$(CC) $(CFLAGS) -c -o $@ $<
ccan-noerr.o: $(CCANDIR)/ccan/noerr/noerr.c
$(CC) $(CFLAGS) -c -o $@ $<

View file

@ -0,0 +1,583 @@
/* Simple speed tests using original critbit code (modified not to allocate).
*
* Results on my 32 bit Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz, gcc 4.5.2:
* Run 100 times: Min-Max(Avg)
#01: Initial insert: 237-257(239)
#02: Initial lookup (match): 180-197(181)
#03: Initial lookup (miss): 171-190(172)
#04: Initial lookup (random): 441-455(446)
#05: Initial delete all: 127-148(128)
#06: Initial re-inserting: 219-298(221)
#07: Deleting first half: 101-109(102)
#08: Adding (a different) half: 159-165(160)
#09: Lookup after half-change (match): 203-216(204)
#10: Lookup after half-change (miss): 217-225(218)
#11: Churn 1: 298-311(300)
#12: Churn 2: 298-318(301)
#13: Churn 3: 301-322(304)
#14: Post-Churn lookup (match): 189-196(190)
#15: Post-Churn lookup (miss): 189-197(191)
#16: Post-Churn lookup (random): 500-531(506)
*/
#include <ccan/tal/str/str.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/tal.h>
#include <ccan/time/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
/* CRITBIT source */
typedef struct {
void *root;
} critbit0_tree;
int critbit0_contains(critbit0_tree *t, const char *u);
int critbit0_insert(critbit0_tree *t, const char *u);
int critbit0_delete(critbit0_tree *t, const char *u);
void critbit0_clear(critbit0_tree *t);
int critbit0_allprefixed(critbit0_tree *t, const char *prefix,
int (*handle) (const char *, void *), void *arg);
#define uint8 uint8_t
#define uint32 uint32_t
static size_t allocated;
/*2:*/
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
typedef struct{
void*child[2];
uint32 byte;
uint8 otherbits;
}critbit0_node;
/*:2*//*3:*/
int
critbit0_contains(critbit0_tree*t,const char*u){
const uint8*ubytes= (void*)u;
const size_t ulen= strlen(u);
uint8*p= t->root;
/*4:*/
if(!p)return 0;
/*:4*/
/*5:*/
while(1&(intptr_t)p){
critbit0_node*q= (void*)(p-1);
/*6:*/
uint8 c= 0;
if(q->byte<ulen)c= ubytes[q->byte];
const int direction= (1+(q->otherbits|c))>>8;
/*:6*/
p= q->child[direction];
}
/*:5*/
/*7:*/
return 0==strcmp(u,(const char*)p);
/*:7*/
}
/*:3*//*8:*/
int critbit0_insert(critbit0_tree*t,const char*u)
{
const uint8*const ubytes= (void*)u;
const size_t ulen= strlen(u);
uint8*p= t->root;
/*9:*/
if(!p){
#if 0
char*x;
int a= posix_memalign((void**)&x,sizeof(void*),ulen+1);
if(a)return 0;
memcpy(x,u,ulen+1);
t->root= x;
#else
t->root = (char *)u;
#endif
return 2;
}
/*:9*/
/*5:*/
while(1&(intptr_t)p){
critbit0_node*q= (void*)(p-1);
/*6:*/
uint8 c= 0;
if(q->byte<ulen)c= ubytes[q->byte];
const int direction= (1+(q->otherbits|c))>>8;
/*:6*/
p= q->child[direction];
}
/*:5*/
/*10:*/
/*11:*/
uint32 newbyte;
uint32 newotherbits;
for(newbyte= 0;newbyte<ulen;++newbyte){
if(p[newbyte]!=ubytes[newbyte]){
newotherbits= p[newbyte]^ubytes[newbyte];
goto different_byte_found;
}
}
if(p[newbyte]!=0){
newotherbits= p[newbyte];
goto different_byte_found;
}
return 1;
different_byte_found:
/*:11*/
/*12:*/
while(newotherbits&(newotherbits-1))newotherbits&= newotherbits-1;
newotherbits^= 255;
uint8 c= p[newbyte];
int newdirection= (1+(newotherbits|c))>>8;
/*:12*/
/*:10*/
/*13:*/
/*14:*/
critbit0_node*newnode;
if(posix_memalign((void**)&newnode,sizeof(void*),sizeof(critbit0_node)))return 0;
allocated++;
char*x;
#if 0
if(posix_memalign((void**)&x,sizeof(void*),ulen+1)){
free(newnode);
return 0;
}
memcpy(x,ubytes,ulen+1);
#else
x = (char *)u;
#endif
newnode->byte= newbyte;
newnode->otherbits= newotherbits;
newnode->child[1-newdirection]= x;
/*:14*/
/*15:*/
void**wherep= &t->root;
for(;;){
uint8*p= *wherep;
if(!(1&(intptr_t)p))break;
critbit0_node*q= (void*)(p-1);
if(q->byte> newbyte)break;
if(q->byte==newbyte&&q->otherbits> newotherbits)break;
uint8 c= 0;
if(q->byte<ulen)c= ubytes[q->byte];
const int direction= (1+(q->otherbits|c))>>8;
wherep= q->child+direction;
}
newnode->child[newdirection]= *wherep;
*wherep= (void*)(1+(char*)newnode);
/*:15*/
/*:13*/
return 2;
}
/*:8*//*16:*/
int critbit0_delete(critbit0_tree*t,const char*u){
const uint8*ubytes= (void*)u;
const size_t ulen= strlen(u);
uint8*p= t->root;
void**wherep= &t->root;
void**whereq= 0;
critbit0_node*q= 0;
int direction= 0;
/*17:*/
if(!p)return 0;
/*:17*/
/*18:*/
while(1&(intptr_t)p){
whereq= wherep;
q= (void*)(p-1);
uint8 c= 0;
if(q->byte<ulen)c= ubytes[q->byte];
direction= (1+(q->otherbits|c))>>8;
wherep= q->child+direction;
p= *wherep;
}
/*:18*/
/*19:*/
if(0!=strcmp(u,(const char*)p))return 0;
#if 0
free(p);
#endif
/*:19*/
/*20:*/
if(!whereq){
t->root= 0;
return 1;
}
*whereq= q->child[1-direction];
free(q);
allocated--;
/*:20*/
return 1;
}
/*:16*//*21:*/
static void
traverse(void*top){
/*22:*/
uint8*p= top;
if(1&(intptr_t)p){
critbit0_node*q= (void*)(p-1);
traverse(q->child[0]);
traverse(q->child[1]);
free(q);
allocated--;
}else{
#if 0
free(p);
#endif
}
/*:22*/
}
void critbit0_clear(critbit0_tree*t)
{
if(t->root)traverse(t->root);
t->root= NULL;
}
/*:21*//*23:*/
static int
allprefixed_traverse(uint8*top,
int(*handle)(const char*,void*),void*arg){
/*26:*/
if(1&(intptr_t)top){
critbit0_node*q= (void*)(top-1);
int direction;
for(direction= 0;direction<2;++direction)
switch(allprefixed_traverse(q->child[direction],handle,arg)){
case 1:break;
case 0:return 0;
default:return-1;
}
return 1;
}
/*:26*/
/*27:*/
return handle((const char*)top,arg);/*:27*/
}
int
critbit0_allprefixed(critbit0_tree*t,const char*prefix,
int(*handle)(const char*,void*),void*arg){
const uint8*ubytes= (void*)prefix;
const size_t ulen= strlen(prefix);
uint8*p= t->root;
uint8*top= p;
size_t i;
if(!p)return 1;
/*24:*/
while(1&(intptr_t)p){
critbit0_node*q= (void*)(p-1);
uint8 c= 0;
if(q->byte<ulen)c= ubytes[q->byte];
const int direction= (1+(q->otherbits|c))>>8;
p= q->child[direction];
if(q->byte<ulen)top= p;
}
/*:24*/
/*25:*/
for(i= 0;i<ulen;++i){
if(p[i]!=ubytes[i])return 1;
}
/*:25*/
return allprefixed_traverse(top,handle,arg);
}
/*:23*/
/* end critbit */
/* Nanoseconds per operation */
static size_t normalize(const struct timeabs *start,
const struct timeabs *stop,
unsigned int num)
{
return time_to_nsec(time_divide(time_between(*stop, *start), num));
}
int main(int argc, char *argv[])
{
size_t i, j, num;
struct timeabs start, stop;
critbit0_tree ct;
char **words, **misswords;
words = tal_strsplit(NULL, grab_file(NULL,
argv[1] ? argv[1] : "/usr/share/dict/words"), "\n", STR_NO_EMPTY);
ct.root = NULL;
num = tal_count(words) - 1;
printf("%zu words\n", num);
/* Append and prepend last char for miss testing. */
misswords = tal_arr(words, char *, num);
for (i = 0; i < num; i++) {
char lastc;
if (strlen(words[i]))
lastc = words[i][strlen(words[i])-1];
else
lastc = 'z';
misswords[i] = tal_fmt(misswords, "%c%s%c%c",
lastc, words[i], lastc, lastc);
}
printf("#01: Initial insert: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
critbit0_insert(&ct, words[i]);
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("Nodes allocated: %zu (%zu bytes)\n",
allocated, allocated * sizeof(critbit0_node));
printf("#02: Initial lookup (match): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
if (!critbit0_contains(&ct, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#03: Initial lookup (miss): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++) {
if (critbit0_contains(&ct, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Lookups in order are very cache-friendly for judy; try random */
printf("#04: Initial lookup (random): ");
fflush(stdout);
start = time_now();
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
if (!critbit0_contains(&ct, words[j]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#05: Initial delete all: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
if (!critbit0_delete(&ct, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#06: Initial re-inserting: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
critbit0_insert(&ct, words[i]);
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#07: Deleting first half: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i+=2)
if (!critbit0_delete(&ct, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#08: Adding (a different) half: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i+=2)
critbit0_insert(&ct, misswords[i]);
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#09: Lookup after half-change (match): ");
fflush(stdout);
start = time_now();
for (i = 1; i < num; i+=2)
if (!critbit0_contains(&ct, words[i]))
abort();
for (i = 0; i < num; i+=2) {
if (!critbit0_contains(&ct, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#10: Lookup after half-change (miss): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i+=2)
if (critbit0_contains(&ct, words[i]))
abort();
for (i = 1; i < num; i+=2) {
if (critbit0_contains(&ct, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Hashtables with delete markers can fill with markers over time.
* so do some changes to see how it operates in long-term. */
printf("#11: Churn 1: ");
start = time_now();
for (j = 0; j < num; j+=2) {
if (!critbit0_delete(&ct, misswords[j]))
abort();
if (critbit0_insert(&ct, words[j]) != 2)
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#12: Churn 2: ");
start = time_now();
for (j = 1; j < num; j+=2) {
if (!critbit0_delete(&ct, words[j]))
abort();
if (critbit0_insert(&ct, misswords[j]) != 2)
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#13: Churn 3: ");
start = time_now();
for (j = 1; j < num; j+=2) {
if (!critbit0_delete(&ct, misswords[j]))
abort();
if (critbit0_insert(&ct, words[j]) != 2)
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Now it's back to normal... */
printf("#14: Post-Churn lookup (match): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
if (!critbit0_contains(&ct, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#15: Post-Churn lookup (miss): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++) {
if (critbit0_contains(&ct, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Lookups in order are very cache-friendly for judy; try random */
printf("#16: Post-Churn lookup (random): ");
fflush(stdout);
start = time_now();
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
if (!critbit0_contains(&ct, words[j]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
return 0;
}

View file

@ -0,0 +1,237 @@
/* Simple speed tests using strset code.
*
* Results on my 32 bit Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz, gcc 4.5.2:
* Run 100 times: Min-Max(Avg)
#01: Initial insert: 212-219(214)
#02: Initial lookup (match): 161-169(162)
#03: Initial lookup (miss): 157-163(158)
#04: Initial lookup (random): 450-479(453)
#05: Initial delete all: 126-137(128)
#06: Initial re-inserting: 193-198(194)
#07: Deleting first half: 99-102(99)
#08: Adding (a different) half: 143-154(144)
#09: Lookup after half-change (match): 183-189(184)
#10: Lookup after half-change (miss): 198-212(199)
#11: Churn 1: 274-282(276)
#12: Churn 2: 279-296(282)
#13: Churn 3: 278-294(280)
#14: Post-Churn lookup (match): 170-180(171)
#15: Post-Churn lookup (miss): 175-186(176)
#16: Post-Churn lookup (random): 522-534(525)
*/
#include <ccan/tal/str/str.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/talloc/talloc.h>
#include <ccan/time/time.h>
#include <ccan/strset/strset.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
/* Nanoseconds per operation */
static size_t normalize(const struct timeabs *start,
const struct timeabs *stop,
unsigned int num)
{
return time_to_nsec(time_divide(time_between(*stop, *start), num));
}
int main(int argc, char *argv[])
{
size_t i, j, num;
struct timeabs start, stop;
struct strset set;
char **words, **misswords;
words = tal_strsplit(NULL, grab_file(NULL,
argv[1] ? argv[1] : "/usr/share/dict/words"),
"\n", STR_NO_EMPTY);
strset_init(&set);
num = tal_count(words) - 1;
printf("%zu words\n", num);
/* Append and prepend last char for miss testing. */
misswords = tal_arr(words, char *, num);
for (i = 0; i < num; i++) {
char lastc;
if (strlen(words[i]))
lastc = words[i][strlen(words[i])-1];
else
lastc = 'z';
misswords[i] = tal_fmt(misswords, "%c%s%c%c",
lastc, words[i], lastc, lastc);
}
printf("#01: Initial insert: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
strset_add(&set, words[i]);
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
#if 0
printf("Nodes allocated: %zu (%zu bytes)\n",
allocated, allocated * sizeof(critbit0_node));
#endif
printf("#02: Initial lookup (match): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
if (!strset_get(&set, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#03: Initial lookup (miss): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++) {
if (strset_get(&set, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Lookups in order are very cache-friendly for judy; try random */
printf("#04: Initial lookup (random): ");
fflush(stdout);
start = time_now();
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
if (!strset_get(&set, words[j]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#05: Initial delete all: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
if (!strset_del(&set, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#06: Initial re-inserting: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
strset_add(&set, words[i]);
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#07: Deleting first half: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i+=2)
if (!strset_del(&set, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#08: Adding (a different) half: ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i+=2)
strset_add(&set, misswords[i]);
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#09: Lookup after half-change (match): ");
fflush(stdout);
start = time_now();
for (i = 1; i < num; i+=2)
if (!strset_get(&set, words[i]))
abort();
for (i = 0; i < num; i+=2) {
if (!strset_get(&set, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#10: Lookup after half-change (miss): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i+=2)
if (strset_get(&set, words[i]))
abort();
for (i = 1; i < num; i+=2) {
if (strset_get(&set, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Hashtables with delete markers can fill with markers over time.
* so do some changes to see how it operates in long-term. */
printf("#11: Churn 1: ");
start = time_now();
for (j = 0; j < num; j+=2) {
if (!strset_del(&set, misswords[j]))
abort();
if (!strset_add(&set, words[j]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#12: Churn 2: ");
start = time_now();
for (j = 1; j < num; j+=2) {
if (!strset_del(&set, words[j]))
abort();
if (!strset_add(&set, misswords[j]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#13: Churn 3: ");
start = time_now();
for (j = 1; j < num; j+=2) {
if (!strset_del(&set, misswords[j]))
abort();
if (!strset_add(&set, words[j]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Now it's back to normal... */
printf("#14: Post-Churn lookup (match): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++)
if (!strset_get(&set, words[i]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
printf("#15: Post-Churn lookup (miss): ");
fflush(stdout);
start = time_now();
for (i = 0; i < num; i++) {
if (strset_get(&set, misswords[i]))
abort();
}
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
/* Lookups in order are very cache-friendly for judy; try random */
printf("#16: Post-Churn lookup (random): ");
fflush(stdout);
start = time_now();
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
if (!strset_get(&set, words[j]))
abort();
stop = time_now();
printf(" %zu ns\n", normalize(&start, &stop, num));
return 0;
}