1. 25 Aug, 2021 1 commit
    • Felix Lange's avatar
      rlp: minor optimizations for slice/array encoding (#23467) · 32c576bd
      Felix Lange authored
      As per benchmark results below, these changes speed up encoding/decoding of
      consensus objects a bit.
      
          name                             old time/op    new time/op    delta
          EncodeRLP/legacy-header-8           384ns ± 1%     331ns ± 3%  -13.83%  (p=0.000 n=7+8)
          EncodeRLP/london-header-8           411ns ± 1%     359ns ± 2%  -12.53%  (p=0.000 n=8+8)
          EncodeRLP/receipt-for-storage-8     251ns ± 0%     239ns ± 0%   -4.97%  (p=0.000 n=8+8)
          EncodeRLP/receipt-full-8            319ns ± 0%     300ns ± 0%   -5.89%  (p=0.000 n=8+7)
          EncodeRLP/legacy-transaction-8      389ns ± 1%     387ns ± 1%     ~     (p=0.099 n=8+8)
          EncodeRLP/access-transaction-8      607ns ± 0%     581ns ± 0%   -4.26%  (p=0.000 n=8+8)
          EncodeRLP/1559-transaction-8        627ns ± 0%     606ns ± 1%   -3.44%  (p=0.000 n=8+8)
          DecodeRLP/legacy-header-8           831ns ± 1%     813ns ± 1%   -2.20%  (p=0.000 n=8+8)
          DecodeRLP/london-header-8           824ns ± 0%     804ns ± 1%   -2.44%  (p=0.000 n=8+7)
      
      * rlp: pass length to byteArrayBytes
      
      This makes it possible to inline byteArrayBytes. For arrays, the length is known
      at encoder construction time, so the call to v.Len() can be avoided.
      
      * rlp: avoid IsNil for pointer encoding
      
      It's actually cheaper to use Elem first, because it performs less checks
      on the value. If the pointer was nil, the result of Elem is 'invalid'.
      
      * rlp: minor optimizations for slice/array encoding
      
      For empty slices/arrays, we can avoid storing a list header entry in the
      encoder buffer. Also avoid doing the tail check at encoding time because
      it is already known at encoder construction time.
      32c576bd
  2. 22 May, 2021 2 commits
    • Felix Lange's avatar
      rlp: optimize byte array handling (#22924) · 154ca32a
      Felix Lange authored
      This change improves the performance of encoding/decoding [N]byte.
      
          name                     old time/op    new time/op    delta
          DecodeByteArrayStruct-8     336ns ± 0%     246ns ± 0%  -26.98%  (p=0.000 n=9+10)
          EncodeByteArrayStruct-8     225ns ± 1%     148ns ± 1%  -34.12%  (p=0.000 n=10+10)
      
          name                     old alloc/op   new alloc/op   delta
          DecodeByteArrayStruct-8      120B ± 0%       48B ± 0%  -60.00%  (p=0.000 n=10+10)
          EncodeByteArrayStruct-8     0.00B          0.00B          ~     (all equal)
      154ca32a
    • Felix Lange's avatar
      rlp: use atomic.Value for type cache (#22902) · 0d076d92
      Felix Lange authored
      All encoding/decoding operations read the type cache to find the
      writer/decoder function responsible for a type. When analyzing CPU
      profiles of geth during sync, I found that the use of sync.RWMutex in
      cache lookups appears in the profiles. It seems we are running into
      CPU cache contention problems when package rlp is heavily used
      on all CPU cores during sync.
      
      This change makes it use atomic.Value + a writer lock instead of
      sync.RWMutex. In the common case where the typeinfo entry is present in
      the cache, we simply fetch the map and lookup the type.
      0d076d92
  3. 07 May, 2021 1 commit
  4. 06 Jul, 2020 1 commit
  5. 01 Jul, 2020 1 commit
  6. 13 Sep, 2019 1 commit
    • Felix Lange's avatar
      rlp: improve nil pointer handling (#20064) · 96fb8391
      Felix Lange authored
      * rlp: improve nil pointer handling
      
      In both encoder and decoder, the rules for encoding nil pointers were a
      bit hard to understand, and didn't leave much choice. Since RLP allows
      two empty values (empty list, empty string), any protocol built on RLP
      must choose either of these values to represent the null value in a
      certain context.
      
      This change adds choice in the form of two new struct tags, "nilString"
      and "nilList". These can be used to specify how a nil pointer value is
      encoded. The "nil" tag still exists, but its implementation is now
      explicit and defines exactly how nil pointers are handled in a single
      place.
      
      Another important change in this commit is how nil pointers and the
      Encoder interface interact. The EncodeRLP method was previously called
      even on nil values, which was supposed to give users a choice of how
      their value would be handled when nil. It turns out this is a stupid
      idea. If you create a network protocol containing an object defined in
      another package, it's better to be able to say that the object should be
      a list or string when nil in the definition of the protocol message
      rather than defining the encoding of nil on the object itself.
      
      As of this commit, the encoding rules for pointers now take precedence
      over the Encoder interface rule. I think the "nil" tag will work fine
      for most cases. For special kinds of objects which are a struct in Go
      but strings in RLP, code using the object can specify the desired
      encoding of nil using the "nilString" and "nilList" tags.
      
      * rlp: propagate struct field type errors
      
      If a struct contained fields of undecodable type, the encoder and
      decoder would panic instead of returning an error. Fix this by
      propagating type errors in makeStruct{Writer,Decoder} and add a test.
      96fb8391
  7. 14 May, 2019 1 commit
    • Felix Lange's avatar
      rlp: fixes for two corner cases and documentation (#19527) · 8deec2e4
      Felix Lange authored
      These changes fix two corner cases related to internal handling of types
      in package rlp: The "tail" struct tag can only be applied to the last field.
      The check for this was wrong and didn't allow for private fields after the
      field with the tag. Unsupported types (e.g. structs containing int) which
      implement either the Encoder or Decoder interface but not both 
      couldn't be encoded/decoded.
      
      Also fixes #19367
      8deec2e4
  8. 08 May, 2018 1 commit
  9. 12 Jun, 2017 1 commit
  10. 15 Apr, 2016 1 commit
  11. 19 Feb, 2016 1 commit
  12. 10 Sep, 2015 3 commits
  13. 13 Aug, 2015 1 commit
  14. 23 Jul, 2015 1 commit
  15. 22 Jul, 2015 1 commit
  16. 07 Jul, 2015 1 commit
  17. 29 Jun, 2015 3 commits
  18. 17 Apr, 2015 1 commit
    • Felix Lange's avatar
      rlp: stricter rules for structs and pointers · cad64fb9
      Felix Lange authored
      The rules have changed as follows:
      
      * When decoding into pointers, empty values no longer produce
        a nil pointer. This can be overriden for struct fields using the
        struct tag "nil".
      * When decoding into structs, the input list must contain an element
        for each field.
      cad64fb9
  19. 25 Mar, 2015 1 commit
  20. 20 Mar, 2015 1 commit
  21. 19 Mar, 2015 1 commit
  22. 17 Mar, 2015 1 commit
  23. 11 Feb, 2015 1 commit
  24. 05 Feb, 2015 1 commit
  25. 15 Jan, 2015 2 commits
    • Felix Lange's avatar
      rlp: allow encoding non-empty interface values · fc92abec
      Felix Lange authored
      This needs to be supported because []someInterface does occur sometimes.
      
      Funny enough, the fix involves changes to the decoder. makeDecoder
      cannot return an error for non-empty interfaces anymore because the type
      cache builds both decoder and writer. Do the check at 'runtime' instead.
      fc92abec
    • Felix Lange's avatar
      rlp: add functions for encoding · 552f5b26
      Felix Lange authored
      I'm reasonably confident that the encoding matches the output of
      ethutil.Encode for values that it supports. Some of the tests have been
      adpated from the Ethereum testing repository.
      
      There are still TODOs in the code.
      552f5b26