If you need a jump table in Common Lisp, the naive way is to just expand to the equivalent ecase form:
Unfortunately, in most Common Lisp implementations, this simply expands to a linear series of checks for each value in turn, which is really not great if there are more than just a few cases.
This library provides ejumpcase
, which is much more efficient. On CCL, it actually expands to an ecase
which expands to a bunch of if
s, but a later stage of compilation actually detects the repeated comparisons on the index variable and converts them into a nice jump table. So ejumpcase
is ultimately O(1) on CCL. On other Common Lisp implementations, ejumpcase
expands to a binary search implemented as a tree of if
forms, offering still vastly superior performance compared to the typical ecase
expansion (if there are more than a few cases). But this really ought to be O(1), so help is requested to add such support for other Common Lisp implementations. Please open an issue or pull request if you know how to add support for this on other Common Lisp implementations.
Note that since version 1.1, when all cases are constant we can achieve O(1) dispatch even on unsupported implementations, thanks to vectorization. We can also achieve nearly O(1) dispatch if most cases are constant.
You can quickly get some rough test results like this: