aboutsummaryrefslogtreecommitdiff
path: root/internal/postgres/insert_module.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2020-06-03 20:18:41 -0400
committerJonathan Amsterdam <jba@google.com>2020-06-04 02:18:16 +0000
commitba4f3b3e6d7f4d52c6f2aa607dece5871a164027 (patch)
tree602a1a156b3e2afcd690e84bbf060ec48d12a29e /internal/postgres/insert_module.go
parentd1b4cbad386ea2b55c965f04f6ccfe3a7026e79f (diff)
downloadgo-x-pkgsite-ba4f3b3e6d7f4d52c6f2aa607dece5871a164027.tar.xz
internal/postgres: makeValidUnicode only copies if necessary
Avoid copying a string if it's already valid unicode. This saves memory, which may matter. See bug for context. Updates b/158067614. Change-Id: I402b7823cba240e593fe1b094f4f85a9cf5eeec2 Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/762241 CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com> Reviewed-by: Julie Qiu <julieqiu@google.com>
Diffstat (limited to 'internal/postgres/insert_module.go')
-rw-r--r--internal/postgres/insert_module.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go
index 7d6652a1..0284ed9e 100644
--- a/internal/postgres/insert_module.go
+++ b/internal/postgres/insert_module.go
@@ -12,6 +12,7 @@ import (
"fmt"
"sort"
"strings"
+ "unicode/utf8"
"github.com/lib/pq"
"go.opencensus.io/trace"
@@ -677,6 +678,18 @@ func (db *DB) DeleteModule(ctx context.Context, modulePath, version string) (err
// characters with the Unicode replacement character, which is the behavior of
// for ... range on strings.
func makeValidUnicode(s string) string {
+ // If s is valid and has no zeroes, don't copy it.
+ hasZeroes := false
+ for _, r := range s {
+ if r == 0 {
+ hasZeroes = true
+ break
+ }
+ }
+ if !hasZeroes && utf8.ValidString(s) {
+ return s
+ }
+
var b strings.Builder
for _, r := range s {
if r != 0 {