Skip to content

Commit

Permalink
Merge pull request #64 from quorumcontrol/feature/fastpath
Browse files Browse the repository at this point in the history
fastpath CBOR
  • Loading branch information
whyrusleeping authored Sep 17, 2019
2 parents 6fc5303 + 9847526 commit 42a6b22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions encoding/cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ func NewPooledCloner(atl atlas.Atlas) PooledCloner {
}
}

type selfCloner interface {
Clone(b interface{}) error
}

// Clone clones a into b using a cloner from the pool.
func (p *PooledCloner) Clone(a, b interface{}) error {
if self, ok := a.(selfCloner); ok {
return self.Clone(b)
}

c := p.pool.Get().(refmt.Cloner)
err := c.Clone(a, b)
p.pool.Put(c)
Expand Down
12 changes: 11 additions & 1 deletion encoding/marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ func NewMarshallerAtlased(atl atlas.Atlas) *Marshaller {
return m
}

type cborMarshaler interface {
MarshalCBOR(w io.Writer) error
}

// Encode encodes the given object to the given writer.
func (m *Marshaller) Encode(obj interface{}, w io.Writer) error {
m.writer.w = w
err := m.marshal.Marshal(obj)
var err error
selfMarshaling, ok := obj.(cborMarshaler)
if ok {
err = selfMarshaling.MarshalCBOR(w)
} else {
err = m.marshal.Marshal(obj)
}
m.writer.w = nil
return err
}
Expand Down
13 changes: 11 additions & 2 deletions encoding/unmarshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ func NewUnmarshallerAtlased(atl atlas.Atlas) *Unmarshaller {
return m
}

type cborUnmarshaler interface {
UnmarshalCBOR(r io.Reader) error
}

// Decode reads a CBOR object from the given reader and decodes it into the
// given object.
func (m *Unmarshaller) Decode(r io.Reader, obj interface{}) error {
func (m *Unmarshaller) Decode(r io.Reader, obj interface{}) (err error) {
m.reader.r = r
err := m.unmarshal.Unmarshal(obj)
selfUnmarshaler, ok := obj.(cborUnmarshaler)
if ok {
err = selfUnmarshaler.UnmarshalCBOR(r)
} else {
err = m.unmarshal.Unmarshal(obj)
}
m.reader.r = nil
return err
}
Expand Down

0 comments on commit 42a6b22

Please sign in to comment.