nseaSeb

nseaSeb

Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (something like “vector lib” or similar?). I’m having trouble finding the information since I didn’t write it down; could a kind soul refresh my memory and perhaps share the details with us?
Best regards,

Showing Posts 1 to 10

mudasobwa

mudasobwa

Creator of Cure

What exactly are you looking for? A data structure? An algorithm? A library?

If you need an optimized traversal, what would be wrong with …ahem… inversing a list and working with this inversed copy?

nseaSeb

nseaSeb OP

I don’t recall exactly what I discovered, but I believe it is possible to declare “optimized” lists. By default, lists consist of [variable, pointer to next variable], which can be slow to iterate over.

mudasobwa

mudasobwa

Creator of Cure

Optimized for what? Random access? Traversal? There is no such thing as “optimized for everything” stuff.

Well. Linked lists are done this way (this description is extremely simplified though.) This is the fastest possible data structure to iterate over. It might be slow at random access, concatenation, whatever else, but the fast iterating is the reason these lists ever exist.

You really should make up you mind about what are you after.

nseaSeb

nseaSeb OP

Sorry if this isn’t clear; I believe it’s Erlang vectors, or something like that.
O(1) vs. O(n) to traverse them.

mudasobwa

mudasobwa

Creator of Cure

O(1) to traverse anything is obviously impossible.

Asd

Asd

Traversal can’t be O(1) cause its like, traversal of all items. Do you want access by index (like array[12] in C syntax)? How do you want to add elements to your collection? What type of elements do you want to store? Do you want to access it concurrently from different processes? Is it write once structure? Do you want to associate elements with keys?

Describe the problem in the detail, please. Erlang and Elixir have a lot of different structures for different use-cases. If you want something similar to cpp vectors, then you want lists, tuples, binaries or :arrays, depending on what exactly you want to do with it

nseaSeb

nseaSeb OP

list = [1, 2, 3, 4, 5] Enum.at(list, 3)
O(n) to traverse

Asd

Asd

Its called “access by index”. “To traverse” means “to iterate over all elements”.

So if you want constant access by index, you need tuples, binaries or atomics, depending on your problem. You can even use integers to store byte arrays. But all these data structures are O(n) when an element is changed or added

There is also :array module for O(ln n) for access and change by index. You can also use maps for similar performance. There is also :queue module for queues, etc

nseaSeb

nseaSeb OP

Thanks everyone,
I just found what I was looking for

ausimian

ausimian

For iteration of n elements, linked lists are O(n), but then so is a tuple.

On real computers, the actual iteration performance of a linked-list will be dominated by the runtime layout of its cells. A large list composed of cons cells scattered irregularly around the process heap will be substantially slower to iterate than an array, as prefetch won’t hit the L1 cache as often. A list with its cons cells laid out contiguously will do much better, but still loses on L1 cache density. And even a contiguous list causes stalls as the next cell is unknown until this cell’s next pointer is visible (speculative execution helps here, but it typically helps array iteration a lot more).

Linked lists are, in theory, one of the worst data structures for modern computer architectures.

In practice, large linked lists are reasonably rare, GC compaction may bring cells closer together and its possible a few BIFs may result in contiguous cons cells anyway. If iteration is all you need, lists are still fine. Also, iterating tuples is a bit weird in Elixir. And lists are immutable and elegant.

As a side note, the code the JIT produces for the list iteration looks better than the code it produces for tuple iteration, assuming the caller also checks tuple-size before accessing the element. The lack of cross-module optimisation means the size check is done twice and more branches are also taken on the happy path.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
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
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews