Skip to content

Commit

Permalink
IMPORT: import cebtree (compact elastic binary trees)
Browse files Browse the repository at this point in the history
This is an import of the compact elastic binary trees at commit
a9cd84a ("OPTIM: descent: better prefetch less and for writes when
deleting")

These will be used to replace certain lists (and possibly certain
tree nodes as well). They're as fast (or even faster) than ebtrees
for lookups, as fast for insertion and slower for deletion, and a
node only uses 2 pointers (like a list).

The only changes were cebtree.h where common/tools.h was replaced
with ebtree.h which we already have and already provides the needed
functions and macros, and the addition of a wrapper cebtree-prv.h in
src/ to redirect to import/cebtree-prv.h.
  • Loading branch information
wtarreau committed Sep 15, 2024
1 parent 6e92988 commit a0205f9
Show file tree
Hide file tree
Showing 20 changed files with 3,419 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,9 @@ OBJS += src/mux_h2.o src/mux_h1.o src/mux_fcgi.o src/stream.o \
src/hpack-tbl.o src/ebsttree.o src/ebistree.o src/auth.o \
src/hpack-huff.o src/freq_ctr.o src/dict.o src/wdt.o \
src/pipe.o src/init.o src/http_acl.o src/hpack-enc.o \
src/cebu32_tree.o src/cebu64_tree.o src/cebua_tree.o \
src/cebub_tree.o src/cebuib_tree.o src/cebuis_tree.o \
src/cebul_tree.o src/cebus_tree.o \
src/ebtree.o src/dgram.o src/hash.o src/version.o \
src/limits.o src/mux_spop.o
Expand Down
1,601 changes: 1,601 additions & 0 deletions include/import/cebtree-prv.h

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions include/import/cebtree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Compact Elastic Binary Trees - exported functions operating on node's address
*
* Copyright (C) 2014-2024 Willy Tarreau - [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef _CEBTREE_H
#define _CEBTREE_H

#include <stddef.h>
#include "ebtree.h"

/* Standard node when using absolute pointers */
struct ceb_node {
struct ceb_node *b[2]; /* branches: 0=left, 1=right */
};

/* indicates whether a valid node is in a tree or not */
static inline int ceb_intree(const struct ceb_node *node)
{
return !!node->b[0];
}

/* tag an untagged pointer */
static inline struct ceb_node *__ceb_dotag(const struct ceb_node *node)
{
return (struct ceb_node *)((size_t)node + 1);
}

/* untag a tagged pointer */
static inline struct ceb_node *__ceb_untag(const struct ceb_node *node)
{
return (struct ceb_node *)((size_t)node - 1);
}

/* clear a pointer's tag */
static inline struct ceb_node *__ceb_clrtag(const struct ceb_node *node)
{
return (struct ceb_node *)((size_t)node & ~((size_t)1));
}

/* returns whether a pointer is tagged */
static inline int __ceb_tagged(const struct ceb_node *node)
{
return !!((size_t)node & 1);
}

/* returns an integer equivalent of the pointer */
static inline size_t __ceb_intptr(struct ceb_node *tree)
{
return (size_t)tree;
}

///* returns true if at least one of the branches is a subtree node, indicating
// * that the current node is at the top of a duplicate sub-tree and that all
// * values below it are the same.
// */
//static inline int __ceb_is_dup(const struct ceb_node *node)
//{
// return __ceb_tagged((struct ceb_node *)(__ceb_intptr(node->l) | __ceb_intptr(node->r)));
//}

#endif /* _CEBTREE_H */
58 changes: 58 additions & 0 deletions include/import/cebu32_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Compact Elastic Binary Trees - exported functions operating on u32 keys
*
* Copyright (C) 2014-2024 Willy Tarreau - [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include "cebtree.h"
#include <inttypes.h>

/* simpler version */
struct ceb_node *cebu32_insert(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu32_first(struct ceb_node **root);
struct ceb_node *cebu32_last(struct ceb_node **root);
struct ceb_node *cebu32_lookup(struct ceb_node **root, uint32_t key);
struct ceb_node *cebu32_lookup_le(struct ceb_node **root, uint32_t key);
struct ceb_node *cebu32_lookup_lt(struct ceb_node **root, uint32_t key);
struct ceb_node *cebu32_lookup_ge(struct ceb_node **root, uint32_t key);
struct ceb_node *cebu32_lookup_gt(struct ceb_node **root, uint32_t key);
struct ceb_node *cebu32_next(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu32_prev(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu32_delete(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu32_pick(struct ceb_node **root, uint32_t key);
void cebu32_default_dump(struct ceb_node **ceb_root, const char *label, const void *ctx);

/* version taking a key offset */
struct ceb_node *cebu32_ofs_insert(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu32_ofs_first(struct ceb_node **root, ptrdiff_t kofs);
struct ceb_node *cebu32_ofs_last(struct ceb_node **root, ptrdiff_t kofs);
struct ceb_node *cebu32_ofs_lookup(struct ceb_node **root, ptrdiff_t kofs, uint32_t key);
struct ceb_node *cebu32_ofs_lookup_le(struct ceb_node **root, ptrdiff_t kofs, uint32_t key);
struct ceb_node *cebu32_ofs_lookup_lt(struct ceb_node **root, ptrdiff_t kofs, uint32_t key);
struct ceb_node *cebu32_ofs_lookup_ge(struct ceb_node **root, ptrdiff_t kofs, uint32_t key);
struct ceb_node *cebu32_ofs_lookup_gt(struct ceb_node **root, ptrdiff_t kofs, uint32_t key);
struct ceb_node *cebu32_ofs_next(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu32_ofs_prev(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu32_ofs_delete(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu32_ofs_pick(struct ceb_node **root, ptrdiff_t kofs, uint32_t key);
void cebu32_ofs_default_dump(struct ceb_node **root, ptrdiff_t kofs, const char *label, const void *ctx);
58 changes: 58 additions & 0 deletions include/import/cebu64_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Compact Elastic Binary Trees - exported functions for operations on u64 keys
*
* Copyright (C) 2014-2024 Willy Tarreau - [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include "cebtree.h"
#include <inttypes.h>

/* simpler version */
struct ceb_node *cebu64_insert(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu64_first(struct ceb_node **root);
struct ceb_node *cebu64_last(struct ceb_node **root);
struct ceb_node *cebu64_lookup(struct ceb_node **root, uint64_t key);
struct ceb_node *cebu64_lookup_le(struct ceb_node **root, uint64_t key);
struct ceb_node *cebu64_lookup_lt(struct ceb_node **root, uint64_t key);
struct ceb_node *cebu64_lookup_ge(struct ceb_node **root, uint64_t key);
struct ceb_node *cebu64_lookup_gt(struct ceb_node **root, uint64_t key);
struct ceb_node *cebu64_next(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu64_prev(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu64_delete(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebu64_pick(struct ceb_node **root, uint64_t key);
void cebu64_default_dump(struct ceb_node **ceb_root, const char *label, const void *ctx);

/* version taking a key offset */
struct ceb_node *cebu64_ofs_insert(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu64_ofs_first(struct ceb_node **root, ptrdiff_t kofs);
struct ceb_node *cebu64_ofs_last(struct ceb_node **root, ptrdiff_t kofs);
struct ceb_node *cebu64_ofs_lookup(struct ceb_node **root, ptrdiff_t kofs, uint64_t key);
struct ceb_node *cebu64_ofs_lookup_le(struct ceb_node **root, ptrdiff_t kofs, uint64_t key);
struct ceb_node *cebu64_ofs_lookup_lt(struct ceb_node **root, ptrdiff_t kofs, uint64_t key);
struct ceb_node *cebu64_ofs_lookup_ge(struct ceb_node **root, ptrdiff_t kofs, uint64_t key);
struct ceb_node *cebu64_ofs_lookup_gt(struct ceb_node **root, ptrdiff_t kofs, uint64_t key);
struct ceb_node *cebu64_ofs_next(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu64_ofs_prev(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu64_ofs_delete(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebu64_ofs_pick(struct ceb_node **root, ptrdiff_t kofs, uint64_t key);
void cebu64_ofs_default_dump(struct ceb_node **root, ptrdiff_t kofs, const char *label, const void *ctx);
57 changes: 57 additions & 0 deletions include/import/cebua_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Compact Elastic Binary Trees - exported functions operating on addr keys
*
* Copyright (C) 2014-2024 Willy Tarreau - [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include "cebtree.h"

/* simpler version */
struct ceb_node *cebua_insert(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebua_first(struct ceb_node **root);
struct ceb_node *cebua_last(struct ceb_node **root);
struct ceb_node *cebua_lookup(struct ceb_node **root, const void *key);
struct ceb_node *cebua_lookup_le(struct ceb_node **root, const void *key);
struct ceb_node *cebua_lookup_lt(struct ceb_node **root, const void *key);
struct ceb_node *cebua_lookup_ge(struct ceb_node **root, const void *key);
struct ceb_node *cebua_lookup_gt(struct ceb_node **root, const void *key);
struct ceb_node *cebua_next(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebua_prev(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebua_delete(struct ceb_node **root, struct ceb_node *node);
struct ceb_node *cebua_pick(struct ceb_node **root, const void *key);
void cebua_default_dump(struct ceb_node **root, const char *label, const void *ctx);

/* version taking a key offset */
struct ceb_node *cebua_ofs_insert(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebua_ofs_first(struct ceb_node **root, ptrdiff_t kofs);
struct ceb_node *cebua_ofs_last(struct ceb_node **root, ptrdiff_t kofs);
struct ceb_node *cebua_ofs_lookup(struct ceb_node **root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebua_ofs_lookup_le(struct ceb_node **root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebua_ofs_lookup_lt(struct ceb_node **root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebua_ofs_lookup_ge(struct ceb_node **root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebua_ofs_lookup_gt(struct ceb_node **root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebua_ofs_next(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebua_ofs_prev(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebua_ofs_delete(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebua_ofs_pick(struct ceb_node **root, ptrdiff_t kofs, const void *key);
void cebua_ofs_default_dump(struct ceb_node **root, ptrdiff_t kofs, const char *label, const void *ctx);
55 changes: 55 additions & 0 deletions include/import/cebub_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Compact Elastic Binary Trees - exported functions operating on mb keys
*
* Copyright (C) 2014-2024 Willy Tarreau - [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include "cebtree.h"

/* simpler version */
struct ceb_node *cebub_insert(struct ceb_node **root, struct ceb_node *node, size_t len);
struct ceb_node *cebub_first(struct ceb_node **root);
struct ceb_node *cebub_last(struct ceb_node **root);
struct ceb_node *cebub_lookup(struct ceb_node **root, const void *key, size_t len);
struct ceb_node *cebub_lookup_le(struct ceb_node **root, const void *key, size_t len);
struct ceb_node *cebub_lookup_lt(struct ceb_node **root, const void *key, size_t len);
struct ceb_node *cebub_lookup_ge(struct ceb_node **root, const void *key, size_t len);
struct ceb_node *cebub_lookup_gt(struct ceb_node **root, const void *key, size_t len);
struct ceb_node *cebub_next(struct ceb_node **root, struct ceb_node *node, size_t len);
struct ceb_node *cebub_prev(struct ceb_node **root, struct ceb_node *node, size_t len);
struct ceb_node *cebub_delete(struct ceb_node **root, struct ceb_node *node, size_t len);
struct ceb_node *cebub_pick(struct ceb_node **root, const void *key, size_t len);

/* version taking a key offset */
struct ceb_node *cebub_ofs_insert(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node, size_t len);
struct ceb_node *cebub_ofs_first(struct ceb_node **root, ptrdiff_t kofs, size_t len);
struct ceb_node *cebub_ofs_last(struct ceb_node **root, ptrdiff_t kofs, size_t len);
struct ceb_node *cebub_ofs_lookup(struct ceb_node **root, ptrdiff_t kofs, const void *key, size_t len);
struct ceb_node *cebub_ofs_lookup_le(struct ceb_node **root, ptrdiff_t kofs, const void *key, size_t len);
struct ceb_node *cebub_ofs_lookup_lt(struct ceb_node **root, ptrdiff_t kofs, const void *key, size_t len);
struct ceb_node *cebub_ofs_lookup_ge(struct ceb_node **root, ptrdiff_t kofs, const void *key, size_t len);
struct ceb_node *cebub_ofs_lookup_gt(struct ceb_node **root, ptrdiff_t kofs, const void *key, size_t len);
struct ceb_node *cebub_ofs_next(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node, size_t len);
struct ceb_node *cebub_ofs_prev(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node, size_t len);
struct ceb_node *cebub_ofs_delete(struct ceb_node **root, ptrdiff_t kofs, struct ceb_node *node, size_t len);
struct ceb_node *cebub_ofs_pick(struct ceb_node **root, ptrdiff_t kofs, const void *key, size_t len);
Loading

0 comments on commit a0205f9

Please sign in to comment.