bryanhuntesl

bryanhuntesl

I’ve done a lot of GitHub searching but I can’t find an erlang/elixir data structure similar to Java’s SortedMap.

I’ve been using ETS :sortedset but the performance hasn’t been sufficient for my needs. Anyone got any suggestions ?

Need to do iterations and multiple lookups …

Showing Posts 1 to 10

wolf4earth

wolf4earth

Have you considered using this library?

mpope

mpope

There is an Erlang gb_tree that is built in. For ordered iteration, there is an iterator/1, and to_list/1 can be used with Enum I think. gb_sets is also available.

Sadly it’s interface isn’t compatible with Elixir pipes, because the data structure is the last argument but an Elixir wrapper shouldn’t be too hard.

japplegame

japplegame

If you need custom ordering, consider AVLTree

bryanhuntesl

bryanhuntesl OP

I’m not sure from the project docs it’s doing what I need.

bryanhuntesl

bryanhuntesl OP

Had a look. I’ll throw in a couple of examples now and maybe benchmark later.

Create new :

:gb_trees.empty

Populate with 10 elements :
gb = 1..10 |> Enum.reduce(:gb_trees.empty, fn x,acc -> :gb_trees.insert(x,x,acc) end)

Iterating elements :
{k,v,next} = :gb_trees.next(iter)

I will mention I started looking at native code to solve my issue (need high performance). Here’s what I also considered.. After some investigation, I came across klib library (pure C, license X11/MIT) and it’s KBtree ordered map implementation. Considering wrapping it with a NIF.

mpope

mpope

If you need the high performance, you could still leverage ets for direct access and use a separate data structure just for iteration of the keys, but that does get cumbersome.

Nifs are tricky :slight_smile: you can take a look at this (very similar) nif I write to wrap a C data structure (also a single header lib). If you need to load a huge amount of items into that tree consider using a dirty nif as not to block the whole VM while it scans the tree. That also comes with overhead of it’s own though.

dimitarvp

dimitarvp

This probably calls for a generic Rustler-based Elixir library that bridges various Rust data structures to Elixir code. In your case that’d be Rust’s BTreeMap.

Discord have a sorted set library with bindings to Rust – not a map but still, in case you need this one as well. You can also just take a look at Discord’s libraries in general, they are quite excellent.

bryanhuntesl

bryanhuntesl OP

Yeah everyone speaks well of Rust for native code - I haven’t taken the time to learn it so worried about the cognitive load but I do know C/C++ . Should probably watch Scroggin’s talk again but I’m spending so much time on beam/infrastructure I’ve not much chance to learn a new language. Excuses. Excuses.

bryanhuntesl

bryanhuntesl OP

Thanks @mpope some good points there. exor_filter/c_src/xor_filter_nif.c at master · mpope9/exor_filter · GitHub yeah good example of resource allocation/deallocation - at least it’s reasonably straightforward in BEAM compared to say for example writing a Java native extension. Good point about the dirty NIF.

Adzz

Adzz

Can you share the use case for an ordered map? I super curious!

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews