Repository navigation
Conversation
strPath built each key with string(segment), which converts the int32 path element to a rune. Every number that is not a valid code point, including the google.api.http (72295728) and google.api.routing (72295729) extension numbers, became U+FFFD, so the routing option's location overwrote the http option's location. MethodHTTPRule then returned the routing span, rules reported HTTP findings on the wrong lines, and disable comments on the http option were ignored. Write each segment as a decimal number.
There was a problem hiding this comment.
Code Review
This pull request fixes a bug in strPath where path segments were incorrectly converted to runes, causing large numbers (like extension numbers) to map to U+FFFD. The fix uses strconv.Itoa and strings.Builder to format segments as decimal numbers, and a new test case is added to verify this behavior. The review feedback suggests a great optimization to avoid heap allocations in strPath by using a stack-allocated byte slice and strconv.AppendInt, which would also eliminate the need for the strings import.
| "strconv" | ||
| "strings" | ||
| "sync" |
There was a problem hiding this comment.
| func strPath(segments []int32) string { | ||
| var b strings.Builder | ||
| for i, segment := range segments { | ||
| if i > 0 { | ||
| p += "," | ||
| b.WriteByte(',') | ||
| } | ||
| p += string(segment) | ||
| b.WriteString(strconv.Itoa(int(segment))) | ||
| } | ||
| return | ||
| return b.String() | ||
| } |
There was a problem hiding this comment.
We can optimize strPath to avoid heap allocations for individual path segments. Currently, strconv.Itoa allocates a new string for any segment value that is not a small integer (e.g., extension numbers like 72295728).
By using a stack-allocated byte slice (var buf [64]byte) and strconv.AppendInt, we can format the integers directly into the buffer and perform exactly one allocation when converting the final byte slice to a string.
| func strPath(segments []int32) string { | |
| var b strings.Builder | |
| for i, segment := range segments { | |
| if i > 0 { | |
| p += "," | |
| b.WriteByte(',') | |
| } | |
| p += string(segment) | |
| b.WriteString(strconv.Itoa(int(segment))) | |
| } | |
| return | |
| return b.String() | |
| } | |
| func strPath(segments []int32) string { | |
| if len(segments) == 0 { | |
| return "" | |
| } | |
| var buf [64]byte | |
| b := buf[:0] | |
| for i, segment := range segments { | |
| if i > 0 { | |
| b = append(b, ',') | |
| } | |
| b = strconv.AppendInt(b, int64(segment), 10) | |
| } | |
| return string(b) | |
| } |
strPathbuilds the key for each source location withstring(segment), which converts every int32 path element to a rune. Numbers that are not valid code points (above 0x10FFFF, or in 55296-57343) all become U+FFFD, so two paths that differ only in such a number share a key and the later location overwrites the earlier one.google.api.http(72295728) andgoogle.api.routing(72295729) are both above 0x10FFFF. On a method that setsgoogle.api.routingaftergoogle.api.http,locations.MethodHTTPRulereturns the span of the routing annotation, so every rule that reports on the HTTP rule points at the wrong lines, and a disable comment placed on thegoogle.api.httpoption is ignored.This shows up on real APIs.
google/datastore/v1/datastore.protodeclares http and then routing on all 8 RPCs; before this change the linter reports its 8core::0127::resource-name-extractionfindings on the routing blocks (for example lines 56-59 forLookup), and with this change on the http blocks (52-55).google/bigtable/v2/bigtable.protouses the same order.The fix writes each segment as a decimal number. The new
TestMethodHTTPRuleWithRoutingfails without the fix (it gets the routing span{9, 4, 11, 6}) and passes with it;go test ./...passes.Building the keys this way is also cheaper: over the 1001 source locations of
bigtable.protothe key loop goes from about 0.31 ms to 0.16 ms (medians of 6 interleaved benchmark runs) and from 14661 to 2248 allocations. Lintingbigtable.protoend to end takes about the same time before and after (median 119 ms vs 114 ms over 11 interleaved runs).This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.