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,
Trending in Questions
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
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
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
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
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
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
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
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
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mudasobwa
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
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
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
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
O(1)to traverse anything is obviously impossible.Asd
Traversal can’t be
O(1)cause its like, traversal of all items. Do you want access by index (likearray[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 itnseaSeb
list = [1, 2, 3, 4, 5] Enum.at(list, 3)
O(n) to traverse
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 addedThere is also
:arraymodule forO(ln n)for access and change by index. You can also use maps for similar performance. There is also:queuemodule for queues, etcnseaSeb
Thanks everyone,
I just found what I was looking for
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.