-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use a reusable bytes.Buffer to speed up MutableTree.String (#456)
The prior code used a naive string concatenation str += "..." which is very slow and inefficient especially when iterating over all the keys, but also it performed an unnecessary byteslice->string conversion for all arguments inside (nodeDB).traverse* using str += fmt.Sprintf("%s: %x\n", string(key), value) notice the `string(key)` passed into fmt.Sprintf("%s"? At bare minimum that code should just simply be: str += fmt.Sprintf("%s: %x\n", key, value) per https://twitter.com/orijtech/status/1462100381803102213?s=20 which saves a whole lot of cycles and RAM. This change uses: * (*bytes.Buffer) in combination with fmt.Fprintf * A sync.Pool with (*bytes.Buffer) values to reuse buffers and the results are profound: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta TreeString-8 111ms ± 8% 4ms ± 4% -96.01% (p=0.000 n=20+20) name old alloc/op new alloc/op delta TreeString-8 734MB ± 0% 2MB ± 1% -99.72% (p=0.000 n=20+20) name old allocs/op new allocs/op delta TreeString-8 37.0k ± 0% 28.7k ± 0% -22.40% (p=0.000 n=20+20) ``` Fixes #455
- Loading branch information
Showing
3 changed files
with
49 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build gcc | ||
// +build gcc | ||
|
||
// This file exists because some of the DBs e.g CLevelDB | ||
|