descriptionRadix-ish tries
last changeThu, 25 Apr 2019 13:12:03 +0000 (15:12 +0200)

qp tries and crit-bit tries

I have been working on radix trees / patricia tries / crit-bit tries with a larger fan-out per branch to reduce lookup costs without wasting memory.

My best solution so far is the "qp trie", short for quelques-bits popcount patricia trie. (Nothing to do with cutie cupid dolls or Japanese mayonnaise!) A qp trie is like a crit-bit trie (aka patricia trie) except each branch is indexed by a few bits at a time instead of one bit. The array of sub-tries at a branch node is compressed using the popcount trick to omit unused branches.

The original version of qp tries used 4 bits at a time, so it was a quadbit popcount patricia trie; I also have a 5 bit version, a quintuple-bit popcount patricia trie, which generally outperforms the 4 bit version; and a 6 bit version which doesn't quite have a name.

Based on a few benchmarks, qp tries have about 1/3 less memory overhead of crit-bit tries, 1.3 words vs 2 words of overhead per item; the average depth of a qp trie is about half that of a crit-bit trie; and the overall speed of qp tries is about 30% faster than crit-bit tries. The qp trie implementation is about 40% bigger.


Type make test or make bench. (You will need to use GNU make.) If you have a recent Intel CPU you might want to add -mpopcnt to the CFLAGS to get SSE4.2 POPCNT instructions. Other build options:

The makefile builds {test,bench}-{qs,qn} with these options; they are otherwise the same as test-qp and bench-qp.


The code has only been tested on 64-bit little endian machines. It might work on 32-bit machines (provided the compiler supports 64 bit integers) and probably won't work on a big-endian machine. It should be easy to port by tweaking the struct bit-field layouts.

Key strings can be byte-aligned but values must be word-aligned; you can swap this restriction (e.g. if you want to map from strings to integers) by tweaking the struct layout and adjusting the check in Tset().

Keys are '\0' terminated C strings, which guarantees one key is not a prefix of another, so leaves and branches cannot occur at the same point. It should be possible to support arbitrary binary keys by being more clever about handling string termination.



Marek Vavrusa (CZ.NIC) and Devon O'Dell (Fastly) enthusiastically put this code to work and provided encouraging feedback.

Vladimír Čunát incorporated qp tries into CZ.NIC Knot DNS, at the suggestion of Jan Včelák.

Simon Tatham proved that parent pointers are not needed for embedded crit-bit tries.


You can clone or browse the repository from:



Written by Tony Finch; You may do anything with this. It has no warranty.

2019-04-25 Ondřej Hermanbind9: update git repository address master
2018-01-04 Tony FinchDo not try to compile broken WIP code
2017-06-02 Tony FinchRib compression for second search in Tset - probably...
2017-06-02 Tony FinchRib compression for first search in Tset
2017-06-02 Tony FinchRevert failed experiment
2017-06-02 Tony FinchFailed experiment
2017-06-02 Tony FinchMake Tget and Tdel search loops more similar
2017-05-28 Tony Finchgeneric leaves: some caveats
2017-05-27 Tony FinchConcatenate nodes when we can. Yowza.
2017-05-27 Tony FinchThe case where two trunks should be concatenated
2017-05-27 Tony FinchEven better
2017-05-27 Tony FinchMuch better clarity of thought about Tdel search loop
2017-05-26 Tony FinchCorrect return value!
2017-05-26 Tony FinchTdel() done, with much head-scratching. Not sure if...
2017-05-26 Tony FinchAvoid NULL returns from realloc()
2017-05-26 Tony FinchOther deletion cases. Moderately ugly.
3 weeks ago master