japplegame

japplegame OP

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?

First 4 of 4 Posts Switch mode

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

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
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
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
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
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement