What are the performance advantage of Elixir's defstruct?












0















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?










share|improve this question


















  • 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 '18 at 7:52


















0















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?










share|improve this question


















  • 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 '18 at 7:52
















0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 6:02









HelloWorldHelloWorld

97110




97110








  • 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 '18 at 7:52
















  • 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 '18 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 '18 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 '18 at 7:52














2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer































    1














    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.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      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

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      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.






      share|improve this answer




























        2














        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.






        share|improve this answer


























          2












          2








          2







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 14:06









          Paweł ObrokPaweł Obrok

          17.6k56060




          17.6k56060

























              1














              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.






              share|improve this answer




























                1














                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.






                share|improve this answer


























                  1












                  1








                  1







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 13:48









                  Aleksei MatiushkinAleksei Matiushkin

                  81.5k95591




                  81.5k95591






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma