Skip to content

perf: eliminate allocations in scanner and formatter#197

Open
wibobm wants to merge 1 commit into
ericlagergren:masterfrom
wibobm:master
Open

perf: eliminate allocations in scanner and formatter#197
wibobm wants to merge 1 commit into
ericlagergren:masterfrom
wibobm:master

Conversation

@wibobm

@wibobm wibobm commented May 14, 2026

Copy link
Copy Markdown
  1. Scanning (SetString):
    • Time: ~15.7% faster.
    • Memory: Allocations were completely eliminated. It went from 32 B/op (1 allocation) down to 0 B/op (0 allocations).
  2. Formatting (String()):
    • Time: ~17.3% faster.
    • Memory: Saved 48 bytes and exactly 1 allocation per operation (from 5 allocs to 4 allocs).
  3. Formatting Large Zeros (fmt.Sprintf("%.100f", &z)):
    • Time: ~6.5% faster.
    • Memory: Saved 48 bytes and 1 allocation per operation.
package decimal_test

import (
	"fmt"
	"testing"

	"github.com/ericlagergren/decimal"
)

func BenchmarkScan(b *testing.B) {
	s := "123456789.987654321E+123"
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		var z decimal.Big
		z.SetString(s)
	}
}

func BenchmarkFormat(b *testing.B) {
	s := "123456789.987654321E+123"
	var z decimal.Big
	z.SetString(s)
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		_ = z.String()
	}
}

func BenchmarkFormatLargeZeros(b *testing.B) {
	s := "1.23"
	var z decimal.Big
	z.SetString(s)
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		_ = fmt.Sprintf("%.100f", &z)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant