Replace all string building operations in the lexer with strings.Builder. Doing so shows significant performance improvements. BurntSushi still has a slight edge in CPU performance, but there's still much work to do on memory performance. name old time/op new time/op delta ParseToml-2 311µs ± 0% 273µs ± 3% -12.29% (p=0.008 n=5+5) UnmarshalToml-2 386µs ± 4% 349µs ± 3% -9.63% (p=0.008 n=5+5) UnmarshalBurntSushiToml-2 368µs ± 8% 341µs ± 2% ~ (p=0.056 n=5+5) name old alloc/op new alloc/op delta ParseToml-2 132kB ± 0% 118kB ± 0% -11.07% (p=0.008 n=5+5) UnmarshalToml-2 147kB ± 0% 133kB ± 0% -9.92% (p=0.008 n=5+5) UnmarshalBurntSushiToml-2 82.6kB ± 0% 82.6kB ± 0% ~ (p=1.000 n=5+5) name old allocs/op new allocs/op delta ParseToml-2 3.19k ± 0% 1.91k ± 0% -40.19% (p=0.008 n=5+5) UnmarshalToml-2 4.03k ± 0% 2.75k ± 0% -31.83% (p=0.008 n=5+5) UnmarshalBurntSushiToml-2 1.73k ± 0% 1.73k ± 0% ~ (all equal) Out of curiosity, I benchmarked the results of updating each function along the way to see how each change effected the overall performance: name \ time/op master lexKey lexLitStringAsString lexStringAsString ParseToml-2 311µs ± 0% 299µs ± 1% 290µs ± 3% 273µs ± 3% UnmarshalToml-2 386µs ± 4% 381µs ± 2% 364µs ± 2% 349µs ± 3% UnmarshalBurntSushiToml-2 368µs ± 8% 341µs ± 2% 345µs ± 5% 341µs ± 2% name \ alloc/op master lexKey lexLitStringAsString lexStringAsString ParseToml-2 132kB ± 0% 132kB ± 0% 125kB ± 0% 118kB ± 0% UnmarshalToml-2 147kB ± 0% 146kB ± 0% 140kB ± 0% 133kB ± 0% UnmarshalBurntSushiToml-2 82.6kB ± 0% 82.6kB ± 0% 82.6kB ± 0% 82.6kB ± 0% name \ allocs/op master lexKey lexLitStringAsString lexStringAsString ParseToml-2 3.19k ± 0% 2.86k ± 0% 2.49k ± 0% 1.91k ± 0% UnmarshalToml-2 4.03k ± 0% 3.70k ± 0% 3.33k ± 0% 2.75k ± 0% UnmarshalBurntSushiToml-2 1.73k ± 0% 1.73k ± 0% 1.73k ± 0% 1.73k ± 0% Benchmarks were run from the benchmark/ directory using: go test -bench=.*Toml -benchmem -count=5 ./...
go-toml
Go library for the TOML format.
This library supports TOML version v1.0.0-rc.1
Features
Go-toml provides the following features for using data parsed from TOML documents:
- Load TOML documents from files and string data
- Easily navigate TOML structure using Tree
- Marshaling and unmarshaling to and from data structures
- Line & column position data for all parsed elements
- Query support similar to JSON-Path
- Syntax errors contain line and column numbers
Import
import "github.com/pelletier/go-toml"
Usage example
Read a TOML document:
config, _ := toml.Load(`
[postgres]
user = "pelletier"
password = "mypassword"`)
// retrieve data directly
user := config.Get("postgres.user").(string)
// or using an intermediate object
postgresConfig := config.Get("postgres").(*toml.Tree)
password := postgresConfig.Get("password").(string)
Or use Unmarshal:
type Postgres struct {
User string
Password string
}
type Config struct {
Postgres Postgres
}
doc := []byte(`
[Postgres]
User = "pelletier"
Password = "mypassword"`)
config := Config{}
toml.Unmarshal(doc, &config)
fmt.Println("user=", config.Postgres.User)
Or use a query:
// use a query to gather elements without walking the tree
q, _ := query.Compile("$..[user,password]")
results := q.Execute(config)
for ii, item := range results.Values() {
fmt.Printf("Query result %d: %v\n", ii, item)
}
Documentation
The documentation and additional examples are available at godoc.org.
Tools
Go-toml provides two handy command line tools:
-
tomll: Reads TOML files and lints them.go install github.com/pelletier/go-toml/cmd/tomll tomll --help -
tomljson: Reads a TOML file and outputs its JSON representation.go install github.com/pelletier/go-toml/cmd/tomljson tomljson --help -
jsontoml: Reads a JSON file and outputs a TOML representation.go install github.com/pelletier/go-toml/cmd/jsontoml jsontoml --help
Docker image
Those tools are also availble as a Docker image from
dockerhub. For example, to
use tomljson:
docker run -v $PWD:/workdir pelletier/go-toml tomljson /workdir/example.toml
Only master (latest) and tagged versions are published to dockerhub. You
can build your own image as usual:
docker build -t go-toml .
Contribute
Feel free to report bugs and patches using GitHub's pull requests system on pelletier/go-toml. Any feedback would be much appreciated!
Run tests
go test ./...
Fuzzing
The script ./fuzz.sh is available to
run go-fuzz on go-toml.
Versioning
Go-toml follows Semantic Versioning. The supported version of TOML is indicated at the beginning of this document. The last two major versions of Go are supported (see Go Release Policy).
License
The MIT License (MIT). Read LICENSE.