Skip to content

fix: replace per-row __id__ clauses with Map lookup#68

Merged
giustinoesposito-prima merged 2 commits intomasterfrom
fix/arm64-jit-clause-limit
Feb 23, 2026
Merged

fix: replace per-row __id__ clauses with Map lookup#68
giustinoesposito-prima merged 2 commits intomasterfrom
fix/arm64-jit-clause-limit

Conversation

@giustinoesposito-prima
Copy link
Contributor

The ARM64 JIT backend in OTP 25 (erts-13.2) silently generates incorrect machine code when a function has more than 4096 clauses of integer pattern matching, causing all calls to fall through to the catch-all clause and return nil.

Previously, gen_internal_id/3 generated one def __id__(N) clause per CSV row plus a catch-all. For large CSVs (e.g. comuni.csv with 7896 rows) this exceeded the 4096-clause threshold, breaking lookups on ARM64 (Apple Silicon via Docker).

Replace the N individual clauses with a single @__csv_map__ module attribute storing a %{id => changeset} map, and a single def __id__/1 that performs a Map.fetch/2 lookup.

The ARM64 JIT backend in OTP 25 (erts-13.2) silently generates
incorrect machine code when a function has more than 4096 clauses
of integer pattern matching, causing all calls to fall through to
the catch-all clause and return nil.

Previously, `gen_internal_id/3` generated one `def __id__(N)` clause
per CSV row plus a catch-all. For large CSVs (e.g. comuni.csv with
7896 rows) this exceeded the 4096-clause threshold, breaking lookups
on ARM64 (Apple Silicon via Docker).

Replace the N individual clauses with a single `@__csv_map__` module
attribute storing a `%{id => changeset}` map, and a single
`def __id__/1` that performs a Map.fetch/2 lookup.
@giustinoesposito-prima giustinoesposito-prima marked this pull request as ready for review February 23, 2026 11:51
@giustinoesposito-prima giustinoesposito-prima requested a review from a team as a code owner February 23, 2026 11:51
@giustinoesposito-prima giustinoesposito-prima requested review from eFFeeMMe and opsidao and removed request for a team February 23, 2026 11:51
@giustinoesposito-prima giustinoesposito-prima enabled auto-merge (squash) February 23, 2026 11:51
Copy link

@claudio-dalicandro claudio-dalicandro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me the VM optimization compared to a Map lookup, which is O(n) anyway, is not relevant in this context. It is true that bumping the OTP version would be enough, and we should do that regardless, but I would go ahead with this change once the Credo violation is fixed, so we can immediately address a bug that is quite hard to detect (Kudos!!).

Co-authored-by: Claudio D'Alicandro <claudio.dalicandro@gmail.com>
@giustinoesposito-prima giustinoesposito-prima merged commit c06c96f into master Feb 23, 2026
5 checks passed
@giustinoesposito-prima giustinoesposito-prima deleted the fix/arm64-jit-clause-limit branch February 23, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants