-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109470 from cockroachdb/blathers/backport-release…
…-23.1-109394 release-23.1: typedesc: avoid slice copy when hydrating enums
- Loading branch information
Showing
3 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package sql | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/security/username" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/lib/pq/oid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// This benchmark tests the performance of resolving an enum with many (10,000) | ||
// values. This is a regression test for #109228. | ||
func BenchmarkResolveTypeByOID(b *testing.B) { | ||
defer leaktest.AfterTest(b)() | ||
defer log.Scope(b).Close(b) | ||
|
||
s, sqlDB, kvDB := serverutils.StartServer(b, base.TestServerArgs{}) | ||
defer s.Stopper().Stop(context.Background()) | ||
|
||
query := strings.Builder{} | ||
query.WriteString("CREATE TYPE typ AS ENUM ('v0'") | ||
for i := 1; i < 10_000; i++ { | ||
query.WriteString(", 'v") | ||
query.WriteString(strconv.Itoa(i)) | ||
query.WriteString("'") | ||
} | ||
query.WriteString(")") | ||
_, err := sqlDB.Exec(query.String()) | ||
require.NoError(b, err) | ||
|
||
var typOID uint32 | ||
err = sqlDB.QueryRow("SELECT 'typ'::regtype::oid").Scan(&typOID) | ||
require.NoError(b, err) | ||
|
||
ctx := context.Background() | ||
execCfg := s.ExecutorConfig().(ExecutorConfig) | ||
sd := NewFakeSessionData(&execCfg.Settings.SV, "test") | ||
sd.Database = "defaultdb" | ||
planner, cleanup := newInternalPlanner("test", kv.NewTxn(ctx, kvDB, s.NodeID()), | ||
username.RootUserName(), &MemoryMetrics{}, &execCfg, sd.SessionData, | ||
) | ||
defer cleanup() | ||
|
||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
typ, err := planner.schemaResolver.ResolveTypeByOID(ctx, oid.Oid(typOID)) | ||
require.NoError(b, err) | ||
require.Equal(b, "typ", typ.Name()) | ||
} | ||
b.StopTimer() | ||
} |