japplegame

japplegame

It appears that structures created with the %Struct{...} syntax do not share the key tuple.

Compare:

iex> Enum.map(1..1000, &%URI{port: &1}) |> :erts_debug.size()
24000
iex(68)> Enum.map(1..1000, &struct!(URI, port: &1)) |> :erts_debug.size()
14010

The difference in memory usage seems significant. Is it a flaw or by design?

Showing Posts 1 to 4

Sorc96

Sorc96

That’s interesting. I thought %Struct{...} would just call Struct.__struct__(...), but it must be doing something else as well. Since it is special syntax, I’m not sure where to look for the implementation in the Elixir source code.

al2o3cr

al2o3cr

The %URL{} version expands the struct definition at compile-time. You can demonstrate this with the @compile :S flag:

defmodule Foo do
  defstruct [:bar, :baz, huh: 69]
end

defmodule Bar do
  @compile :S

  def literal_version(x) do
    %Foo{bar: x, baz: 42}
  end

  def struct_version(x) do
    struct(Foo, bar: x, baz: 42)
  end
end

Running elixir on this file fails, but also produces a new file with the extension .S that has BEAM assembly in it.

literal_version compiles to an explicit map operation:

{function, literal_version, 1, 11}.
  {label,10}.
    {line,[{location,"struct_demo.ex",8}]}.
    {func_info,{atom,'Elixir.Bar'},{atom,literal_version},1}.
  {label,11}.
    {put_map_assoc,{f,0},
                   {literal,#{}},
                   {x,0},
                   1,
                   {list,[{atom,'__struct__'},
                          {atom,'Elixir.Foo'},
                          {atom,bar},
                          {x,0},
                          {atom,baz},
                          {integer,42},
                          {atom,huh},
                          {integer,69}]}}.
    return.

Versus the struct version:

{function, struct_version, 1, 13}.
  {label,12}.
    {line,[{location,"struct_demo.ex",12}]}.
    {func_info,{atom,'Elixir.Bar'},{atom,struct_version},1}.
  {label,13}.
    {test_heap,5,1}.
    {put_tuple2,{x,0},{list,[{atom,bar},{x,0}]}}.
    {put_list,{x,0},{literal,[{baz,42}]},{x,1}}.
    {move,{atom,'Elixir.Foo'},{x,0}}.
    {line,[{location,"struct_demo.ex",13}]}.
    {call_ext_only,2,{extfunc,'Elixir.Kernel',struct,2}}.
Sorc96

Sorc96

That makes sense for performance. I also just tried putting the original code into a module and now both examples have the exact same result. So this must be related to IEx and doesn’t make a difference in regular modules.

LostKobrakai

LostKobrakai

Yeah, you always want to test performance related stuff on modules not ad hoc evaluated code.

— All posts loaded —

Where Next? Top

Trending in Questions Top

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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews