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

protocodec: make a few tweaks to allow inlining #45

Merged
merged 1 commit into from
Apr 9, 2024
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
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
}
Loading