Does passing around struct really consume a lot of memory?

I read this article on Medium today from the Awesome Elixir newsletter. It talks about how populating Ecto structs loaded with data and associations consumes far more memory than only selecting the fields you need when querying the database.

Personally, I prefer loading data into structs and passing them around because it gives me a clear data structure I can access. Selecting custom fields in queries each time is less structured and prone to inconsistency. But I’m concerned about this memory hoarding.

Has anyone experienced anything similar?

Honestly, I doubt I understand how reducing an amount of data in operation might lead to inconsistency.

1 Like

If you need to pass all the data everyone, it might be a symptom of bad architecture. Also preloading always all associations means you make more queries than you probably need, thus needlessly increasing overall latency.

I’m pretty sure the difference is not in having structs or not, but rather in loading all fields of the struct or just a couple fields that you need. I suspect similar effect could be achieved with loading the structs only with data that was needed with the struct/2 helper

2 Likes

Thanks for the responses. I may have misunderstood the article. I selectively preload, but always pass around the struct, not just a map with the selected fields. I feel that gives my application more structure by allowing me to pattern match on a %User{} or %Transfer{} struct. But yes, I don’t preload all associations by default; that’s overkill.

2 Likes

I would say definitely lean towards keeping the data in structs rather than loading just partial data into maps. Structs are really good so the code can understand (along with the programmer reading the code) what data is being dealt with or what transformations are happening in the code. Of course if you do start to run into performance problems than it may make sense to load only part of the data in the row (although in that case I might still lean toward a struct).

1 Like