Improved performance of the data finalizer

This commit is contained in:
Eduard Urbach 2025-03-04 16:54:17 +01:00
parent e5f0123eea
commit f2db223684
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 30 additions and 11 deletions

View File

@ -121,7 +121,7 @@ func (c *compiler) compile(x asm.Instruction) {
case asm.PUSH: case asm.PUSH:
switch operands := x.Data.(type) { switch operands := x.Data.(type) {
case *asm.Number: case *asm.Number:
c.code = x86.PushNumber(c.code, operands.Number) c.code = x86.PushNumber(c.code, int32(operands.Number))
case *asm.Register: case *asm.Register:
c.code = x86.PushRegister(c.code, operands.Register) c.code = x86.PushRegister(c.code, operands.Register)
} }

View File

@ -1,7 +1,6 @@
package core package core
import ( import (
"fmt"
"slices" "slices"
"git.urbach.dev/cli/q/src/asm" "git.urbach.dev/cli/q/src/asm"
@ -37,7 +36,7 @@ func (f *Function) CallExtern(fn *Function, parameters []*expression.Expression)
f.Number(asm.PUSH, 0) f.Number(asm.PUSH, 0)
f.Number(asm.PUSH, 0) f.Number(asm.PUSH, 0)
f.RegisterNumber(asm.SUB, x86.RSP, 32) f.RegisterNumber(asm.SUB, x86.RSP, 32)
f.DLLCall(fmt.Sprintf("%s.%s", fn.Package, fn.Name)) f.DLLCall(fn.UniqueName)
f.RegisterRegister(asm.MOVE, x86.RSP, x86.RBP) f.RegisterRegister(asm.MOVE, x86.RSP, x86.RBP)
f.Register(asm.POP, x86.RBP) f.Register(asm.POP, x86.RBP)

View File

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

View File

@ -6,13 +6,9 @@ import (
) )
// PushNumber pushes a number onto the stack. // PushNumber pushes a number onto the stack.
func PushNumber(code []byte, number int) []byte { func PushNumber(code []byte, number int32) []byte {
length := sizeof.Signed(number) length := sizeof.Signed(number)
if length >= 8 {
panic("x86 does not support pushing 64-bit numbers")
}
if length >= 2 { if length >= 2 {
return append( return append(
code, code,

View File

@ -10,7 +10,7 @@ import (
func TestPushNumber(t *testing.T) { func TestPushNumber(t *testing.T) {
usagePatterns := []struct { usagePatterns := []struct {
Number int Number int32
Code []byte Code []byte
}{ }{
{0, []byte{0x6A, 0x00}}, {0, []byte{0x6A, 0x00}},