critbit: Crit-bit maps and sets

[ bsd3, data, library ] [ Propose Tags ]

This package implements crit-bit trees, a key-value container type for storing keys that can be treated as bitstrings (e.g. ByteString and Text).

Compared to the data structures from the containers and unordered-containers packages, you will find that sometimes the functions implemented in this package are faster, sometimes slower.

In many cases, a CritBit tree provides performance close to that of a HashMap, while providing ordered storage and traversal like a Map.

[Skip to Readme]


Automatic Flags

operate in developer mode


Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,
Dependencies array, base (>=4 && <4.11), bytestring (>=0.9), deepseq, text (>= && <1.3), vector [details]
License BSD-3-Clause
Copyright 2013-2014 Bryan O'Sullivan and others
Author Bryan O'Sullivan <>
Maintainer Bryan O'Sullivan <>
Revised Revision 2 made by Bodigrim at 2022-01-16T17:59:23Z
Category Data
Home page
Bug tracker
Source repo head: git clone
head: hg clone
Uploaded by BryanOSullivan at 2014-07-04T05:40:07Z
Distributions Arch:
Reverse Dependencies 2 direct, 2 indirect [details]
Downloads 7052 total (18 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Successful builds reported [all 1 reports]

Readme for critbit-

[back to package description]

Crit-bit trees for Haskell

This is the first purely functional implementation of crit-bit trees that I'm aware of.

A crit-bit tree is a key/value container that allows efficient lookups and ordered traversal for data that can be represented as a string of bits.

This package exists in part with education in mind:

  • The core data structures are simple.

  • The core algorithms are easy to grasp.

  • I have intentionally structured the source to be easy to follow and extend.

  • Originally, I deliberately left the package incomplete. (It has since been substantially fleshed out.) Ever thought to yourself, "I'd write a bit of Haskell if only I had a project to work on"? Well, here's your chance! I will set aside time to review your code and answer what questions I can.

Education aside, crit-bit trees offer some interesting features compared to other key/value container types in Haskell.

  • For some operations, they are much faster than Data.Map from the containers package, while for others, they are slower.

  • Compared to Data.HashMap, you get about the same lookup performance, but also some features that a hash-based structure can't provide: prefix-based search, efficient neighbour lookup, ordered storage.

Of course crit-bit trees have some downsides, too. For example, building a tree from randomly ordered inputs is somewhat slow, and of course the set of usable key types is small (only types that can be interpreted as bitstrings "for free").

Compared to the most easily findable crit-bit tree code you'll come across that's written in C, the core of this library has a lot less accidental complexity, and so may be easier to understand. It also handles arbitrary binary data that will cause the C library to go wrong.

How to contribute

I've purposely published this package in an incomplete state, and I'd like your help to round it out. In return, you get to learn a little Haskell, have your code reviewed by someone who wants to see you succeed, and contribute to a rather nifty library.

Do you need any prior experience with Haskell to get started? No! All you need is curiosity and the ability to learn from context. Oh, and a github account.

My aim with this library is drop-in API compatibility with the widely used Haskell containers library, which has two happy consequences:

  • There are lots of functions to write!

  • In almost every case, you'll find a pre-existing function in containers that (from a user's perspective) does exactly what its counterparts in this library ought to do.

Getting started

If you want to contribute or play around, please use the most modern version of the Haskell Platform.

Once you have the Platform installed, there are just a few more steps.

Set up your local database of known open source Haskell packages.

cabal update

Both the new cabal command and cabal-dev will install to $HOME/.cabal/bin, so put that directory at the front of your shell's search path before you continue.

Get the critbit source.

git clone git://

Set up a sandbox.

The first time through, you may need to download and install a ton of dependencies, so hang in there.

cd critbit
cabal sandbox init
cabal install \
--enable-tests \
--enable-benchmarks \
	--only-dependencies \

The -j flag above tells cabal to use all of your CPUs, so even the initial build shouldn't take more than a few minutes.

To actually build:

cabal build

Running the test suite

Once you've built the code, you can run the entire test suite fairly quickly. This takes about 30 seconds on my oldish 8-core Mac laptop:

dist/build/tests/tests +RTS -N

(The +RTS -N above tells GHC's runtime system to use all available cores.)

If you're feeling impatient, run a subset of the test suite:

dist/build/tests/tests -t properties/map/bytestring +RTS -N

And if you want to explore, the tests program accepts a --help option. Try it out.

Running benchmarks

It is just as easy to benchmark stuff as to test it.

First, you need a dictionary. If your system doesn't have a file named /usr/share/dict/words, you can download a dictionary here.

If you've downloaded a dictionary, tell the benchmark suite where to find it by setting the WORDS environment variable.

export WORDS=/my/path/to/linuxwords

You can then run benchmarks and generate a report. For instance, this runs every benchmark that begins with bytestring/lookup.

dist/build/benchmarks/benchmarks -o lookup.html \

Open the lookup.html file in your browser. Here's an example of what to expect.

As with tests, run the benchmarks program with --help if you want to do some exploring.

What your code should look like

Okay, so you've bought into this idea, and would like to try writing a patch. How to begin?

I've generally tried to write commits with a view to being readable, so there are examples you can follow.

For instance, here's the commit where I added the keys function. This commit follows a simple pattern:

Naturally, you'll follow the prevailing coding and formatting style. If you forget, I'll be sad and offer you only a terse "fix your formatting" review, and then you'll be sad too.

What your commits should look like

Please follow the guidelines below, as they make it easier to review your pull request and deal with your commits afterwards.

  • One logical idea per commit! If you want to add five functions, that's fine, but please spread them across five commits.

  • Do not reorganize or refactor unrelated code in a commit whose purpose is to add new code.

  • When you add a new function, add its tests and benchmarks in the same commit.

  • Do not add trailing whitespace. Follow the same formatting and naming conventions as you already see in the code around you.

  • Keep your maximum line length at 80 columns for everything except lines of example code in documentation.

(If you can't follow the guidelines, there's a good chance I'll ask you to fix your commits and resubmit them.)