{"id":21005129,"url":"https://github.com/wtarreau/cebtree","last_synced_at":"2025-05-15T01:33:26.046Z","repository":{"id":209366265,"uuid":"723860966","full_name":"wtarreau/cebtree","owner":"wtarreau","description":"Compact Elastic Binary Trees: only require two pointers, like a doubly-linked list, to build a tree. Duplicates not implemented for now, but algorithmically supported.","archived":false,"fork":false,"pushed_at":"2025-05-04T09:19:32.000Z","size":950,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-04T09:31:36.773Z","etag":null,"topics":["binary-search-tree","ebtree"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wtarreau.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-11-26T23:13:27.000Z","updated_at":"2025-04-27T12:35:06.000Z","dependencies_parsed_at":"2024-01-02T08:02:16.000Z","dependency_job_id":"9ddd5cad-c317-48cb-95f1-bc5240cdf834","html_url":"https://github.com/wtarreau/cebtree","commit_stats":null,"previous_names":["wtarreau/cbtree"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtarreau%2Fcebtree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtarreau%2Fcebtree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtarreau%2Fcebtree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wtarreau%2Fcebtree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wtarreau","download_url":"https://codeload.github.com/wtarreau/cebtree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254256612,"owners_count":22040318,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-search-tree","ebtree"],"created_at":"2024-11-19T08:39:23.333Z","updated_at":"2025-05-15T01:33:26.037Z","avatar_url":"https://github.com/wtarreau.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Compact Elastic Binary Tree (cebtree)\n\n## Abstract\n\nCEBTree is a form of very compact binary search tree that only requires two\npointers (like a list) to index data. The structure is a much more compact\nvariant of the [EBTree](https://github.com/wtarreau/ebtree) structure, so no\nallocation is needed when inserting a node. There are no upward pointers so\nsome operations are slower as they will require a preliminary lookup. For\nexample a `next()` operation requires a first descent to identify the closest\nfork point, then a second descent from that optimal point.\n\nBut the structure provides a number of other benefits. The first one being the\nmemory usage: **the tree uses the same storage as a list**, thus can be\ninstalled anywhere a list would be used. This can be particularly interesting\nfor read-mostly data (configuration, append-only indexes etc). It preserves\nstructure alignment, thus does not require to contain the data itself, the data\nmay be appended just after the pointer nodes, which saves the need for typed\ntrees thus typed operations. It may also make the code a bit cleaner, because\nwith EBTree it's often tempting to touch node-\u003ekey from the main code, without\nalways realizing the impacts (namely with signed values).\n\nIt should also be easier to implement variants (e.g. case insensitive strings\nlookups, or faster memory lookups matching one word at a time, etc) thanks to\nthe unified data types.\n\n## Properties\n\nJust like EBTrees, duplicate keys are supported and are visited in insertion\norder. This allows for duplicate detection and graceful handling for example\nin configuration files, as well as timer management (though ebtrees are much\nfaster for timer management, albeit bigger).\n\n#### Comparison of costs by model\n\nFor more info please consult the [EB vs CEB benchmark](results/bench-eb-ceb/)\nthat was run using the [bench utility](tests/bench.c).\n\n|             model |      list       | hash (B buckets)   |      rbtree     |     cebtree     |      ebtree       |\n|-------------------|:---------------:|:------------------:|:---------------:|:---------------:|:-----------------:|\n|__operation__      | min / avg / max |  min / avg / max   | min / avg / max | min / avg / max |  min / avg / max  |\n|lookup ops         |  - / O(N) / -   |  1 / N/B / O(N)    | - / O(logN) / - | - / O(logN) / - |  - / O(logN) / -  |\n|insert ordered     |  - / O(N) / -   |  1 / N/B / O(N)    | - / O(logN) / - | - / O(logN) / - |  - / O(logN) / -  |\n|append (unordered) |  - / O(1) / -   |  - / O(1) / -      | - / O(logN) / - | - / O(logN) / - |  - / O(logN) / -  |\n|delete             |  - / O(1) / -   |  - / O(1) / -      | - / O(logN) / - | - / O(logN) / - |  - /   O(1)  / -  |\n|first/last         |  - / O(1) / -   |  1 / N/B / O(N)    | - / O(logN) / - | - / O(logN) / - |  - / O(logN) / -  |\n|next/prev          |  - / O(1) / -   |  1 / O(1) / O(N/B) | O(1) / O(1) / O(logN) | - / O(logN) / - | O(1) / O(1) / O(logN) |\n||\n|__Costs per operation__|\n|string lookup cost|    N*strcmp()    |    N/B*strcmp()    | ~logN*strcmp()  |  ~1*strcmp()    |    ~1*strcmp()    |\n|visited nodes     |         N        |        N/B         |    2*logN       |    2*logN       |      1*logN       |\n\n#### Synthetic performance comparison\n- enumeration in insertion order: lists \u003e ebtree = rbtree \u003e cebtree\n- enumeration in key order: ebtree \u003e cebtree = rbtree \u003e lists\n- random lookups: ebtree \u003e cebtree = rbtree \u003e lists\n- random deletion: lists \u003e ebtree \u003e cebtree = rbtree\n- total purge: lists \u003e ebtree \u003e cebtree = rbtree\n\nThe tagged pointers permit the string lookup cost to remain low. Without tagged\npointers (i.e. version 0.2), the string lookup cost becomes logN*strcmp() since\na complete string needs to be compared at each layer (like in other non-radix\ntrees such as rbtree).\n\n## API\n\nThe application integration and API are documented in [this document](doc/API.md).\n\n## Limitations and future improvements\n\nRelative addressing is not yet implemented but is in progress. This is handy to\nmanipulate data in memory areas shared between multiple processes, where no\npointer is stored.\n\nPerformance is a bit lower than EBTrees even for small keys due to the need to\nread both branches at each node to figure whether to stop or continue the\ndescent. It effectively doubles the number of visited nodes (hence is less TLB\nfriendly), though it does not necessarily increase the memory bandwidth since\nthe nodes are much smaller.\n\nIt was verified that an almost lockless approach could be implemented: lookups\nand insertion could be done without locking but deletion requires locking. As\nsuch, an approach would require [rwlocks](https://github.com/wtarreau/plock)\nwith shared locking for insertion and lookup, and exclusive locking for\ndeletion. This might come as an advantage over EBTrees for highly contended\nenvironments.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwtarreau%2Fcebtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwtarreau%2Fcebtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwtarreau%2Fcebtree/lists"}