MyNixOS website logo
Description

Bencode encoding and decoding library.

benc

Hackage Haskell-CI

Bencode encoding and decoding library

Bencode

Bencode is a simple encoding format for loosely structured data, comparable to JSON. It is used primarily in the BitTorrent protocol. For a description of the format see

Features

This library offers

  • A nice API
  • Correctness
  • Speed

This library does not attempt to support

  • Lazy or incremental parsing
  • Failing with detailed error messages

Getting started

Please see the Haddocks for Data.Bencode.Decode and Data.Bencode.Encode.

Alternatives

There are currently three other Bencode libraries on Hackage:

All of these are in some combination of buggy, slow, and unmaintained.

Click for details
  • bencode:
    • Bugs (e.g. crashes on input "i-e")
    • Very slow parsing
    • No high-level encoding API
    • [Minor] Lax parsing (e.g. admits the invalid "i-0e")
  • AttoBencode
    • Slow parsing
    • [Minor] Lax parsing (e.g. admits the invalid "i-0e")
  • bencoding
    • Bugs (e.g. crashes on parsing non-UTF-8 into Text)
    • Questionable design of dict encoding/decoding API, where human error can lead to mis-parsing Bencode or writing invalid Bencode.
    • [Minor] Lax parsing (e.g. admits the invalid "i-0e")

API comparison

See the benchmark file as a comparison point of the library APIs.

Benchmarks

Below is a comparison of decoding and encoding of two torrent files, performed with GHC 9.6.3. See the benchmark file for details.

Decoding

Librarycrossref timeallocubuntu timealloc
benc21.3 ms ± 902 μs24 MB1.30 μs ± 90 ns3.6 KB
bencode218 ms ± 7.7 ms737 MB29.0 μs ± 2.6 μs121 KB
AttoBencode44.6 ms ± 4.0 ms129 MB3.01 μs ± 102 ns17 KB
bencoding39.1 ms ± 2.3 ms104 MB2.44 μs ± 175 ns15 KB

Note: bencode parses from a lazy ByteString unlike the rest which parse from strict ByteStrings, and so is expected to be a little slower.

Encoding

Librarycrossref timeallocubuntu timealloc
benc9.17 ms ± 487 μs42 MB1.58 μs ± 101 ns11 KB
bencode37.8 ms ± 860 μs113 MB3.17 μs ± 174 ns19 KB
AttoBencode19.7 ms ± 1.8 ms109 MB10.1 μs ± 993 ns295 KB
bencoding11.9 ms ± 916 μs67 MB1.81 μs ± 129 ns15 KB

Note: AttoBencode encodes to a strict ByteString via a lazy ByteString, unlike the rest, which only prepare the lazy ByteString. As such, it is expected to be slower.

Contributing

Questions, bug reports, documentation improvements, code contributions welcome! Please open an issue as the first step.

Metadata

Version

0.1.1.0

License

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows