-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve performance of readStringVarFromBuff
Optimize readStringVarFromBuff for better performance and memory usage. - The optimizations were tested using new benchmarks: - Short: 33.26% faster, 42.86% less memory usage, 1 fewer allocation. - Medium: 28.34% faster, 54.55% less memory usage, 1 fewer allocation. - Long: 26.90% faster, 73.42% less memory usage, 3 fewer allocations. - Long: with Low Max: 19.12% faster, 48.15% less memory usage, 1 fewer allocation. The overall improvements show significant gains in both execution speed and memory efficiency. For more check eventsreader_bench_test.go. Changes: - Preallocated the buffer with a reasonable initial capacity to avoid repeated slice resizing. - Removed TrimLeft calls since the decoded buffer is converted to a string right after.
- Loading branch information
Showing
3 changed files
with
149 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package bufferdecoder | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func BenchmarkReadStringVarFromBuff_ShortString(b *testing.B) { | ||
buffer := []byte{'H', 'e', 'l', 'l', 'o', 0} | ||
max := 10 | ||
var str string | ||
|
||
for i := 0; i < b.N; i++ { | ||
decoder := New(buffer) | ||
str, _ = readStringVarFromBuff(decoder, max) | ||
} | ||
_ = str | ||
} | ||
|
||
func BenchmarkReadStringVarFromBuff_MediumString(b *testing.B) { | ||
buffer := []byte{'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't', 0} | ||
max := 20 | ||
var str string | ||
|
||
for i := 0; i < b.N; i++ { | ||
decoder := New(buffer) | ||
str, _ = readStringVarFromBuff(decoder, max) | ||
} | ||
_ = str | ||
} | ||
|
||
func BenchmarkReadStringVarFromBuff_LongString(b *testing.B) { | ||
buffer := append(bytes.Repeat([]byte{'A'}, 10000), 0) | ||
max := 10000 | ||
var str string | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
decoder := New(buffer) | ||
str, _ = readStringVarFromBuff(decoder, max) | ||
} | ||
_ = str | ||
} | ||
|
||
func BenchmarkReadStringVarFromBuff_LongStringLowMax(b *testing.B) { | ||
buffer := bytes.Repeat([]byte{'A'}, 10000) | ||
max := 100 | ||
var str string | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
decoder := New(buffer) | ||
str, _ = readStringVarFromBuff(decoder, max) | ||
} | ||
_ = str | ||
} |
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