zachdaniel

zachdaniel

Creator of Ash

Recent Spark performance improvements should have significant positive effects on Ash application performance

Hey folks, made some recent performance improvements to spark, the tool underlying all of our DSLs. GitHub - ash-project/spark: Tooling for building DSLs in Elixir · GitHub

It should make code that accesses DSL options significantly faster. If people can try it out with mix deps.update spark that would be great :slight_smile: Bonus points to anyone who has some level of performance measuring, if you could show how its impacted things for you, that would be great :heart:

Some technical context around what was actually done:

Modules that use the DSL built by spark create a data structure under the hood, that looks like this:

%{
  persisted: %{...explicitly cached things that are used often},
  [:attributes] => %{entities: [...], options: [...]},
  [:json_api, :routes] => %{entities: [...], options: [...]},
  ... and so on
}

This data structure groups everything by section. However, when accessing DSL information, you always go through a set of functions provided by spark, for example Spark.Dsl.Extension.get_opt and Spark.Dsl.entities. We did this on purpose to allow us to optimize the underlying storage mechanisms and access patterns. While doing some recent benchmarking, we saw that the current naive implementations were taking quite a bit of time during the execution of Ash actions. The implementation would be something like dsl |> Map.get(path) |> Map.get(:options) |> Keyword.get(option_name). We would do similar things with entities.

What we’ve instead done is defined functions under the hood for each section path and option, that pattern matches and returns the value. It looks like this:

        for {path, %{opts: opts}} <- @spark_dsl_config, is_list(path) do
          for {key, value} <- opts do
            def fetch_opt(unquote(path), unquote(key)) do
              {:ok, unquote(Macro.escape(value))}
            end
          end
        end

        def fetch_opt(_, _), do: :error

Then we updated Extension.get_opt to call this generated function instead of getting the big DSL map and pulling out values. Measurements so far show significant performance improvements (especially in bulk creates, which s what sparked this recent investigation into unnecessary slowness).

Aside from this, we also leveraged the persist key to optimize a bunch of commonly accessed Ash DSL items.

Please give it a try! Bonus points to anyone who has some level of performance measuring, if you could show how its impacted things for you, that would be great :heart:

Most Liked

frankdugan3

frankdugan3

I have an ERP app (82,528 lines of Elixir) with over 80 resources that heavily leverage Ash extensions, and it seems like these improvements also vastly improve compile time, cutting it down to less than half for me!

With spark 1.1.46:

hsm on  graphql-api-tokens [!?] via  v1.14.5 (OTP 25) mix compile --force
Compiling 499 files (.ex)
Generated hsm app
Compiling golang module :gopcua_bridge (native/gopcua_bridge)...
hsm on  graphql-api-tokens [!?] via  v1.14.5 (OTP 25) took 1m26s

With spark 1.1.48:

hsm on  graphql-api-tokens [!?] via  v1.14.5 (OTP 25)

ᕕ(ᐛ)ᕗ mix compile --force
Compiling 499 files (.ex)
Generated hsm app
Compiling golang module :gopcua_bridge (native/gopcua_bridge)...
hsm on  graphql-api-tokens [!?] via  v1.14.5 (OTP 25) took 41s
15
Post #2

Where Next?

Popular in News & Updates Top

lawik
NervesHub has had a CLI for a long time. It is called nerves_hub_cli and for the longest time you’d add it as a dependency. But only once...
New
zachdaniel
Hey folks! I’ve released the first version of our new AI policy. Accepting it is now part of our issue and pull request templates. Feel ...
New
ConnorRigby
This week we added official Nerves support for the OSD32MP1 line of SOMs. Currently we have tested the osd32mp1-brk breakout board, and ...
New
fhunleth
We recently released Nerves 1.4.0 and an update to the Nerves new project generator, nerves_bootstrap. The biggest change is support for ...
New
jjcarstens
Do you like Hacktoberfest? Also enjoy working with Nerves and want to contribute? Fantastic! :tada: :beers: Here are some potential sta...
New
bartblast
Hologram v0.6.0 is here, bringing production-ready features to the full-stack Elixir web framework! This release focuses on enhanced secu...
New
zachdaniel
Hey folks, we have a minor CVE issued for AshAuthentication. Please read the CVE and update accordingly. Relatively low severity, can’t c...
New
zachdaniel
Going into main shortly, to be released with atomics being wrapped up (which will get its own larger announcement), we will support valid...
New
zachdaniel
Join us for our first Ash Office Hours next Thursday at 6PM EST! Myself and anyone from the core team who can make it will be answering q...
New
zachdaniel
@jimsynz just launched an epic new extension for Ash! It allows you to create rich mix tasks for calling your resource actions directly f...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement