Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use toml.Unmarshal instead of toml.Decode #1122

Merged
merged 1 commit into from
Nov 16, 2024

Conversation

alexandear
Copy link
Contributor

@alexandear alexandear commented Nov 14, 2024

The PR refactors parseConfig implementation to use toml.Unmarshal instead of toml.Decode. The first accepts the data as []byte, the second as string. This removes one conversion between from []byte to string.

Benchmark says that this refactoring saves one allocation without increasing parseConfig complexity.

Benchmarks

func BenchmarkParseConfig(b *testing.B) {
	var config lint.Config

	configPath := filepath.Join("..", "full.toml")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := parseConfig(configPath, &config)
		if err != nil {
			b.Fatalf("failed to parse config: %v", err)
		}
	}
}

func BenchmarkParseConfigString(b *testing.B) {
	var config lint.Config

	configPath := filepath.Join("..", "full.toml")

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err := parseConfigString(configPath, &config)
		if err != nil {
			b.Fatalf("failed to parse config: %v", err)
		}
	}
}
❯ go test -benchmem -bench=. ./...
goos: darwin
goarch: arm64
pkg: github.com/mgechev/revive/config
cpu: Apple M1 Pro
BenchmarkParseConfig-8              5030            213835 ns/op          158632 B/op       1789 allocs/op
BenchmarkParseConfigString-8        5779            210645 ns/op          161789 B/op       1790 allocs/op
PASS
ok      github.com/mgechev/revive/config        2.752s

@chavacava chavacava merged commit 1b4440c into mgechev:master Nov 16, 2024
8 checks passed
@alexandear alexandear deleted the refactor/parse-config branch November 16, 2024 15:22
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.

2 participants