sezaru
I have a resource that has the following fields with regexes:
attribute :cnpj_basico, :string do
public? true
allow_nil? false
constraints min_length: 8, max_length: 8, match: ~r/^[A-Za-z0-9]{8}$/
end
attribute :cnpj_sufixo, :string do
public? true
allow_nil? false
constraints min_length: 6, max_length: 6, match: ~r/^\d{6}$/
end
attribute :cnpj_formatado, :string do
public? true
allow_nil? false
constraints min_length: 18, max_length: 18, match: ~r/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
end
I need to bulk insert millions of rows into the database using this resource.
I noticed that casting the resource would have a major slowdown if the match constraints are in place.
Here are the most expensive calls with match enabled using eprof:
# CALLS % TIME µS/CALL
Total 3676 100.0 2155 0.59
...
:crypto.strong_rand_bytes_nif/1 1 0.79 17 17.00
Ecto.Type.cast_fun/1 13 0.84 18 1.38
Ash.Changeset.force_change_attribute/3 10 1.07 23 2.30
Enum."-map/2-lists^map/1-1-"/2 80 1.16 25 0.31
:lists.member/2 47 1.16 25 0.53
:lists.keyfind/3 40 1.16 25 0.63
:ets.match_object/2 4 1.30 28 7.00
:re.import/1 495 24.41 526 1.06
Core.Cnpj.Estabelecimento.persisted/0 41 38.61 832 20.29
And here are the most expensive ones if I remove the match constraint
# CALLS % TIME µS/CALL
Total 3160 100. 592 0.19
Spark.Dsl.Extension.persisted!/3 43 1.69 10 0.23
Ash.Changeset.do_change_attribute/4 7 1.86 11 1.57
:erlang.module_loaded/1 30 1.86 11 0.37
anonymous fn/1 in Ash.Changeset.expand_upsert_fields/2 37 2.20 13 0.35
Enum."-map/2-lists^map/1-1-"/2 80 2.36 14 0.18
:crypto.strong_rand_bytes_nif/1 1 2.53 15 15.00
Ash.Changeset.force_change_attribute/3 10 2.70 16 1.60
:erlang.binary_to_atom/2 8 2.70 16 2.00
:ets.match_object/2 4 4.22 25 6.25
As you can see, the call went from 2155 µS to 592 µS.
This is even more expressive when processing the data in bulk, my times when from (10_000 chunks) ~6 seconds to 0.3~0.5 seconds.
So, is there some way to optimize this checks in Ash.Changeset calls during casting?
If not, is there some way for me to disable the regex check during bulk insertion so I can make it faster?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
zachdaniel
Interesting. You could likely switch to use a
changeto validate the values, and then do it conditionally in bulk action calls instead of on each attribute?Schultzer
Regexes could be translated to binary pattern matches at compile time, that would be significantly faster.
zachdaniel
That’s true
I’ve wanted a generic regex transpiration library for a while, thought of writing it myself. Something to take an elixir regex and transpire it to match spec, js, and PostgreSQL especially. (naturally allowing that not all regexes can be translated that way).
kip
In unicode_string i have a function Unicode.Set.to_pattern/1 to transpile Unicode sets into binary patterns (or to
:re-compatible regexs). Adapting that to transpile a subset of regex into pattern match wouldn’t be too much work so I’ll look at that.Another thought that strikes me is that maybe, for Ash, something like the old COBOL PIC clause might be a good abstraction - it is declarative and it would map directly to a binary pattern match. The examples in this thread would be supported by such a construct.
matchcould be reserved for regex, andpatterncould be added for binary pattern matching.Here I’m using the following symbols but they can anything:
X- ASCII alphanumericA- ASCII alphbeticD- ASCII digitsI can envisage additional symbols for
A pattern could look like the following (using the examples in the thread) :`
I think this is quite an elegant and declarative way to express many (but clearly not all!) data format expectations. And since they can be transpiled to binary pattern matches they would be a good fit for the BEAM. They can also abstract away some of the complexities of “what is a letter”, “what is a digit” and “what is whitespace” given Unicode has quite extensive character repertories for these categories and others.
zachdaniel
Hmm…I much prefer something that doesn’t require the user learning a new pattern language. Its a neat idea though, and pulling from an existing standard is the only thing that really makes it tenable.
sick
sezaru
So @zachdaniel I just tried your suggestion, basically I changed my attributes to this:
And added these changes to my create action:
So, theorically, AFAIK, these two should be equal and should take the same time.
But, when I compared them both in my bulk loading, the times where very different, for example, here is the time with my changes:
Around 0.24s per batch
Now with the
matchconstraint:Around 2.1s per batch
Is this expected? I would expect that they would take the same amount of time. This makes me wonder if the
matchconstraint is actually doing more work than necessary?zachdaniel
sezaru
Yep, that seems to be the case, I run both codes now with
cprofand I can see some interesting changes:With the changes, I get this number of calls for these functions:
Which makes sense, I have 3 changes and each are doing one
Regex.match?call.Now, removing the changes and having the
matchconstraint, I get these numbers:As you can see, all the calls are the same except for
:re.import/1which is called 495.Going back to
eprofthis is the heaviest call:In other words, it seems that, for some reason, having the
matchconstraint makes Ash call:re.import/1hundreds of times per time during anAsh.Changeset.for_createcall.user20251023
Can you do
constraints: [match: {Spark.Regex, :cache, ["^\\d{8}$", ""]}]instead ofconstraints: [match: ~r/^\d{8}$/]?You will need to change
match: [ type: :regex ]tomatch: [ type: :regex_as_mfa ]inlib/ash/type/string.ex.It was reverted in this commit.
https://github.com/ash-project/ash/commit/d77537dd03f7e31dc7a127a415a0a935be15043c#diff-1d18069e5b6963e5934ed6ccc230df9a8095f4a3785d93cc0dc328e1a419b83f
https://github.com/ash-project/spark/blob/b985057765789cbae5c45321fbae8f10c263d6dd/lib/spark/options/options.ex#L194
sezaru
Ah, that explains it, I did saw some code in the regex match part that allowed calling a function with the mfa, but when I tried myself it would break somewhere before because the support was removed.