Implemented an assembler
All checks were successful
/ test (push) Successful in 28s

This commit is contained in:
Eduard Urbach 2025-06-24 12:55:26 +02:00
parent 2b703e9af2
commit 70c2da4a4d
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
40 changed files with 821 additions and 117 deletions

4
src/data/Data.go Normal file
View file

@ -0,0 +1,4 @@
package data
// Data saves slices of bytes referenced by labels.
type Data map[string][]byte

28
src/data/Data_test.go Normal file
View file

@ -0,0 +1,28 @@
package data_test
import (
"testing"
"git.urbach.dev/cli/q/src/data"
"git.urbach.dev/go/assert"
)
func TestInterning(t *testing.T) {
d := data.Data{}
d.Insert("label1", []byte("Hello"))
d.Insert("label2", []byte("ello"))
raw, positions := d.Finalize()
assert.DeepEqual(t, raw, []byte("Hello"))
assert.Equal(t, positions["label1"], 0)
assert.Equal(t, positions["label2"], 1)
}
func TestInterningReverse(t *testing.T) {
d := data.Data{}
d.Insert("label1", []byte("ello"))
d.Insert("label2", []byte("Hello"))
raw, positions := d.Finalize()
assert.DeepEqual(t, raw, []byte("Hello"))
assert.Equal(t, positions["label1"], 1)
assert.Equal(t, positions["label2"], 0)
}

41
src/data/Finalize.go Normal file
View file

@ -0,0 +1,41 @@
package data
import (
"bytes"
"sort"
)
// Finalize returns the final raw data slice and a map of labels with their respective indices.
// It will try to reuse existing data whenever possible.
func (data Data) Finalize() ([]byte, map[string]int) {
var (
keys = make([]string, 0, len(data))
positions = make(map[string]int, len(data))
capacity = 0
)
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)
if position != -1 {
positions[key] = position
} else {
positions[key] = len(final)
final = append(final, raw...)
}
}
return final, positions
}

6
src/data/Insert.go Normal file
View file

@ -0,0 +1,6 @@
package data
// Insert registers a slice of bytes for the given label.
func (data Data) Insert(label string, raw []byte) {
data[label] = 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()
}
}