Skip to content

Commit

Permalink
protocodec: make a few tweaks to allow inlining
Browse files Browse the repository at this point in the history
protocodec.GalaxyGet is particularly hot, and trivial so it should get
inlined. Make a few tweaks to allow inlining. (bringing the cost down
below the magic 80 number in the go compiler)

Similarly, split up protocodec.backendGetterV2.Get to allow inlining of
the fast path. (Amusingly, this also allows the new `setSlow` to inline,
but that wasn't the goal)
  • Loading branch information
dfinkel committed Apr 9, 2024
1 parent c7ef985 commit f34785d
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 f34785d

Please sign in to comment.