Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/elixir/pages/anti-patterns/code-anti-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@

The Erlang VM has two internal representations for maps: a flat map and a hash map. A flat map is represented internally as two tuples: one tuple containing the keys and another tuple holding the values. Whenever you update a flat map, the tuple keys are shared, reducing the amount of memory used by the update. A hash map has a more complex structure, which is efficient for a large amount of keys, but it does not share the key space.

Maps of up to 32 keys are represented as flat maps. All others are hash map. Since structs maintain a metadata key called `__struct__`, any struct with fewer than 32 fields are represented as a flat map, which allows us to optimize several struct operations, as we never add or remove fields to structs, we simply update them.
Maps of up to 32 keys are represented as flat maps. All others are hash map. Structs _are_ maps (with a metadata field called `__struct__`) and so any struct with fewer than 32 fields is represented as a flat map. This allows us to optimize several struct operations, as we never add or remove fields to structs, we simply update them.

Check failure on line 562 in lib/elixir/pages/anti-patterns/code-anti-patterns.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Emphasis style

lib/elixir/pages/anti-patterns/code-anti-patterns.md:562:86 MD049/emphasis-style Emphasis style [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md049.md

Check failure on line 562 in lib/elixir/pages/anti-patterns/code-anti-patterns.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Emphasis style

lib/elixir/pages/anti-patterns/code-anti-patterns.md:562:90 MD049/emphasis-style Emphasis style [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md049.md

Furthermore, structs of the same name "instantiated" in the same module will share the same "tuple keys" at compilation times, as long as they have fewer than 32 fields. For example, the following code:

Expand All @@ -577,10 +577,10 @@

Removing this anti-pattern, in a nutshell, requires ensuring your struct has fewer than 32 fields. There are a few techniques you could apply:

* If the struct has "optional" fields, for example, fields which are initialized with nil, you could nest all optional fields into other field, called `:metadata`, `:optionals`, or similar. This could lead to benefits such as being able to use pattern matching to check if a field exists or not, instead of relying on `nil` values

Check failure on line 580 in lib/elixir/pages/anti-patterns/code-anti-patterns.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Unordered list indentation

lib/elixir/pages/anti-patterns/code-anti-patterns.md:580:1 MD007/ul-indent Unordered list indentation [Expected: 2; Actual: 0] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md007.md

* You could nest structs, by storing structs within other fields. Fields that are rarely read or written to are good candidates to be moved to a nested struct

Check failure on line 582 in lib/elixir/pages/anti-patterns/code-anti-patterns.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Unordered list indentation

lib/elixir/pages/anti-patterns/code-anti-patterns.md:582:1 MD007/ul-indent Unordered list indentation [Expected: 2; Actual: 0] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md007.md

* You could nest fields as tuples. For example, if two fields are always read or updated together, they could be moved to a tuple (or another composite data structure)

Check failure on line 584 in lib/elixir/pages/anti-patterns/code-anti-patterns.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Unordered list indentation

lib/elixir/pages/anti-patterns/code-anti-patterns.md:584:1 MD007/ul-indent Unordered list indentation [Expected: 2; Actual: 0] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md007.md

The challenge is to balance the changes above with API ergonomics, in particular, when fields may be frequentlyb read and written to.
Loading