sezaru

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?

Showing Posts 1 to 10

zachdaniel

zachdaniel

Creator of Ash

Interesting. You could likely switch to use a change to validate the values, and then do it conditionally in bulk action calls instead of on each attribute?

Schultzer

Schultzer

Regexes could be translated to binary pattern matches at compile time, that would be significantly faster.

zachdaniel

zachdaniel

Creator of Ash

That’s true :smiley: 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

kip

ex_cldr Core Team

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. match could be reserved for regex, and pattern could be added for binary pattern matching.

Here I’m using the following symbols but they can anything:

  • X - ASCII alphanumeric
  • A - ASCII alphbetic
  • D - ASCII digits
  • Anything other character - a literal

I can envisage additional symbols for

  • Unicode alphanumeric
  • Unicode digit
  • ASCII alphabetic
  • Unicode alphabetic
  • Sign matching (+ / -)
  • Upper/lower case matching
  • Quote mark matching (open then close)
  • Unicode general category matching (generalised form of the above)
  • Unicode script matching

A pattern could look like the following (using the examples in the thread) :`

# ~r/^[A-Za-z0-9]{8}$/
X(8)

# ~r/^\d{6}$/
D(6)

# ~r/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
DD.DDD.DDD/DDDD-DD

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

zachdaniel

Creator of Ash

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 :smiling_face_with_sunglasses:

sezaru

sezaru OP

So @zachdaniel I just tried your suggestion, basically I changed my attributes to this:

     attribute :cnpj_basico, :string do
       public? true
       allow_nil? false

       constraints min_length: 8, max_length: 8
     end

     attribute :cnpj_sufixo, :string do
       public? true
       allow_nil? false

       constraints min_length: 6, max_length: 6
     end

     attribute :cnpj_formatado, :string do
       public? true
       allow_nil? false

       constraints min_length: 18, max_length: 18
     end

And added these changes to my create action:

       change fn changeset, _ ->
        regex = ~r/^[A-Za-z0-9]{8}$/

        cnpj_basico = Ash.Changeset.get_attribute(changeset, :cnpj_basico)

        if not is_nil(cnpj_basico) and Regex.match?(regex, cnpj_basico) do
          changeset
        else
          exception = [value: cnpj_basico, field: :cnpj_basico, message: "must match the pattern %{regex}", vars: [regex: inspect(regex)]] |> Ash.Error.Changes.InvalidAttribute.exception()

          Ash.Changeset.add_error(changeset, exception)
        end
      end

      change fn changeset, _ ->
        regex = ~r/^\d{6}$/

        cnpj_sufixo = Ash.Changeset.get_attribute(changeset, :cnpj_sufixo)

        if not is_nil(cnpj_sufixo) and Regex.match?(regex, cnpj_sufixo) do
          changeset
        else
          exception = [value: cnpj_sufixo, field: :cnpj_sufixo, message: "must match the pattern %{regex}", vars: [regex: inspect(regex)]] |> Ash.Error.Changes.InvalidAttribute.exception()

          Ash.Changeset.add_error(changeset, exception)
        end
      end

      change fn changeset, _ ->
        regex = ~r/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/

        cnpj_formatado = Ash.Changeset.get_attribute(changeset, :cnpj_formatado)

        if not is_nil(cnpj_formatado) and Regex.match?(regex, cnpj_formatado) do
          changeset
        else
          exception = [value: cnpj_formatado, field: :cnpj_formatado, message: "must match the pattern %{regex}", vars: [regex: inspect(regex)]] |> Ash.Error.Changes.InvalidAttribute.exception()

          Ash.Changeset.add_error(changeset, exception)
        end
      end

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:

process establishments: 0.235459
process establishments: 0.235015
process establishments: 0.256663
process establishments: 0.229017
process establishments: 0.241808
process establishments: 0.264744
process establishments: 0.264392
process establishments: 0.241751
process establishments: 0.236187
process establishments: 0.24704

Around 0.24s per batch

Now with the match constraint:

process establishments: 1.889976
process establishments: 1.869798
process establishments: 1.88271
process establishments: 1.924556
process establishments: 1.94613
process establishments: 2.154315
process establishments: 2.37499
process establishments: 2.378589
process establishments: 2.349797
process establishments: 2.36511

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 match constraint is actually doing more work than necessary?

zachdaniel

zachdaniel

Creator of Ash

:thinking: that is very surprising. I’m wondering if we may be applying the regex more than once for case where its an attribute constraint :thinking:

sezaru

sezaru OP

Yep, that seems to be the case, I run both codes now with cprof and I can see some interesting changes:

With the changes, I get this number of calls for these functions:

  :re.run/3                                                            3
  :re.import/1                                                         3
  Regex.safe_run/3                                                     3
  Regex.match?/2                                                       3
  anonymous fn/4 in Regex.safe_run/3                                   3

Which makes sense, I have 3 changes and each are doing one Regex.match? call.

Now, removing the changes and having the match constraint, I get these numbers:

  :re.import/1                                                       495
  :re.run/3                                                            3
  Regex.safe_run/3                                                     3
  Regex.match?/2                                                       3
  anonymous fn/4 in Regex.safe_run/3                                   3

As you can see, all the calls are the same except for :re.import/1 which is called 495.

Going back to eprof this is the heaviest call:

:re.import/1                                                        495 33.19  525    1.06 

In other words, it seems that, for some reason, having the match constraint makes Ash call :re.import/1 hundreds of times per time during an Ash.Changeset.for_create call.

user20251023

user20251023

Can you do constraints: [match: {Spark.Regex, :cache, ["^\\d{8}$", ""]}] instead of constraints: [match: ~r/^\d{8}$/]?

You will need to change match: [ type: :regex ] to match: [ type: :regex_as_mfa ] in ‎lib/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

sezaru OP

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.

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