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

internal/transport: optimize grpc-message encoding/decoding #5654

Merged
merged 1 commit into from
Oct 4, 2022
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
21 changes: 10 additions & 11 deletions internal/transport/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package transport

import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"io"
Expand Down Expand Up @@ -251,13 +250,13 @@ func encodeGrpcMessage(msg string) string {
}

func encodeGrpcMessageUnchecked(msg string) string {
var buf bytes.Buffer
var sb strings.Builder
for len(msg) > 0 {
r, size := utf8.DecodeRuneInString(msg)
for _, b := range []byte(string(r)) {
if size > 1 {
// If size > 1, r is not ascii. Always do percent encoding.
buf.WriteString(fmt.Sprintf("%%%02X", b))
fmt.Fprintf(&sb, "%%%02X", b)
continue
}

Expand All @@ -266,14 +265,14 @@ func encodeGrpcMessageUnchecked(msg string) string {
//
// fmt.Sprintf("%%%02X", utf8.RuneError) gives "%FFFD".
if b >= spaceByte && b <= tildeByte && b != percentByte {
buf.WriteByte(b)
sb.WriteByte(b)
} else {
buf.WriteString(fmt.Sprintf("%%%02X", b))
fmt.Fprintf(&sb, "%%%02X", b)
}
}
msg = msg[size:]
}
return buf.String()
return sb.String()
}

// decodeGrpcMessage decodes the msg encoded by encodeGrpcMessage.
Expand All @@ -291,23 +290,23 @@ func decodeGrpcMessage(msg string) string {
}

func decodeGrpcMessageUnchecked(msg string) string {
var buf bytes.Buffer
var sb strings.Builder
lenMsg := len(msg)
for i := 0; i < lenMsg; i++ {
c := msg[i]
if c == percentByte && i+2 < lenMsg {
parsed, err := strconv.ParseUint(msg[i+1:i+3], 16, 8)
if err != nil {
buf.WriteByte(c)
sb.WriteByte(c)
} else {
buf.WriteByte(byte(parsed))
sb.WriteByte(byte(parsed))
i += 2
}
} else {
buf.WriteByte(c)
sb.WriteByte(c)
}
}
return buf.String()
return sb.String()
}

type bufWriter struct {
Expand Down
24 changes: 24 additions & 0 deletions internal/transport/http_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,27 @@ func (s) TestParseDialTarget(t *testing.T) {
}
}
}

func BenchmarkDecodeGrpcMessage(b *testing.B) {
input := "Hello, %E4%B8%96%E7%95%8C"
want := "Hello, 世界"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
got := decodeGrpcMessage(input)
if got != want {
b.Fatalf("decodeGrpcMessage(%q) = %s, want %s", input, got, want)
}
}
}

func BenchmarkEncodeGrpcMessage(b *testing.B) {
input := "Hello, 世界"
want := "Hello, %E4%B8%96%E7%95%8C"
b.ReportAllocs()
for i := 0; i < b.N; i++ {
got := encodeGrpcMessage(input)
if got != want {
b.Fatalf("encodeGrpcMessage(%q) = %s, want %s", input, got, want)
}
}
}