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

feat: Opt for concatenation instead of using fmt.Sprintf #2270

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
- Upgraded go version to 1.21, set toolchain version.
- Reverted rpc-caller-procedure value setting.
- Performance improvement in the tchannel transport by reducing the number of allocations.

## [1.72.1] - 2024-03-14
- tchannel: Renamed caller-procedure header from `$rpc$-caller-procedure` to `rpc-caller-procedure`.
Expand Down
7 changes: 4 additions & 3 deletions pkg/procedure/procedure.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
package procedure

import (
"fmt"
"strings"
)

const _separator = "::"

// ToName gets the procedure name we should use for a method
// with the given service name and method name.
func ToName(serviceName string, methodName string) string {
return fmt.Sprintf("%s::%s", serviceName, methodName)
return serviceName + _separator + methodName
Copy link

Choose a reason for hiding this comment

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

In general, isn't this fully pre-computable and cacheable at startup? All yarpc configuration is static, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that by pre-computing this information early and storing it in the client object, we might further reduce allocations. I'll investigate this possibility in a separate PR.

}

// FromName gets the service name and method name from a procdure name.
func FromName(name string) (serviceName string, methodName string) {
parts := strings.SplitN(name, "::", 2)
parts := strings.SplitN(name, _separator, 2)
if len(parts) == 1 {
return parts[0], ""
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/procedure/procedure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ func TestProcedureNameAndSplit(t *testing.T) {
assert.Equal(t, tt.Method, m)
}
}

func BenchmarkToName(b *testing.B) {
alphabet := "abcdefghijklmnopqrstuvwxyz"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ToName(alphabet, alphabet)
}
}