Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

We have a file that has lines with IP ranges like the following:

IP1,     IP2,      Country, Region
1.0.0.1, 1.0.0.10, pt,      l

We use this file to know where an IP belongs to. So, for example, If I am given the IP 1.0.0.9, because it is between IP1 and IP2, I know it is in Portugal, Lisbon.

As you may imagine, this file has millions of lines like this. We need an efficient way to get the location of an IP.

ETS

ETS tables are known for being fast. So one of my first reactions was: what if I put this file in an ETS table with keys / values and then use pattern matching to check if a given IP is within the range?

Problem

However one of my co-workers pointed that although this would be possible using select the complexity would be O(n) for worst case, meaning I would run the entire table until I pattern matched into the answer.

This is not acceptable, so I was wondering if there is a more efficient way of doing this using ETS tables.

Any suggestions?

Showing Posts 1 to 8

NobbZ

NobbZ

Compile into a module at runtime. Very bad load time and in general not good write times, but fast for read, constant time.

There are even packages abstracting those away for you.

idi527

idi527

There are even packages abstracting those away for you.

And there’s also Locus: Geolocation and ASN lookup of IP addresses

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is true of select generally because the patterns can be arbitrarily complex. That doesn’t mean that a range check is necessarily O(n) worst case. I’d do some testing and see.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

We tried doing that but we ran out of atoms. When we increased the number of atoms, we ran out of RAM at compile time. We are still looking into it though…

Due to current company policies, this is not an option for us.

How would you test this exactly?

idi527

idi527

You can look into how Locus, for example, approached the problem and apply a similar solution … For compilation into a module at runtime you can look into foil or fastglobal, but I’d still go with locus’s approach instead.

NobbZ

NobbZ

How?

A single module, with a single function, this doesn’t seem to be much of atoms…

If though you want to have all IP addresses as atoms, well then there is not much we could do for you, if though you use tuple notation for them, it should be fine.

I’m sorry for you. But perhaps you might take a look at them and check how they do it? Alternatively there is the :persistent_term module in a recent enough OTP.

From all of what I’ve heard, it is meant to be used as a write once, read fast store of data.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

We are using multi-clause functions. Somehow, we ran out of atoms because of that. We are generating a module that has 10_000_000 functions. Admittedly this is something I plan on asking in another discussion, since this topic is supposed to be about ETS tables or similar alternatives.

Rather interesting but unfortunately the API doesn’t have support for range queries. Thanks for the effort though!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

As an aside, this seems like a situation where an interval tree could be useful.

Have a set of IPs or ranges you want to look up, benchmark how long it takes you to look them up on the ets table at various sizes. Plot. If it’s a line you’ve got O(n), if it’s an increasingly flat curve you’ve got O(log(n))

— All posts loaded —

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
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews