Skip to content

Commit

Permalink
Merge pull request #45 from vimeo/protocodec_inline_optimizations
Browse files Browse the repository at this point in the history
protocodec: make a few tweaks to allow inlining
  • Loading branch information
dfinkel committed Apr 9, 2024
2 parents c7ef985 + f34785d commit efe6cae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
25 changes: 14 additions & 11 deletions protocodec/backend_getter_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ func (b backendGetterV2[C, T]) Get(ctx context.Context, key string, dest galaxyc
if bgErr != nil {
return bgErr
}
switch d := dest.(type) {
case *CodecV2[C, T]:
if d, ok := dest.(*CodecV2[C, T]); ok {
d.Set(out)
default:
vs, mErr := proto.Marshal(out)
if mErr != nil {
return fmt.Errorf("failed to marshal value as bytes: %w", mErr)
}

if uErr := dest.UnmarshalBinary(vs); uErr != nil {
return fmt.Errorf("destination codec (type %T) Unmarshal failed: %w", dest, uErr)
}
return nil
}
return b.setSlow(out, dest)

}

func (b backendGetterV2[C, T]) setSlow(out T, dest galaxycache.Codec) error {
vs, mErr := proto.Marshal(out)
if mErr != nil {
return fmt.Errorf("failed to marshal value as bytes: %w", mErr)
}

if uErr := dest.UnmarshalBinary(vs); uErr != nil {
return fmt.Errorf("destination codec (type %T) Unmarshal failed: %w", dest, uErr)
}
return nil
}
10 changes: 5 additions & 5 deletions protocodec/galaxywrap_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

// GalaxyGet is a simple wrapper around a Galaxy.Get method-call that takes
// care of constructing the protocodec.CodecV2, etc. (making the interface more idiomatic for Go)
func GalaxyGet[C any, T pointerMessage[C]](ctx context.Context, g *galaxycache.Galaxy, key string) (T, error) {
pc := NewV2[C, T]()
getErr := g.Get(ctx, key, &pc)
func GalaxyGet[C any, T pointerMessage[C]](ctx context.Context, g *galaxycache.Galaxy, key string) (m T, getErr error) {
pc := CodecV2[C, T]{}
getErr = g.Get(ctx, key, &pc)
if getErr != nil {
return nil, getErr
return // use named return values to bring the inlining cost down
}
return pc.Get(), nil
return pc.msg, nil
}

0 comments on commit efe6cae

Please sign in to comment.