forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changefeedccl: Improve JSON encoder performance
Rewrite JSON encoder to improve its performance. Prior to this change JSON encoder was very inefficient. This inefficiency had multiple underlying reasons: * New Go map objects were constructed for each event. * Underlying json conversion functions had inefficiencies (tracked in cockroachdb#87968) * Conversion of Go maps to JSON incurs the cost of sorting the keys -- for each row. Sorting, particularly when rows are wide, has significant cost. * Each conversion to JSON allocated new array builder (to encode keys) and new object builder; that too has cost. * Underlying code structure, while attempting to reuse code when constructing different "envelope" formats, cause the code to be more inefficient. This PR addresses all of the above. In particular, since a schema version for the table is guaranteeed to have the same set of primary key and value columns, we can construct JSON builders once. The expensive sort operation can be performed once per version; builders can be memoized and cached. The performance impact is significant: * Key encoding speed up is 5-30%, depending on the number of primary keys. * Value encoding 30% - 60% faster (slowest being "wrapped" envelope with diff -- which effectively encodes 2x values) * Byte allocations per row reduces by over 70%, with the number of allocations reduced similarly. Release note (enterprise change): Changefeed JSON encoder performance improved by 50%. Release justification: performance improvement
- Loading branch information
Yevgeniy Miretskiy
committed
Sep 28, 2022
1 parent
d170075
commit e6dbfbc
Showing
17 changed files
with
469 additions
and
149 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
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,42 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package cdcevent | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" | ||
"github.com/cockroachdb/cockroach/pkg/util/cache" | ||
) | ||
|
||
// DefaultCacheConfig is the default configuration for unordered cache. | ||
var DefaultCacheConfig = cache.Config{ | ||
Policy: cache.CacheFIFO, | ||
// TODO: If we find ourselves thrashing here in changefeeds on many tables, | ||
// we can improve performance by eagerly evicting versions using Resolved notifications. | ||
// A old Version with a timestamp entirely before a notification can be safely evicted. | ||
ShouldEvict: func(size int, _ interface{}, _ interface{}) bool { return size > 1024 }, | ||
} | ||
|
||
// CacheKey is the key for the event caches. | ||
type CacheKey struct { | ||
ID descpb.ID | ||
Version descpb.DescriptorVersion | ||
FamilyID descpb.FamilyID | ||
} | ||
|
||
// GetCachedOrCreate returns cached object, or creates and caches new one. | ||
func GetCachedOrCreate( | ||
k CacheKey, c *cache.UnorderedCache, creator func() interface{}, | ||
) interface{} { | ||
if v, ok := c.Get(k); ok { | ||
return v | ||
} | ||
v := creator() | ||
c.Add(k, v) | ||
return v | ||
} |
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
Oops, something went wrong.