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

Cleanup gRPC util funcs for timeout and percent encoding #596

Merged
merged 10 commits into from
Oct 3, 2023

Conversation

emcfarlane
Copy link
Contributor

Small cleanup on gRPC util funcs. Removes timeout related init func and improves percent decoder.

Fix for timeout value length: grpcEncodeTimeout("45s") was 45000m but should be 45000000u. 8 char limit should only be applied to the value not the value and unit.

-BenchmarkGRPCPercentEncoding-8           3277959               347.2 ns/op            64 B/op          2 allocs/op
-BenchmarkGRPCPercentDecoding-8          17811729                65.90 ns/op           32 B/op          1 allocs/op
-BenchmarkGRPCTimeoutEncoding-8          15360220                78.53 ns/op           40 B/op          4 allocs/op
+BenchmarkGRPCPercentEncoding-8          18958806                64.16 ns/op           32 B/op          1 allocs/op
+BenchmarkGRPCPercentDecoding-8          22806225                53.29 ns/op           16 B/op          1 allocs/op
+BenchmarkGRPCTimeoutEncoding-8          29902784                40.86 ns/op           24 B/op          2 allocs/op

@emcfarlane emcfarlane changed the title gRPC util cleanup timeout and percent Cleanup gRPC util funcs for timeout and percent encoding Sep 26, 2023
@emcfarlane emcfarlane self-assigned this Sep 26, 2023
protocol_grpc.go Outdated Show resolved Hide resolved
@mattrobenolt
Copy link
Contributor

Side note, in general, I'd be happy to help figure out a workflow to get us setup with benchcmp as a bit more of a standardized way to show results here. Big fan of benchcmp.

@emcfarlane
Copy link
Contributor Author

@mattrobenolt that'd be nice!

protocol_grpc.go Outdated Show resolved Hide resolved
protocol_grpc.go Outdated Show resolved Hide resolved
protocol_grpc.go Outdated Show resolved Hide resolved
protocol_grpc.go Show resolved Hide resolved
Copy link
Contributor

@mattrobenolt mattrobenolt left a comment

Choose a reason for hiding this comment

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

Nice.

@akshayjshah
Copy link
Member

Side note, in general, I'd be happy to help figure out a workflow to get us setup with benchcmp as a bit more of a standardized way to show results here. Big fan of benchcmp.

❤️ That would be amazing, @mattrobenolt. I'm surprised there isn't a halfway decent service for this already :/

Copy link
Member

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

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

Left some comments, all of which I'm going to fix by pushing some commit's to Ed's branch. The two main issues are that:

  • We're leaving a little performance on the table by allocating extra strings when encoding timeouts,
  • We've undone a small performance optimization for gRPC handlers, and
  • I'd prefer not to remove the error return from unit lookup.

Ed, please take a look at my commits and make sure they're okay with you.

protocol_grpc.go Outdated
case 'H':
return time.Hour
default:
return 0
Copy link
Member

Choose a reason for hiding this comment

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

API-wise, this doesn't feel like an improvement to me: we're overloading the zero value in a way that feels odd in Go, and returning an error won't measurably affect performance.

protocol_grpc.go Outdated
size, unit = time.Hour, 'H'
}
value := timeout / size
return strconv.FormatInt(int64(value), 10 /* base */) + string(unit)
Copy link
Member

Choose a reason for hiding this comment

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

As long as we're golfing allocs, we can probably shave an alloc here by using strconv.AppendInt.

protocol_grpc.go Show resolved Hide resolved
protocol_grpc_test.go Outdated Show resolved Hide resolved
protocol_grpc_test.go Outdated Show resolved Hide resolved
protocol_grpc.go Show resolved Hide resolved
protocol_grpc.go Show resolved Hide resolved
protocol_grpc.go Outdated Show resolved Hide resolved
protocol_grpc.go Outdated
@@ -134,7 +109,7 @@ type grpcHandler struct {
}

func (g *grpcHandler) Methods() map[string]struct{} {
return grpcAllowedMethods
return map[string]struct{}{http.MethodPost: {}}
Copy link
Member

Choose a reason for hiding this comment

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

This is an extra allocation along the request path. The returned map isn't accessible to Connect users, so I think we ought to keep the code as-is.

Was there a safety concern here, or were we just trying to get rid of globals?

protocol_grpc_test.go Outdated Show resolved Hide resolved
@akshayjshah
Copy link
Member

Pushed some extra commits to resolve all the review feedback I left. make bench now runs all the benchmarks in the root package, and you can use BENCH=Timeout make bench to run just a subset of tests.

With these changes, timeout encoding drops to one allocation.

@emcfarlane
Copy link
Contributor Author

All good with me!

@akshayjshah akshayjshah merged commit d423d1e into main Oct 3, 2023
7 checks passed
@akshayjshah akshayjshah deleted the ed/grpcutil branch October 3, 2023 05:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants