# Proposal: an opt-in emulator flag that makes map order independent of atom creation order

**URL:** <https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033>\
**Category:** Proposals: RFC\
**Tags:** maps, erlang-otp\
**Created:** [October 9, 2026, 9:16am UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033 "2026-10-09T09:16:12Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![anagrius](https://erlangforums.com/user_avatar/erlangforums.com/anagrius/32/3242_2.png) [@anagrius](https://erlangforums.com/u/anagrius)\
**Post date:** [October 9, 2026, 9:16am UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/1 "2026-10-09T09:16:12Z")

</div>

I’ve been working on making our Elixir application build reproducibly under Bazel. After enabling `deterministic`, fixing the build path and normalising timestamps, we still had a bunch of BEAM files that differed between builds of identical sources.

This matters to us because we share cached build results between developers and CI. When a compilation has to run again, we want unchanged inputs to produce unchanged output. A dependency producing a slightly different BEAM file can invalidate our Dialyzer PLT and the builds downstream of it, even though nothing meaningful has changed.

**The common cause was code iterating maps whose keys contained atoms.**

For small maps, atom keys are ordered by their atom-table indices. For large maps, the internal hash uses those indexes so iteration order depends on them too and atoms nested inside tuple or list keys can have the same effect.  
It is race condition which process happens to create a giiven atom first.

Parallel compilation is one source of variation but also the boot loader loads modules in parallel, before any of our build code runs. I found that even just two bare VM starts can assign different indices to thousands of atoms!

We found this showing up in several fairly ordinary places.. like for example:

- `Postgrex.ErrorCode` groups error codes into a map and then generates clauses from it.
- Spark walks option maps while generating functions. Other modules had differences from struct defaults in debug information, maps escaped into literals, or a map being inspected into `@doc`.

None of these generators should rely on an unspecified iteration order. Sorting in the generator CAN fix this but getting a reproducible build this way means finding every relevant call site in every dependency, including places where the map traversal happens inside the compiler. it was a bit of a wack-a-mole situation and dependencies can break it with a new vesion. So we looked into fixing it at the source.

## Previous Proof Earlier Wack-a-model fixes

I got the AI to look for previous reports of undeterminism in the map. There have already been fixes at several levels:

| Reference | What it addressed |
| --- | --- |
| OTP [#5153](https://github.com/erlang/otp/pull/5153?utm_source=chatgpt.com) | Large map literals could produce different BEAM files despite `deterministic`. Added deterministic encoding to `term_to_binary/2` and made the compiler use it for literals. |
| Elixir [#12350](https://github.com/elixir-lang/elixir/pull/12350?utm_source=chatgpt.com) | Reported unstable `exports_md5` values caused by encoding large maps. Closed without merging because Elixir already avoided that particular problem through list-based struct metadata. |
| Elixir [#12418](https://github.com/elixir-lang/elixir/pull/12418?utm_source=chatgpt.com) | Added explicit sorting when expanding struct fields following the map-ordering change in OTP 26. |
| Elixir [#13517](https://github.com/elixir-lang/elixir/pull/13517?utm_source=chatgpt.com) | Sorted map fields when converting type descriptions to quoted syntax, making the representation deterministic and fixing a flaky test. |
| OTP [#11748](https://github.com/erlang/otp/issues/11748?utm_source=chatgpt.com) → [#11774](https://github.com/erlang/otp/pull/11774?utm_source=chatgpt.com) | Atom creation order affected the compiler’s type analysis and generated code. The fix made traversal of exported functions use a consistent order. |

There is also related work in [`public_key` #11396](https://github.com/erlang/otp/pull/11396?utm_source=chatgpt.com). and a broader reproducible-builds issue, [#4417](https://github.com/erlang/otp/issues/4417?utm_source=chatgpt.com), dates back to 2020.

## What I tried

My first attempt was to generate a boot script that pre-interned the atoms used by the build in a fixed order. That helped, but it required a complete atom list as a build input. Both not practical and defeated the point of not compiling more than needed.

The prototype adds an opt-in emulator flag, `+zdao true`, for “deterministic atom order”. While i think this should be enabled under `deterministic` builds, I figured it would be easier to merge if it had its own flag initially. It defaults to `false`.

When enabled the internal hash uses the atom’s name hash rather than its table index, and small flat maps compare atom keys by term order rather than index order.

## The prototype

There is a [prototype based on OTP 29.1](https://github.com/erlang/otp/compare/OTP-29.1...berserkdb:otp:deterministic-atom-order), roughly 160 lines across ten files. There is also a [prebuilt Ubuntu 24.04 x86-64 release](https://github.com/berserkdb/otp/releases/tag/OTP-29.1-zdao.1).

I’d appreciate a review of that assumption, particularly around other consumers of the internal hash and any remaining code that assumes flat-map atoms are in index order.

We are now running this fork and it has stabilized out bazel build setup for the Elixir app.

(Edited to remove AI slop)

---

<div class="post-metadata">

**Author:** ![g-andrade](https://erlangforums.com/user_avatar/erlangforums.com/g-andrade/32/426_2.png) [@g-andrade](https://erlangforums.com/u/g-andrade)\
**Post date:** [October 9, 2026, 2:30pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/2 "2026-10-09T14:30:42Z")

</div>

Hi @anagrius. The merits of your proposal aside, the text style vibes of AI to the point of throwing me off - which I’m sure other people will also feel. I’m sorry if this is a false positive, it’s just very frustrating to get your attention flooded with LLM output these days.

---

<div class="post-metadata">

**Author:** ![anagrius](https://erlangforums.com/user_avatar/erlangforums.com/anagrius/32/3242_2.png) [@anagrius](https://erlangforums.com/u/anagrius)\
**Post date:** [October 9, 2026, 5:19pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/3 "2026-10-09T17:19:55Z")

</div>

I updated the text to have fewer AI slop details, and hopefully more clearly describe the issue and proposal. 👍

---

<div class="post-metadata">

**Author:** ![Schultzer](https://erlangforums.com/user_avatar/erlangforums.com/schultzer/32/2859_2.png) [@Schultzer](https://erlangforums.com/u/Schultzer)\
**Post date:** [October 10, 2026, 1:22pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/4 "2026-10-10T13:22:37Z")

</div>

Sounds like there would be a more straightforward way to generate consistent cache keys for your builds, most people use a hash of the mix files.

This is one reason why people sometimes states that compilers can be non-deterministic. Since the CPU is non-deterministic due to speculative execution.

I never heard about any regulated field require this level of determination especially since we have a bigger hardware issue.

---

<div class="post-metadata">

**Author:** ![anagrius](https://erlangforums.com/user_avatar/erlangforums.com/anagrius/32/3242_2.png) [@anagrius](https://erlangforums.com/u/anagrius)\
**Post date:** [October 10, 2026, 1:52pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/5 "2026-10-10T13:52:16Z")

</div>

@Schultzer fair questions, thank you for the reply 🙂

Hashing the Mix files is what we do for the compile step itself - - aand that part works well. The trouble is downstream in the build system. In Bazel (and Nix or similar tools) the outputs of one action are the inputs of the next build action , so the content hash of each BEAM file feeds the cache key of the dependents that compile against it, for example the Dialyzer PLT and the release artifacts. It is not something we can or would want to control ourselves.

If a dependency gets recompiled on a cold CI runner and its BEAM bytes come out different even though the sources did not change, everything after it is rebuilt too. A warm dependency cache hides this nicely until the day it is cold, which is exactly when you want the hits. In big monorepos like ours it can become make a huge difference in CI work + time.

Regarding speculative execution, I think we agree more than it sounds. SE changes timing, but the not results, so two runs may schedule differently but the instructions still produces same value. When compiler output depends on which process happened to intern an atom first, that is an ordinary race in software, and OTP seeems to already treats this class of thing as a bug:

that is what `+deterministic`, the deterministic `term_to_binary` encoding in #5153 and the ordering fix in #11774 I listed above fixed. This proposal is the same idea one level down instead of wack-a-mole solving each instance.

> I never heard about any regulated field require this level of determination especially since we have a bigger hardware issue.

There is no regulatory motive on our side. We just want small changes to reuse as much compiled work as possible, and two builds of the same exact same source to be verifiably the exact same output artifact

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [October 21, 2026, 8:11am UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/6 "2026-10-21T08:11:24Z")

</div>

[erts: Fix exact small map compare by garazdawi · Pull Request #11568 · erlang/otp · GitHub](https://github.com/erlang/otp/pull/11568) might this (still pending) PR solve your problem?

---

<div class="post-metadata">

**Author:** ![anagrius](https://erlangforums.com/user_avatar/erlangforums.com/anagrius/32/3242_2.png) [@anagrius](https://erlangforums.com/u/anagrius)\
**Post date:** [October 21, 2026, 2:44pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/7 "2026-10-21T14:44:47Z")

</div>

thanks for the pointer! That patch fixes comparisons of smal maps when they’re used as keys, affecting ordered iteration and deterministic encoding. The issue I’m seeing also occurs in ordinary map iteration, where atom creation order still affects traversal order. Code generators using that traversal can still produce different BEAM files between builds.

So I guess I should just make a PR at this point.

---

<div class="post-metadata">

**Author:** ![juhlig](https://erlangforums.com/user_avatar/erlangforums.com/juhlig/32/837_2.png) [@juhlig](https://erlangforums.com/u/juhlig)\
**Post date:** [October 21, 2026, 3:38pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/8 "2026-10-21T15:38:06Z")

</div>

Or maybe comment at the existing PR first, see if they can fix that at the same time?

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [October 21, 2026, 7:55pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/9 "2026-10-21T19:55:15Z")

</div>

Yeah, since this PR originated in turn from [`maps`: property tests by Maria-12648430 · Pull Request #11558 · erlang/otp · GitHub](https://github.com/erlang/otp/pull/11558) , the goal is clearly to have map iteration order deterministic in _all_ cases.

---

<div class="post-metadata">

**Author:** ![kiko](https://erlangforums.com/user_avatar/erlangforums.com/kiko/32/1630_2.png) [@kiko](https://erlangforums.com/u/kiko)\
**Post date:** [October 22, 2026, 11:47am UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/10 "2026-10-22T11:47:42Z")

</div>

Sorry @Maria-12648430 I am a bit swamp with stuff, but I will surely try to test your PR myself latest next week. I also wanted to learn property-based testing, which is why I need to find a bigger chunk of time. Thanks for your patience.

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [October 22, 2026, 12:13pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/11 "2026-10-22T12:13:02Z")

</div>

No worries @kiko 🙃

---

<div class="post-metadata">

**Author:** ![anagrius](https://erlangforums.com/user_avatar/erlangforums.com/anagrius/32/3242_2.png) [@anagrius](https://erlangforums.com/u/anagrius)\
**Post date:** [October 28, 2026, 3:44am UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/12 "2026-10-28T03:44:31Z")

</div>

I made a PR but it got closed by @jhogberg closed it because he doesn’t want flags that change code behavior, and pointed out that compilers that aren’t deterministic when asked to be should be fixed instead. That makes sense for issues like the ones in the compilers themselves (like #11748 mentioned in my ooriginal post).

But what a compiler fix can’t fix is code that runs at compile time. Not sure I got that point across. A macro or parse transform that iterates a map to generate clauses or literals emits them in the map’s iteration order. That order depends on atom indices, so on the order atoms were created in that VM. With parallel boot and parallel compilation, that order varies between runs, so identical sources give different BEAM files.

So I jave tried to make a no-flag version: make map order independent of atom creation order for everyone.

This gives slower microbench results, but no differencen in actual compiles of real-world codebases is neglegeble:

**Prototype** (benchmarked on x86-64; the ARM JIT falls back to the C hash and is untested) [erts: order and hash atom map keys by name hash (prototype) · berserkdb/otp@caa6d3f · GitHub](https://github.com/berserkdb/otp/commit/caa6d3fa9d4)

**Results** (EPYC 7502P, interleaved runs, median change vs stock OTP)

| | name hash | ordering by name |
| --- | --- | --- |
| Compile kernel, stdlib and compiler (90 s) | +0.2% | +0.3% |
| Elixir `make compile` (60 s) | +0.9% | +0.9% |
| `maps:from_list`, 8 to 32 atom keys | +16 to +68% | +20 to +527% |
| `maps:merge`, 16 + 16 keys | +3 to +24% | +40 to +272% |
| Dynamic-key lookup, 1000-key map | +13 to +26% | +34 to +49% |
| `persistent_term:get` | +3 to +19% | +50 to +70% |
| Literal-key match/update | about ±12% | about ±30% |
| Sort maps with differing keys | +4 to +9% | -70 to -78% |

As you can see, ordering by name (what my original flag did) is far too slow for module-name keys, which just confirms the reasoning in #6151 (which has caused my pain). Ordering by name hash costs nothing measurable in whole compiles, but small-map construction is clearly slower in isolation. While the numbers here look high:

**Compiling kernel, stdlib, the compiler iteself, and Elixir itself, shows no measurable difference (within ±1%).**

In my mind this solves my repeatability issue and prevents future compiler bugs and the whole macro situation.

**Questions**

1. Would ordering by name hash be acceptable unconditionally, given these costs? and if so what other benchmarks would you want to see (ARM64, specific workloads)? While it LOOKS like the cost is within noise on real world codebases, I would love a second opinion and benchmarks or more codebases.
2. If the microbenchmark cost is too high to impose on everyone, does that change the view on an opt-in mode, used only by build VMs?

The prototype is at [erts: order and hash atom map keys by name hash (prototype) · berserkdb/otp@caa6d3f · GitHub](https://github.com/berserkdb/otp/commit/caa6d3fa9d4), and the benchmark harness, raw results and steps to reproduce are at [otp/atom-order-bench at map-order-by-atom-name-hash · berserkdb/otp · GitHub](https://github.com/berserkdb/otp/tree/map-order-by-atom-name-hash/atom-order-bench).

---

<div class="post-metadata">

**Author:** ![josevalim](https://erlangforums.com/user_avatar/erlangforums.com/josevalim/32/353_2.png) [@josevalim](https://erlangforums.com/u/josevalim)\
**Post date:** [October 28, 2026, 9:10am UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/13 "2026-10-28T09:10:57Z")

</div>

First off, thank you for all of your contributions on making builds determinitic!

However, aren’t we just postponing the footgun with this change? Large maps still remain non deterministic, so once you cross the small → large map boundary, the same issue still appears, except now it is harder to detect.

For those reasons, I do think that trying to impose any ordering guarantee on maps is the wrong way to go about this. On the Elixir side, I’d rather try to find ways of making it clearer we are traversing a map during compilation, although how is not exactly clear at the moment.

---

<div class="post-metadata">

**Author:** ![anagrius](https://erlangforums.com/user_avatar/erlangforums.com/anagrius/32/3242_2.png) [@anagrius](https://erlangforums.com/u/anagrius)\
**Post date:** [October 28, 2026, 9:35am UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/14 "2026-10-28T09:35:30Z")

</div>

Hey Jose, thanks for the the reply!

This is not just changing to small-map ordering, also works for large.

So, besides the flatmap order optimization for small maps, an atom’s internal hash becomes its name hash instead of its atom index, and that hash is what decides the hashmap layout. So large maps don’t depend on atom creation order either, and crossing the small to large boundary doesn’t bring the problem back.

I checked this with large maps of up to 150 keys that are atoms with all kinds of scenarios. Each comes out in the same order when the atoms are created in opposite orders on stock OTP every one of them differs.

You know much better if this is the right direction, I just have the very irritating issue that I cannot cache my builds ☹

So, I’d very much welcome a way to make compile-time map traversal visible on the Elixir side. Today we only find them by building twice and diffing BEAM files, which only catches cases where the order happened to differ.

My instinct just was that this was a more systemic issue that should be solved deeper down.

---

<div class="post-metadata">

**Author:** ![mikpe](https://erlangforums.com/letter_avatar_proxy/v4/letter/m/b487fb/32.png) [@mikpe](https://erlangforums.com/u/mikpe)\
**Post date:** [October 28, 2026, 3:53pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/15 "2026-10-28T15:53:12Z")

</div>

The tools that want to produce deterministic results from non-deterministic operations (map order) are buggy and should be fixed. All it takes is to sort at the affected places.

Redefining map order to be deterministic burdens the VM and the entire community for the benefit of what is by comparison a niche issue.

---

<div class="post-metadata">

**Author:** ![krestenkrab](https://erlangforums.com/user_avatar/erlangforums.com/krestenkrab/32/3291_2.png) [@krestenkrab](https://erlangforums.com/u/krestenkrab)\
**Post date:** [October 29, 2026, 2:03pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/16 "2026-10-29T14:03:28Z")

</div>

It seems to me that this fix does two things:

1. Make the hash of atoms be deterministic (hash bytes of the atom rather than depend on intern order).
2. Sort map contents.

It seems that making atom hashing deterministic builds would make sense as a runtime-level flag as this will aid stability, and maybe the parts that need sorted maps should be done in the relevant compilers?

At any rate, we’re running with @anagrius version in our build pipeline for now.

---

<div class="post-metadata">

**Author:** ![Schultzer](https://erlangforums.com/user_avatar/erlangforums.com/schultzer/32/2859_2.png) [@Schultzer](https://erlangforums.com/u/Schultzer)\
**Post date:** [October 29, 2026, 6:10pm UTC](https://erlangforums.com/t/proposal-an-opt-in-emulator-flag-that-makes-map-order-independent-of-atom-creation-order/6033/17 "2026-10-29T18:10:25Z")

</div>

I’ve always wondered why this is something people want, not that there is anything wrong with, but given that you have absolutely no guarantee on how the CPU predicts stuff across architectures, then I don’t really get the benefit of all this determinism, although if it brings performance improvements then I’m game.
