Skip to content

Dictionary data structures library #1640

Closed

Description

@jeromekelleher

There's a number of places in which we're missing having efficient dictionary data structures in tskit (mainly hash tables and AVL trees). See #1639 as a current example.

I wanted to start this thread to explore some possibilities for libraries that we could use, before we start considering rolling our own.

Some requirements:

  1. Has to be fully cross platform
  2. Embeddable - we can include it in our build, an external library dependency is not an option
  3. License compatible with our MIT licence
  4. Multi-threaded - anything that uses global library-level memory would be treated with deepest suspicion.
  5. "Good". Other people should be using it in production, preferably for a long time.
  6. Full error checking compatible with our model
  7. Not have a terrible API

Here's some options:

libdict

Pros:

  • Extensive suite of dictionary types
  • Looks well written, clean OO type API
  • 68 stars/42 forks on GitHub, so presumably reasonably well used

Cons:

  • Not much documentation; this is all I could find.
  • No guidance on how to embed (but Makefile looks pretty simple, and someone has make a PR showing how to build with meson
  • No releases

klib

Pros

  • Specifically designed for embedding
  • Has lots of other things we might want at some point
  • Extensively used (embedded in htslib, AFAIK) - 3.4K stars
  • Pretty good documentation linked from the README
  • Very efficient (probably - haven't looked at any actual benchmarks)

Cons

  • Container types like hash tables use macros (efficient, but very hard to read)
  • Coding style is extremely dense, very difficult to read IMO (especially matters when implemented via macros)

Activity

  1. jeromekelleher commented on Aug 23, 2021

    @jeromekelleher
    MemberAuthor

    Looking into libdict a bit more, the code does generally look good. The only thing I don't like really is that the library does all the mallocing rather than allowing the user to take control of mallocing the tree nodes. There is some facility to override this, but it's at the library level (you set the dict_malloc_func, which is then expanded by the MALLOC macro here), but this is worse. Maybe I'm worrying too much about keeping a lid on the number of small mallocs, though, and the library does look like it handles malloc errors pretty well.

  2. added this to the C API 1.0.0 milestone on Aug 23, 2021
  3. molpopgen commented on Aug 30, 2021

    @molpopgen
    Member

    Libdict sounds promising. Seems like making a quick set of benchmarks against the old libavl is worth it.

  4. jeromekelleher commented on Sep 1, 2021

    @jeromekelleher
    MemberAuthor

    I had a go at integrating libdict, and jeez this is tedious. I'm tempted to just get my copy of TAOCP and implement a balanced tree from scratch.

  5. molpopgen commented on Sep 1, 2021

    @molpopgen
    Member

    Where's that XKCD about competing standards? ;)

  6. jeromekelleher commented on Sep 6, 2021

    @jeromekelleher
    MemberAuthor

    For the record, we went with an implementation of Knuth's version of an AVL tree. Much simpler than integrating an external library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions