Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastpath CBOR #64

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 this is what i had in mind

} 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

if ok {
err = selfUnmarshaler.UnmarshalCBOR(r)
} else {
err = m.unmarshal.Unmarshal(obj)
}
m.reader.r = nil
return err
}
Expand Down