cbor icon indicating copy to clipboard operation
cbor copied to clipboard

feature: Migrate zero copy API and streaming API into master branch

Open fxamacker opened this issue 4 years ago • 4 comments

Add API that uses zero copy to boost performance. Add streaming mode decoder and encoder.

fxamacker avatar Jun 16 '21 23:06 fxamacker

Hi fxamacker, I am trying to get the stream_decoder API (from the stream-mode branch) working but have no clue how to use floats in the stream. I expected to see a floats in the API, but mainly find int's, arrays and maps. Are you planning to add floats while merging the stream-mode branch? Kind regards, Dennis

bijwaard avatar Sep 22 '21 09:09 bijwaard

Hi Faye, While creating my stream-decoder, I noticed that most decode functions use a return for the result. It may be more efficient to use a pointer-argument for the larger types (e.g. strings and byte arrays), so you can also update an existing element without new memory allocation when the element is big enough? It may also save an extra copy for the int64/float64 types.

My stream-decoder now looks like this (decoding floats does not yet compile...), I use a pointer to the rtd_msg struct to avoid creating a new one for every message:

type rtd_msg struct {
  rtd_id uint64
  values []float64
}

func DecodeRTD(buf []byte,r *rtd_msg) error {
  sd:=cbor.NewStreamDecoder(bytes.NewReader(buf))
  var err error
  var l uint64
  _, err = sd.DecodeArrayHead()
  if r.rtd_id,err = sd.DecodeUint64(); err != nil {
    return err
  }

  l, _ = sd.DecodeArrayHead()
  // FIXME: increase array if necessary if l>len(r.values) ...
  for i:=uint64(0);i<l;i++ {
    if r.values[i],err = sd.DecodeFloat(); err!=nil {
      return err
    }
  }
  return nil
}

Kind regards, Dennis

bijwaard avatar Sep 23 '21 10:09 bijwaard

Hi fxamacker, I am trying to get the stream_decoder API (from the stream-mode branch) working but have no clue how to use floats in the stream. I expected to see a floats in the API, but mainly find int's, arrays and maps. Are you planning to add floats while merging the stream-mode branch? Kind regards, Dennis

Hi @bijwaard,

The feature/stream-mode branch was developed for Cadence to improve performance.

Cadence didn't need floats, so the feature to stream floats wasn't added. I don't have a specific timeline for merging this branch into main yet. Before merging to main I'd definitely like to add stream support for floats and maps.

fxamacker avatar Oct 11 '21 02:10 fxamacker

Hi Faye, I'm looking forward to the merged speed-up for floats! For now your library already helps me to decode electrical measurements roughly twice as fast as my earlier cbor decoder in Python. The C++ counterpart is still way faster since it has everything pre-allocated... Kind regards, Dennis

bijwaard avatar Oct 11 '21 08:10 bijwaard