Uploaded image for project: 'Erlang/OTP'
  1. Erlang/OTP
  2. ERL-1460

Mention state copy in efficiency guide

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:

      Description

      I don't know where to add this knowledge, but bumped into a performance issue caused by misunderstanding the compiler. I think the efficiency-guide/advanced.html under memory is a good place to write something about this. But there probably is a better place.

      The issue is:

      when the programmer writes:

      handle_call(f, _From, State) ->
          Fun = fun() -> State#state.small == 12 end,
          {reply, Fun, State}.

      then the complete state is the "environment" of the function. This complete state is copied and therefore this operation may take a lot of time when other field of the state is huge (see example attached).

      So, one should advice users to bind only the part of state needed (or optimise the compiler):

      handle_call(f, _From, State) ->
          X = State#state.small,
          Fun = fun() -> X == 12 end,
          {reply, Fun, State}.
      

        Attachments

          Activity

            People

            Assignee:
            bjorn Björn Gustavsson
            Reporter:
            QuickCheck Thomas Arts
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: