Draft:Elixir/bzip2-ex: Difference between revisions
Convert direct links to references. Donno if this will turn out being the right choice. |
No edit summary |
||
Line 42: | Line 42: | ||
Elixir owes its existence to Erlang and the virtual machine that runs them both, BEAM<ref>https://blog.stenmans.org/theBeamBook/#_the_erlang_virtual_machine_beam</ref>. Extending the machine is done by writing a NIF library<ref>https://www.erlang.org/doc/man/erl_nif.html</ref> which consists of compiled C/C++ and a small Erlang stub providing the interface. Actually, the stub can be written in Elixir just as well, which was a nice surprise. | Elixir owes its existence to Erlang and the virtual machine that runs them both, BEAM<ref>https://blog.stenmans.org/theBeamBook/#_the_erlang_virtual_machine_beam</ref>. Extending the machine is done by writing a NIF library<ref>https://www.erlang.org/doc/man/erl_nif.html</ref> which consists of compiled C/C++ and a small Erlang stub providing the interface. Actually, the stub can be written in Elixir just as well, which was a nice surprise. | ||
== References == | |||
<references /> |
Revision as of 22:02, 7 September 2022
A chronicle of my first Erlang/Elixir library binding (NIF).
Adam Wight, Sept 2022
Background
One common way to analyze Wikipedia content is to process its database backup dumps[1], which are provided XML compressed using bzip2. The unpacked files are too large to manipulate locally, and even the compressed files can be unwieldly, so a common practice is to stream the content, decompress in memory, and analyze in a single pass. The output might be to extract a smaller subset of the data.
Of course, the normal approach would be to search for a proven library in a solid data-sciencey language such as Python, pip install mwxml[2] and go on with the day. That wouldn't be as interesting as trying to do exactly the same thing in an esoteric young language, in service of a pet project[3] with zero adoption.
"But imagine how much better a truly concurrent wiki dump processor could be!" —~~~
Problem statement
Unfortunately, there's no BEAM (Elixir- and Erlang-compatible) library for reading bzip2 files, so the options are either to fetch and run the data through an external process and bidirectional pipes, or write a new library.
How hard could it be to write a binding...
Is libbzip2 maintained?
Um, sure... The official bzip2 pages[4] might be hosted on a Red Hat site nobody has heard of since 2005, very likely written as static HTML, the project has no edits to its changelog[5] since 2019 (don't worry, the code has been changed[6] even if the changelog has not), but yes bzip2 1.0 is the current version dammit!
What isn't mentioned on the official page, but is well documented in the Wikipedia article, is that ownership has peacefully moved to a new author (while slipping through another would-be maintainer's fingertips) who will kindly take us into the future with bzip2 1.1 and beyond[7]—once this is released and mainstream, please edit my comments here.
Don't be confused just because I am.
The bzip2 file format has no specification
That's a bigger problem and we will ignore until it comes back angry—which it does.
High- or low-level integration?
We already chose the "hard landing" by writing a new library in the first place, but there's still an opportunity for making life sort of easy again, by choosing one of two APIs provided by libbzip2, either the high-level[8] interface which opens a file and returns the decompressed contents, or the low-level[9] interface for maximum interactivity, which is called with tiny bites of data, shares its memory with the caller, exposes an internal state machine, and offers your humble developer many forms of additional stimulation.
There were a few considerations here, and in theory the high-level interface would have made a perfectly acceptable binding. However, this would be a single call to decompress the world, and control flow wouldn't return to Elixir for several hours. This pretty much defeats the purpose of writing a binding library for a concurrent virtual machine.
Native implemented function (NIF)
This is where I began to feel like I was learning stuff.
Elixir owes its existence to Erlang and the virtual machine that runs them both, BEAM[10]. Extending the machine is done by writing a NIF library[11] which consists of compiled C/C++ and a small Erlang stub providing the interface. Actually, the stub can be written in Elixir just as well, which was a nice surprise.
References
- ↑ https://dumps.wikimedia.org/backup-index.html
- ↑ https://pythonhosted.org/mwxml/
- ↑ https://gitlab.com/adamwight/mediawiki_client_ex
- ↑ https://sourceware.org/bzip2/
- ↑ https://sourceware.org/bzip2/CHANGES
- ↑ https://sourceware.org/git/?p=bzip2.git
- ↑ https://gitlab.com/bzip2/bzip2/
- ↑ https://sourceware.org/bzip2/manual/manual.html#hl-interface
- ↑ https://sourceware.org/bzip2/manual/manual.html#low-level
- ↑ https://blog.stenmans.org/theBeamBook/#_the_erlang_virtual_machine_beam
- ↑ https://www.erlang.org/doc/man/erl_nif.html