From c04cd7d054a4e0d01855b804868c007cccb2455c Mon Sep 17 00:00:00 2001 From: Dave Enyeart Date: Tue, 25 Jan 2022 03:02:23 -0500 Subject: [PATCH] Bump Go to 1.17.5 (release-2.2) (#3186) * Run goimports for Go 1.17.5 (release-2.2) Run goimports for Go 1.17.5 to clean up build instructions. Signed-off-by: David Enyeart * Bump Go to 1.17.5 Bump Go to 1.17.5. Fix unit tests to work with Go 1.17.5. Update dependencies to work with Go 1.17. Signed-off-by: David Enyeart * Update docs for Go 1.17 Update docs for Go 1.17. Signed-off-by: David Enyeart --- Makefile | 2 +- bccsp/factory/nopkcs11.go | 1 + bccsp/factory/nopkcs11_test.go | 1 + bccsp/factory/pkcs11.go | 1 + bccsp/factory/pkcs11_test.go | 1 + bccsp/factory/pkcs11factory.go | 1 + bccsp/factory/pkcs11factory_test.go | 1 + bccsp/pkcs11/ecdsakey_test.go | 1 + bccsp/pkcs11/pkcs11_test.go | 1 + ci/azure-pipelines-merge.yml | 2 +- ci/azure-pipelines-release.yml | 2 +- ci/azure-pipelines.yml | 2 +- common/crypto/expiration_test.go | 4 +- core/handlers/library/race_test.go | 1 + docs/source/dev-setup/devenv.rst | 2 +- go.mod | 2 + go.sum | 4 +- integration/ledger/ledger_generate_test.go | 1 + internal/peer/node/signals.go | 1 + internal/peer/node/signals_windows.go | 1 + internal/pkg/comm/server_test.go | 2 +- internal/pkg/comm/testdata/certs/generate.go | 5 +- orderer/common/cluster/util_test.go | 4 +- orderer/common/server/signals.go | 1 + orderer/common/server/signals_windows.go | 1 + vagrant/golang.sh | 2 +- .../github.com/cespare/xxhash/v2/.travis.yml | 8 --- vendor/github.com/cespare/xxhash/v2/README.md | 6 +- vendor/github.com/cespare/xxhash/v2/xxhash.go | 1 - .../cespare/xxhash/v2/xxhash_amd64.s | 62 +++++++++---------- .../cespare/xxhash/v2/xxhash_unsafe.go | 53 +++++++++------- vendor/modules.txt | 3 +- 32 files changed, 101 insertions(+), 79 deletions(-) delete mode 100644 vendor/github.com/cespare/xxhash/v2/.travis.yml diff --git a/Makefile b/Makefile index f77a02b2b3d..5570fda4bdd 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,7 @@ METADATA_VAR += CommitSHA=$(EXTRA_VERSION) METADATA_VAR += BaseDockerLabel=$(BASE_DOCKER_LABEL) METADATA_VAR += DockerNamespace=$(DOCKER_NS) -GO_VER = 1.16.7 +GO_VER = 1.17.5 GO_TAGS ?= RELEASE_EXES = orderer $(TOOLS_EXES) diff --git a/bccsp/factory/nopkcs11.go b/bccsp/factory/nopkcs11.go index 7896cf2bb0d..625bcdef30e 100644 --- a/bccsp/factory/nopkcs11.go +++ b/bccsp/factory/nopkcs11.go @@ -1,3 +1,4 @@ +//go:build !pkcs11 // +build !pkcs11 /* diff --git a/bccsp/factory/nopkcs11_test.go b/bccsp/factory/nopkcs11_test.go index c6a3f835066..95d17d6bced 100644 --- a/bccsp/factory/nopkcs11_test.go +++ b/bccsp/factory/nopkcs11_test.go @@ -1,3 +1,4 @@ +//go:build !pkcs11 // +build !pkcs11 /* diff --git a/bccsp/factory/pkcs11.go b/bccsp/factory/pkcs11.go index a0a1932dac2..ee28b094b2c 100644 --- a/bccsp/factory/pkcs11.go +++ b/bccsp/factory/pkcs11.go @@ -1,3 +1,4 @@ +//go:build pkcs11 // +build pkcs11 /* diff --git a/bccsp/factory/pkcs11_test.go b/bccsp/factory/pkcs11_test.go index 88ff2531a57..3627f5ae66d 100644 --- a/bccsp/factory/pkcs11_test.go +++ b/bccsp/factory/pkcs11_test.go @@ -1,3 +1,4 @@ +//go:build pkcs11 // +build pkcs11 /* diff --git a/bccsp/factory/pkcs11factory.go b/bccsp/factory/pkcs11factory.go index 40890be5540..8ea5e837262 100644 --- a/bccsp/factory/pkcs11factory.go +++ b/bccsp/factory/pkcs11factory.go @@ -1,3 +1,4 @@ +//go:build pkcs11 // +build pkcs11 /* diff --git a/bccsp/factory/pkcs11factory_test.go b/bccsp/factory/pkcs11factory_test.go index ba51610a1f1..3ad9460e469 100644 --- a/bccsp/factory/pkcs11factory_test.go +++ b/bccsp/factory/pkcs11factory_test.go @@ -1,3 +1,4 @@ +//go:build pkcs11 // +build pkcs11 /* diff --git a/bccsp/pkcs11/ecdsakey_test.go b/bccsp/pkcs11/ecdsakey_test.go index 3af5c2ae8ec..df90215f6e8 100644 --- a/bccsp/pkcs11/ecdsakey_test.go +++ b/bccsp/pkcs11/ecdsakey_test.go @@ -1,3 +1,4 @@ +//go:build pkcs11 // +build pkcs11 /* diff --git a/bccsp/pkcs11/pkcs11_test.go b/bccsp/pkcs11/pkcs11_test.go index 40ffd654476..9d0830e455a 100644 --- a/bccsp/pkcs11/pkcs11_test.go +++ b/bccsp/pkcs11/pkcs11_test.go @@ -1,3 +1,4 @@ +//go:build pkcs11 // +build pkcs11 /* diff --git a/ci/azure-pipelines-merge.yml b/ci/azure-pipelines-merge.yml index 5fc79ade570..ad6ca5e27c8 100644 --- a/ci/azure-pipelines-merge.yml +++ b/ci/azure-pipelines-merge.yml @@ -11,7 +11,7 @@ pr: none variables: GOPATH: $(Agent.BuildDirectory)/go PATH: $(Agent.BuildDirectory)/go/bin:/usr/local/go/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin - GOVER: 1.16.7 + GOVER: 1.17.5 jobs: - job: UnitTests diff --git a/ci/azure-pipelines-release.yml b/ci/azure-pipelines-release.yml index e07cfb12b30..364ccebbdb5 100644 --- a/ci/azure-pipelines-release.yml +++ b/ci/azure-pipelines-release.yml @@ -11,7 +11,7 @@ variables: - name: GOPATH value: $(Agent.BuildDirectory)/go - name: GOVER - value: 1.16.7 + value: 1.17.5 stages: - stage: BuildBinaries diff --git a/ci/azure-pipelines.yml b/ci/azure-pipelines.yml index 911a71e2d46..f026b32e723 100644 --- a/ci/azure-pipelines.yml +++ b/ci/azure-pipelines.yml @@ -11,7 +11,7 @@ pr: variables: GOPATH: $(Agent.BuildDirectory)/go PATH: $(Agent.BuildDirectory)/go/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin - GOVER: 1.16.7 + GOVER: 1.17.5 stages: - stage: VerifyBuild diff --git a/common/crypto/expiration_test.go b/common/crypto/expiration_test.go index f3f438d000c..419004bd2a9 100644 --- a/common/crypto/expiration_test.go +++ b/common/crypto/expiration_test.go @@ -277,14 +277,14 @@ cRv6rqxvy5M+t0DhRtiwCen70YCUsksb }{ { description: "Bad first certificate", - errContains: "asn1:", + errContains: "malformed certificate", first: []byte{1, 2, 3}, second: bob, }, { description: "Bad second certificate", - errContains: "asn1:", + errContains: "malformed certificate", first: alice, second: []byte{1, 2, 3}, }, diff --git a/core/handlers/library/race_test.go b/core/handlers/library/race_test.go index dd12f0eab19..b78141cfb0f 100644 --- a/core/handlers/library/race_test.go +++ b/core/handlers/library/race_test.go @@ -1,3 +1,4 @@ +//go:build race // +build race /* diff --git a/docs/source/dev-setup/devenv.rst b/docs/source/dev-setup/devenv.rst index 69a28b1183b..8dd31ce3ad8 100644 --- a/docs/source/dev-setup/devenv.rst +++ b/docs/source/dev-setup/devenv.rst @@ -5,7 +5,7 @@ Prerequisites ~~~~~~~~~~~~~ - `Git client `__ -- `Go `__ version 1.15.x +- `Go `__ version 1.17.x - `Docker `__ version 18.03 or later - (macOS) `Xcode Command Line Tools `__ - `SoftHSM `__ diff --git a/go.mod b/go.mod index 2cb69d35242..321fb344311 100644 --- a/go.mod +++ b/go.mod @@ -73,3 +73,5 @@ require ( ) replace github.com/onsi/gomega => github.com/onsi/gomega v1.9.0 + +replace github.com/cespare/xxhash/v2 => github.com/cespare/xxhash/v2 v2.1.2 // fix for Go 1.17 in github.com/prometheus/client_golang dependency without updating protobuf diff --git a/go.sum b/go.sum index d637e88991e..b5442768f19 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= -github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/containerd/continuity v0.0.0-20181203112020-004b46473808/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y= diff --git a/integration/ledger/ledger_generate_test.go b/integration/ledger/ledger_generate_test.go index a7a330ed8ad..5cb304a21cd 100644 --- a/integration/ledger/ledger_generate_test.go +++ b/integration/ledger/ledger_generate_test.go @@ -1,3 +1,4 @@ +//go:build generate // +build generate /* diff --git a/internal/peer/node/signals.go b/internal/peer/node/signals.go index 8fb28452c66..a0391139455 100644 --- a/internal/peer/node/signals.go +++ b/internal/peer/node/signals.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows /* diff --git a/internal/peer/node/signals_windows.go b/internal/peer/node/signals_windows.go index 31d95f67d09..ba4e6f7426e 100644 --- a/internal/peer/node/signals_windows.go +++ b/internal/peer/node/signals_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows /* diff --git a/internal/pkg/comm/server_test.go b/internal/pkg/comm/server_test.go index ca48d567f45..21e9e89f3e4 100644 --- a/internal/pkg/comm/server_test.go +++ b/internal/pkg/comm/server_test.go @@ -496,7 +496,7 @@ func TestNewGRPCServerInvalidParameters(t *testing.T) { badRootCAs := [][]byte{[]byte(badPEM)} err = srv.SetClientRootCAs(badRootCAs) - assert.EqualError(t, err, "failed to set client root certificate(s): asn1: syntax error: data truncated") + assert.EqualError(t, err, "failed to set client root certificate(s): x509: malformed certificate") } func TestNewGRPCServer(t *testing.T) { diff --git a/internal/pkg/comm/testdata/certs/generate.go b/internal/pkg/comm/testdata/certs/generate.go index 6f1dfc56548..8eb0dbbaedb 100644 --- a/internal/pkg/comm/testdata/certs/generate.go +++ b/internal/pkg/comm/testdata/certs/generate.go @@ -1,11 +1,12 @@ +//go:build ignore +// +build ignore + /* Copyright IBM Corp. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ -// +build ignore - //go:generate -command gencerts go run ./generate.go //go:generate gencerts -orgs 2 -child-orgs 2 -servers 2 -clients 2 diff --git a/orderer/common/cluster/util_test.go b/orderer/common/cluster/util_test.go index c2eab43c777..059e0f95ecb 100644 --- a/orderer/common/cluster/util_test.go +++ b/orderer/common/cluster/util_test.go @@ -157,7 +157,7 @@ func TestDialerBadConfig(t *testing.T) { _, err := dialer.Dial("127.0.0.1:8080", func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { return nil }) - assert.EqualError(t, err, "error adding root certificate: asn1: syntax error: sequence truncated") + assert.EqualError(t, err, "error adding root certificate: x509: malformed certificate") } func TestDERtoPEM(t *testing.T) { @@ -178,7 +178,7 @@ func TestStandardDialer(t *testing.T) { _, err := standardDialer.Dial(cluster.EndpointCriteria{Endpoint: "127.0.0.1:8080", TLSRootCAs: certPool}) assert.EqualError(t, err, - "failed creating gRPC client: error adding root certificate: asn1: syntax error: sequence truncated", + "failed creating gRPC client: error adding root certificate: x509: malformed certificate", ) } diff --git a/orderer/common/server/signals.go b/orderer/common/server/signals.go index d491d9fa892..fbb3ce71223 100644 --- a/orderer/common/server/signals.go +++ b/orderer/common/server/signals.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows /* diff --git a/orderer/common/server/signals_windows.go b/orderer/common/server/signals_windows.go index 06c4dd235c3..7bf5c1b7611 100644 --- a/orderer/common/server/signals_windows.go +++ b/orderer/common/server/signals_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows /* diff --git a/vagrant/golang.sh b/vagrant/golang.sh index 218e77c5fda..1423133f0f6 100644 --- a/vagrant/golang.sh +++ b/vagrant/golang.sh @@ -5,7 +5,7 @@ # SPDX-License-Identifier: Apache-2.0 GOROOT='/opt/go' -GO_VERSION=1.16.7 +GO_VERSION=1.17.5 # ---------------------------------------------------------------- # Install Golang diff --git a/vendor/github.com/cespare/xxhash/v2/.travis.yml b/vendor/github.com/cespare/xxhash/v2/.travis.yml deleted file mode 100644 index c516ea88da7..00000000000 --- a/vendor/github.com/cespare/xxhash/v2/.travis.yml +++ /dev/null @@ -1,8 +0,0 @@ -language: go -go: - - "1.x" - - master -env: - - TAGS="" - - TAGS="-tags purego" -script: go test $TAGS -v ./... diff --git a/vendor/github.com/cespare/xxhash/v2/README.md b/vendor/github.com/cespare/xxhash/v2/README.md index 2fd8693c21b..792b4a60b34 100644 --- a/vendor/github.com/cespare/xxhash/v2/README.md +++ b/vendor/github.com/cespare/xxhash/v2/README.md @@ -1,7 +1,7 @@ # xxhash -[![GoDoc](https://godoc.org/github.com/cespare/xxhash?status.svg)](https://godoc.org/github.com/cespare/xxhash) -[![Build Status](https://travis-ci.org/cespare/xxhash.svg?branch=master)](https://travis-ci.org/cespare/xxhash) +[![Go Reference](https://pkg.go.dev/badge/github.com/cespare/xxhash/v2.svg)](https://pkg.go.dev/github.com/cespare/xxhash/v2) +[![Test](https://github.com/cespare/xxhash/actions/workflows/test.yml/badge.svg)](https://github.com/cespare/xxhash/actions/workflows/test.yml) xxhash is a Go implementation of the 64-bit [xxHash](http://cyan4973.github.io/xxHash/) algorithm, XXH64. This is a @@ -64,4 +64,6 @@ $ go test -benchtime 10s -bench '/xxhash,direct,bytes' - [InfluxDB](https://github.com/influxdata/influxdb) - [Prometheus](https://github.com/prometheus/prometheus) +- [VictoriaMetrics](https://github.com/VictoriaMetrics/VictoriaMetrics) - [FreeCache](https://github.com/coocood/freecache) +- [FastCache](https://github.com/VictoriaMetrics/fastcache) diff --git a/vendor/github.com/cespare/xxhash/v2/xxhash.go b/vendor/github.com/cespare/xxhash/v2/xxhash.go index db0b35fbe39..15c835d5417 100644 --- a/vendor/github.com/cespare/xxhash/v2/xxhash.go +++ b/vendor/github.com/cespare/xxhash/v2/xxhash.go @@ -193,7 +193,6 @@ func (d *Digest) UnmarshalBinary(b []byte) error { b, d.v4 = consumeUint64(b) b, d.total = consumeUint64(b) copy(d.mem[:], b) - b = b[len(d.mem):] d.n = int(d.total % uint64(len(d.mem))) return nil } diff --git a/vendor/github.com/cespare/xxhash/v2/xxhash_amd64.s b/vendor/github.com/cespare/xxhash/v2/xxhash_amd64.s index d580e32aed4..be8db5bf796 100644 --- a/vendor/github.com/cespare/xxhash/v2/xxhash_amd64.s +++ b/vendor/github.com/cespare/xxhash/v2/xxhash_amd64.s @@ -6,7 +6,7 @@ // Register allocation: // AX h -// CX pointer to advance through b +// SI pointer to advance through b // DX n // BX loop end // R8 v1, k1 @@ -16,39 +16,39 @@ // R12 tmp // R13 prime1v // R14 prime2v -// R15 prime4v +// DI prime4v -// round reads from and advances the buffer pointer in CX. +// round reads from and advances the buffer pointer in SI. // It assumes that R13 has prime1v and R14 has prime2v. #define round(r) \ - MOVQ (CX), R12 \ - ADDQ $8, CX \ + MOVQ (SI), R12 \ + ADDQ $8, SI \ IMULQ R14, R12 \ ADDQ R12, r \ ROLQ $31, r \ IMULQ R13, r // mergeRound applies a merge round on the two registers acc and val. -// It assumes that R13 has prime1v, R14 has prime2v, and R15 has prime4v. +// It assumes that R13 has prime1v, R14 has prime2v, and DI has prime4v. #define mergeRound(acc, val) \ IMULQ R14, val \ ROLQ $31, val \ IMULQ R13, val \ XORQ val, acc \ IMULQ R13, acc \ - ADDQ R15, acc + ADDQ DI, acc // func Sum64(b []byte) uint64 TEXT ·Sum64(SB), NOSPLIT, $0-32 // Load fixed primes. MOVQ ·prime1v(SB), R13 MOVQ ·prime2v(SB), R14 - MOVQ ·prime4v(SB), R15 + MOVQ ·prime4v(SB), DI // Load slice. - MOVQ b_base+0(FP), CX + MOVQ b_base+0(FP), SI MOVQ b_len+8(FP), DX - LEAQ (CX)(DX*1), BX + LEAQ (SI)(DX*1), BX // The first loop limit will be len(b)-32. SUBQ $32, BX @@ -65,14 +65,14 @@ TEXT ·Sum64(SB), NOSPLIT, $0-32 XORQ R11, R11 SUBQ R13, R11 - // Loop until CX > BX. + // Loop until SI > BX. blockLoop: round(R8) round(R9) round(R10) round(R11) - CMPQ CX, BX + CMPQ SI, BX JLE blockLoop MOVQ R8, AX @@ -100,16 +100,16 @@ noBlocks: afterBlocks: ADDQ DX, AX - // Right now BX has len(b)-32, and we want to loop until CX > len(b)-8. + // Right now BX has len(b)-32, and we want to loop until SI > len(b)-8. ADDQ $24, BX - CMPQ CX, BX + CMPQ SI, BX JG fourByte wordLoop: // Calculate k1. - MOVQ (CX), R8 - ADDQ $8, CX + MOVQ (SI), R8 + ADDQ $8, SI IMULQ R14, R8 ROLQ $31, R8 IMULQ R13, R8 @@ -117,18 +117,18 @@ wordLoop: XORQ R8, AX ROLQ $27, AX IMULQ R13, AX - ADDQ R15, AX + ADDQ DI, AX - CMPQ CX, BX + CMPQ SI, BX JLE wordLoop fourByte: ADDQ $4, BX - CMPQ CX, BX + CMPQ SI, BX JG singles - MOVL (CX), R8 - ADDQ $4, CX + MOVL (SI), R8 + ADDQ $4, SI IMULQ R13, R8 XORQ R8, AX @@ -138,19 +138,19 @@ fourByte: singles: ADDQ $4, BX - CMPQ CX, BX + CMPQ SI, BX JGE finalize singlesLoop: - MOVBQZX (CX), R12 - ADDQ $1, CX + MOVBQZX (SI), R12 + ADDQ $1, SI IMULQ ·prime5v(SB), R12 XORQ R12, AX ROLQ $11, AX IMULQ R13, AX - CMPQ CX, BX + CMPQ SI, BX JL singlesLoop finalize: @@ -179,9 +179,9 @@ TEXT ·writeBlocks(SB), NOSPLIT, $0-40 MOVQ ·prime2v(SB), R14 // Load slice. - MOVQ b_base+8(FP), CX + MOVQ b_base+8(FP), SI MOVQ b_len+16(FP), DX - LEAQ (CX)(DX*1), BX + LEAQ (SI)(DX*1), BX SUBQ $32, BX // Load vN from d. @@ -199,7 +199,7 @@ blockLoop: round(R10) round(R11) - CMPQ CX, BX + CMPQ SI, BX JLE blockLoop // Copy vN back to d. @@ -208,8 +208,8 @@ blockLoop: MOVQ R10, 16(AX) MOVQ R11, 24(AX) - // The number of bytes written is CX minus the old base pointer. - SUBQ b_base+8(FP), CX - MOVQ CX, ret+32(FP) + // The number of bytes written is SI minus the old base pointer. + SUBQ b_base+8(FP), SI + MOVQ SI, ret+32(FP) RET diff --git a/vendor/github.com/cespare/xxhash/v2/xxhash_unsafe.go b/vendor/github.com/cespare/xxhash/v2/xxhash_unsafe.go index 53bf76efbc2..376e0ca2e49 100644 --- a/vendor/github.com/cespare/xxhash/v2/xxhash_unsafe.go +++ b/vendor/github.com/cespare/xxhash/v2/xxhash_unsafe.go @@ -6,41 +6,52 @@ package xxhash import ( - "reflect" "unsafe" ) -// Notes: +// In the future it's possible that compiler optimizations will make these +// XxxString functions unnecessary by realizing that calls such as +// Sum64([]byte(s)) don't need to copy s. See https://golang.org/issue/2205. +// If that happens, even if we keep these functions they can be replaced with +// the trivial safe code. + +// NOTE: The usual way of doing an unsafe string-to-[]byte conversion is: // -// See https://groups.google.com/d/msg/golang-nuts/dcjzJy-bSpw/tcZYBzQqAQAJ -// for some discussion about these unsafe conversions. +// var b []byte +// bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) +// bh.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data +// bh.Len = len(s) +// bh.Cap = len(s) // -// In the future it's possible that compiler optimizations will make these -// unsafe operations unnecessary: https://golang.org/issue/2205. +// Unfortunately, as of Go 1.15.3 the inliner's cost model assigns a high enough +// weight to this sequence of expressions that any function that uses it will +// not be inlined. Instead, the functions below use a different unsafe +// conversion designed to minimize the inliner weight and allow both to be +// inlined. There is also a test (TestInlining) which verifies that these are +// inlined. // -// Both of these wrapper functions still incur function call overhead since they -// will not be inlined. We could write Go/asm copies of Sum64 and Digest.Write -// for strings to squeeze out a bit more speed. Mid-stack inlining should -// eventually fix this. +// See https://github.com/golang/go/issues/42739 for discussion. // Sum64String computes the 64-bit xxHash digest of s. // It may be faster than Sum64([]byte(s)) by avoiding a copy. func Sum64String(s string) uint64 { - var b []byte - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - bh.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data - bh.Len = len(s) - bh.Cap = len(s) + b := *(*[]byte)(unsafe.Pointer(&sliceHeader{s, len(s)})) return Sum64(b) } // WriteString adds more data to d. It always returns len(s), nil. // It may be faster than Write([]byte(s)) by avoiding a copy. func (d *Digest) WriteString(s string) (n int, err error) { - var b []byte - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - bh.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data - bh.Len = len(s) - bh.Cap = len(s) - return d.Write(b) + d.Write(*(*[]byte)(unsafe.Pointer(&sliceHeader{s, len(s)}))) + // d.Write always returns len(s), nil. + // Ignoring the return output and returning these fixed values buys a + // savings of 6 in the inliner's cost model. + return len(s), nil +} + +// sliceHeader is similar to reflect.SliceHeader, but it assumes that the layout +// of the first two words is the same as the layout of a string. +type sliceHeader struct { + s string + cap int } diff --git a/vendor/modules.txt b/vendor/modules.txt index 73349354018..b32d28c52e6 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -36,7 +36,7 @@ github.com/alecthomas/template/parse github.com/alecthomas/units # github.com/beorn7/perks v1.0.1 github.com/beorn7/perks/quantile -# github.com/cespare/xxhash/v2 v2.1.1 +# github.com/cespare/xxhash/v2 v2.1.1 => github.com/cespare/xxhash/v2 v2.1.2 github.com/cespare/xxhash/v2 # github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc ## explicit @@ -477,3 +477,4 @@ gopkg.in/yaml.v2 gopkg.in/yaml.v3 # golang.org/x/sys => golang.org/x/sys v0.0.0-20190920190810-ef0ce1748380 # github.com/onsi/gomega => github.com/onsi/gomega v1.9.0 +# github.com/cespare/xxhash/v2 => github.com/cespare/xxhash/v2 v2.1.2