What are the performance advantage of Elixir's defstruct?
up vote
0
down vote
favorite
My understanding of Elixir's structure is that;
1. It can't hold keys other than ones defined at compile time.
2. It can have default values, which is evaluated at compile time.
Is there are any performance advantage?
Also, is there anything that I'm missing?
elixir structure
add a comment |
up vote
0
down vote
favorite
My understanding of Elixir's structure is that;
1. It can't hold keys other than ones defined at compile time.
2. It can have default values, which is evaluated at compile time.
Is there are any performance advantage?
Also, is there anything that I'm missing?
elixir structure
1
From the docs: "Structs can also be used in pattern matching, both for matching on the value of specific keys as well as for ensuring that the matching value is a struct of the same type as the matched value." So you can win by pattern matching rather than using less performant algorithmic method - I guess.
– GavinBrelstaff
Nov 14 at 7:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My understanding of Elixir's structure is that;
1. It can't hold keys other than ones defined at compile time.
2. It can have default values, which is evaluated at compile time.
Is there are any performance advantage?
Also, is there anything that I'm missing?
elixir structure
My understanding of Elixir's structure is that;
1. It can't hold keys other than ones defined at compile time.
2. It can have default values, which is evaluated at compile time.
Is there are any performance advantage?
Also, is there anything that I'm missing?
elixir structure
elixir structure
asked Nov 14 at 6:02
HelloWorld
7416
7416
1
From the docs: "Structs can also be used in pattern matching, both for matching on the value of specific keys as well as for ensuring that the matching value is a struct of the same type as the matched value." So you can win by pattern matching rather than using less performant algorithmic method - I guess.
– GavinBrelstaff
Nov 14 at 7:52
add a comment |
1
From the docs: "Structs can also be used in pattern matching, both for matching on the value of specific keys as well as for ensuring that the matching value is a struct of the same type as the matched value." So you can win by pattern matching rather than using less performant algorithmic method - I guess.
– GavinBrelstaff
Nov 14 at 7:52
1
1
From the docs: "Structs can also be used in pattern matching, both for matching on the value of specific keys as well as for ensuring that the matching value is a struct of the same type as the matched value." So you can win by pattern matching rather than using less performant algorithmic method - I guess.
– GavinBrelstaff
Nov 14 at 7:52
From the docs: "Structs can also be used in pattern matching, both for matching on the value of specific keys as well as for ensuring that the matching value is a struct of the same type as the matched value." So you can win by pattern matching rather than using less performant algorithmic method - I guess.
– GavinBrelstaff
Nov 14 at 7:52
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
The main advantage of struct is not any performance, but code readability and correctness. Consider something like %{id: 123}
- we don't know what that map represents. With structs we can have %Person{id: 123}
and %Company{id: 123}
, and these will be different things. Furthermore one can easily express which one is expected with pattern matching, as mentioned by GavinBrelstaff. Another example of a feature in struct aimed at correctness is @enforce_keys
- by setting it in your struct you make sure that any normally created instance of it will have the given keys present.
add a comment |
up vote
1
down vote
Well, Elixir is open source. As it might be easily seen from the implementation of Kernel.defstruct/1
, everything it does is it defines __struct__
method on the module where it was called (only the false
part of the case could be considered without the loss of generality).
Everything else there is responsible for informing the Elixir compiler about this struct to enable new syntax (%MyStruct{}
) for it and to store some metainformation about the struct into compiler globals.
Underneath the struct is represented by bare map %{}
and there could not be any room for any performance boost.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The main advantage of struct is not any performance, but code readability and correctness. Consider something like %{id: 123}
- we don't know what that map represents. With structs we can have %Person{id: 123}
and %Company{id: 123}
, and these will be different things. Furthermore one can easily express which one is expected with pattern matching, as mentioned by GavinBrelstaff. Another example of a feature in struct aimed at correctness is @enforce_keys
- by setting it in your struct you make sure that any normally created instance of it will have the given keys present.
add a comment |
up vote
2
down vote
accepted
The main advantage of struct is not any performance, but code readability and correctness. Consider something like %{id: 123}
- we don't know what that map represents. With structs we can have %Person{id: 123}
and %Company{id: 123}
, and these will be different things. Furthermore one can easily express which one is expected with pattern matching, as mentioned by GavinBrelstaff. Another example of a feature in struct aimed at correctness is @enforce_keys
- by setting it in your struct you make sure that any normally created instance of it will have the given keys present.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The main advantage of struct is not any performance, but code readability and correctness. Consider something like %{id: 123}
- we don't know what that map represents. With structs we can have %Person{id: 123}
and %Company{id: 123}
, and these will be different things. Furthermore one can easily express which one is expected with pattern matching, as mentioned by GavinBrelstaff. Another example of a feature in struct aimed at correctness is @enforce_keys
- by setting it in your struct you make sure that any normally created instance of it will have the given keys present.
The main advantage of struct is not any performance, but code readability and correctness. Consider something like %{id: 123}
- we don't know what that map represents. With structs we can have %Person{id: 123}
and %Company{id: 123}
, and these will be different things. Furthermore one can easily express which one is expected with pattern matching, as mentioned by GavinBrelstaff. Another example of a feature in struct aimed at correctness is @enforce_keys
- by setting it in your struct you make sure that any normally created instance of it will have the given keys present.
answered Nov 14 at 14:06
Paweł Obrok
17.2k56060
17.2k56060
add a comment |
add a comment |
up vote
1
down vote
Well, Elixir is open source. As it might be easily seen from the implementation of Kernel.defstruct/1
, everything it does is it defines __struct__
method on the module where it was called (only the false
part of the case could be considered without the loss of generality).
Everything else there is responsible for informing the Elixir compiler about this struct to enable new syntax (%MyStruct{}
) for it and to store some metainformation about the struct into compiler globals.
Underneath the struct is represented by bare map %{}
and there could not be any room for any performance boost.
add a comment |
up vote
1
down vote
Well, Elixir is open source. As it might be easily seen from the implementation of Kernel.defstruct/1
, everything it does is it defines __struct__
method on the module where it was called (only the false
part of the case could be considered without the loss of generality).
Everything else there is responsible for informing the Elixir compiler about this struct to enable new syntax (%MyStruct{}
) for it and to store some metainformation about the struct into compiler globals.
Underneath the struct is represented by bare map %{}
and there could not be any room for any performance boost.
add a comment |
up vote
1
down vote
up vote
1
down vote
Well, Elixir is open source. As it might be easily seen from the implementation of Kernel.defstruct/1
, everything it does is it defines __struct__
method on the module where it was called (only the false
part of the case could be considered without the loss of generality).
Everything else there is responsible for informing the Elixir compiler about this struct to enable new syntax (%MyStruct{}
) for it and to store some metainformation about the struct into compiler globals.
Underneath the struct is represented by bare map %{}
and there could not be any room for any performance boost.
Well, Elixir is open source. As it might be easily seen from the implementation of Kernel.defstruct/1
, everything it does is it defines __struct__
method on the module where it was called (only the false
part of the case could be considered without the loss of generality).
Everything else there is responsible for informing the Elixir compiler about this struct to enable new syntax (%MyStruct{}
) for it and to store some metainformation about the struct into compiler globals.
Underneath the struct is represented by bare map %{}
and there could not be any room for any performance boost.
answered Nov 14 at 13:48
Aleksei Matiushkin
77.3k95088
77.3k95088
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294028%2fwhat-are-the-performance-advantage-of-elixirs-defstruct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
From the docs: "Structs can also be used in pattern matching, both for matching on the value of specific keys as well as for ensuring that the matching value is a struct of the same type as the matched value." So you can win by pattern matching rather than using less performant algorithmic method - I guess.
– GavinBrelstaff
Nov 14 at 7:52