bokner

bokner

InPlace is an experimental library that has implementations of several ADTs based on atomics module

In short, the ‘atomics’ API allows operations on mutable arrays of integers.

What is implemented:

  • arrays
  • stacks
  • queues
  • heaps
  • priority queues
  • linked lists

Check out the implementation of X algorithm by Donald Knuth, accompanied by the implementation of Sudoku as an example of usage.

X algorithm is built on the idea of Dancing Links, which relies on the modification of linked lists in place.
The implementation of Dancing Links with immutable data is usually considered to be close to impossible :slight_smile:

https://github.com/bokner/inplace

Showing Posts 1 to 8

garrison

garrison

lmao this is so unhinged I absolutely love it.

Does anyone know how the lookup from reference => atomic is actually done internally? Is there a table, or something more clever? I feel like you could do some sort of inline caching-style trick.

Schultzer

Schultzer

This is awesome to see, although be careful about atomics as they can be slow, I had to completely avoid them for my SOTA pool implementation as they got extremely slow due to how schedulers work in conjunction with processes that can move around freely.

If you can keep your atomics on each scheduler then you can avoid global contention.

bokner

bokner OP

I just found out that Donald Knuth had his birthday yesterday.
I have to say that a sizeable part of my motivation to do this project was to have more of Knuth algos in Elixir :-).

garrison

garrison

Rediscovering Erlang from first principles lol.

But they should be faster than any other way to do shared memory across cores, right? I assume you have a use-case where you want shared memory on each core but not across cores? Like the ETS decentralized_counters trick?

Schultzer

Schultzer

They are indeed faster, and my first attempt with atomics was extremely fast, I only saw the issues when I had to push harder for correctness and extreme concurrency where my pool ended up allowing multiple processes to use the same connection at the same time.

So I had to lean into the BEAM for correctness which also reduced cache misses, I could properly have kept the original design and just built a queue, but it got to a place where I had to trade simplicity vs complexity.

All that being said, I want to see more of this, since we can get bare metal performance on the BEAM without writing low level code.

garrison

garrison

I’m using :atomics in XKS (Hobbes’s storage engine) to synchronize the LSM epoch so the readers can quickly check the current epoch from the single writer without message passing. When they’re done with an epoch they can just message the writer what epoch they’re on and it can GC old tables. I’ll write more about it soon; I think it’s pretty optimal :atomics usage. I don’t see how anything else could be faster given that even ETS will take locks.

But I am interested in mutable data structures (like the OP) because I’ve found that mixing mutable and immutable state leads to bugs. E.g. I had code that would update an ETS table and then return a new struct, and because the mental model was that of mutable state (this is a database) I on multiple occasions forgot to keep the new struct, and this caused very subtle bugs. Thankfully I have aggressive correctness tests, but it’s easier if you don’t write bugs to begin with :slight_smile: I try to store metadata like that in the ETS table itself now so that it can’t get out of sync.

Immutability is great for preventing bugs, but not in every situation.

Asd

Asd

Reference is literally a pointer into the memory to the refcounter. And the atomic is just a refcounted binary

garrison

garrison

Oh, that’s interesting.

References themselves are heap terms so we have to chase two pointers to get the atomic, correct? Or would that be three with the refcounting?

My understanding of the reference encoding comes from here, but it seems incomplete. I assume the atomics references take up the same space but have a pointer encoded in them? And I assume they must be tagged somehow?

Are other uses of reference overloaded in this way? E.g. an ETS table ref, or monitor ref?

I should really start reading the source :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Announcing Top

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ausimian
Castle is a package I put together to assist with building Elixir releases capable of being live-upgraded via the ‘official’ release hand...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews