Improved performance of the data finalizer

This commit is contained in:
Eduard Urbach 2025-03-04 16:54:17 +01:00
parent ada7aaa1e2
commit bfa8f9c7c4
Signed by: eduard
GPG key ID: 49226B848C78F6C8
6 changed files with 30 additions and 11 deletions

View file

@ -9,19 +9,22 @@ import (
// It will try to reuse existing data whenever possible.
func (data Data) Finalize() ([]byte, map[string]int32) {
var (
final []byte
keys = make([]string, 0, len(data))
positions = make(map[string]int32, len(data))
capacity = 0
)
for key := range data {
for key, value := range data {
keys = append(keys, key)
capacity += len(value)
}
sort.SliceStable(keys, func(i, j int) bool {
return len(data[keys[i]]) > len(data[keys[j]])
})
final := make([]byte, 0, capacity)
for _, key := range keys {
raw := data[key]
position := bytes.Index(final, raw)

21
src/data/bench_test.go Normal file
View file

@ -0,0 +1,21 @@
package data_test
import (
"testing"
"git.urbach.dev/cli/q/src/data"
)
func BenchmarkFinalize(b *testing.B) {
d := data.Data{}
d.Insert("1", []byte("Beautiful is better than ugly."))
d.Insert("2", []byte("Explicit is better than implicit."))
d.Insert("3", []byte("Simple is better than complex."))
d.Insert("4", []byte("Complex is better than complicated."))
d.Insert("5", []byte("Flat is better than nested."))
d.Insert("6", []byte("Sparse is better than dense."))
for b.Loop() {
d.Finalize()
}
}