Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Package ini reads and writes INI files in Go.

## Features

- **Fast loading**: a single-pass lexer, linear in the size of the input and up to 31× faster than v1.
- **Fast loading**: a single-pass lexer, linear in the size of the input and up to 37× faster than v1.
- **Several sources**: files, `[]byte` and `io.Reader`, with later sources overriding earlier ones.
- **Rich INI syntax**: parent-child sections, auto-increment keys, repeated (shadow) keys, nested and multiline values, and `%(name)s` references.
- **Typed values**: numbers, booleans, durations and times, with generic lists and candidates of any of them, and ranges of all but booleans.
Expand Down Expand Up @@ -60,23 +60,23 @@ Options change the syntax that is accepted, such as `ini.Load("app.ini", ini.All

## Performance

v2 loads files up to 31 times faster than v1, with a third to a fifth of the allocations:
v2 loads files 3 to 37 times faster than v1, with a seventh to a two-hundredth of the allocations:

| Operation | v1.67.3 | v2 | Allocations, v1 → v2 |
|---|---|---|---|
| Load 10,000 keys in one section | 137 ms | 4.4 ms (31× faster) | 50.2k → 10.1k |
| Load 10,000 keys in 1,000 sections | 8.2 ms | 3.2 ms (2.5× faster) | 58.1k → 18.1k |
| Load 10,000 keys, mixed syntax | 101 ms | 3.9 ms (26× faster) | 57.6k → 18.8k |
| Write 10,000 keys | 2.2–4.3 ms | 4.3–7.4 ms (1.7–2.0× slower) | 3.0k–20.0k → 13.0k–30.0k |
| `MapTo` a 13-field struct | 3.8 µs | 4.5 µs | 44 → 48 |
| `ReflectFrom` the same struct | 13.9 µs | 11.8 µs | 132 → 118 |
| Read one key | 60 ns | 71 ns, or 36 ns with `Locking(false)` | 0 → 0 |
| Load 10,000 keys in one section | 77 ms | 2.1 ms (37× faster) | 40.2k → 0.2k |
| Load 10,000 keys in 1,000 sections | 6.6 ms | 2.2 ms (3.0× faster) | 55.1k → 8.1k |
| Load 10,000 keys, mixed syntax | 6.7 ms | 2.3 ms (2.9× faster) | 54.8k → 8.4k |
| Write 10,000 keys | 1.7–2.7 ms | 3.1–4.1 ms (1.5–1.8× slower) | 17–8.9k → 10.0k–22.8k |
| `MapTo` a 13-field struct | 2.7 µs | 4.7 µs | 50 → 48 |
| `ReflectFrom` the same struct | 2.8 µs | 4.8 µs | 51 → 48 |
| Read one key | 44 ns | 58 ns, or 32 ns with `Locking(false)` | 0 → 0 |

- **Load** is linear in the number of keys. v1 compared each new key with every key before it in its section, so a large section took quadratic time.
- **Write** is slower because it checks that every line it writes reads back as the same value. v1 could write files that loaded differently.
- **Reads** take the file's read lock so that they are safe while other goroutines change the file. v1 read values without it, as in `Key.String`.

Measured with Go 1.26.10 on a 4-core Intel Xeon at 2.60 GHz, as the median of 8 runs.
Loads read the file from disk. The mixed input is 100 sections of 100 keys with comments, inline comments, quoted values, colons, auto-increment keys and continuation lines. Measured with Go 1.27.4 on an Intel Xeon at 2.60 GHz with `GOMAXPROCS=4`, as the median of 8 runs.

## Migrating from v1

Expand Down
57 changes: 57 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ini

import (
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -116,3 +119,57 @@ func Benchmark_Key_Strings_LargeArray(b *testing.B) {
_ = key.Strings(",")
}
}

// benchConfig is a typical large config: sections with comments, plain,
// quoted and commented values.
var benchConfig = func() []byte {
var b strings.Builder
b.WriteString("; global settings\nAPP_NAME = bench\nRUN_MODE = prod\n\n")
for i := range 500 {
fmt.Fprintf(&b, "# Section %d\n[section_%d]\n", i, i)
for j := range 20 {
switch j % 4 {
case 0:
fmt.Fprintf(&b, "key_%d = value_%d_%d\n", j, i, j)
case 1:
fmt.Fprintf(&b, "; comment for key %d\nkey_%d = %d ; inline comment\n", j, j, i*j)
case 2:
fmt.Fprintf(&b, "key_%d = \"quoted value %d\"\n", j, j)
case 3:
fmt.Fprintf(&b, " key_%d = https://example.com/path/%d?x=%d \n", j, i, j)
}
}
b.WriteString("\n")
}
return []byte(b.String())
}()

func benchmarkLoad(b *testing.B, data []byte, opts ...LoadOption) {
b.SetBytes(int64(len(data)))
b.ReportAllocs()
for b.Loop() {
if _, err := Load(data, opts...); err != nil {
b.Fatal(err)
}
}
}

func Benchmark_Load(b *testing.B) {
benchmarkLoad(b, benchConfig)
}

func Benchmark_Load_Insensitive(b *testing.B) {
benchmarkLoad(b, benchConfig, CaseInsensitive(true))
}

func Benchmark_Load_Full(b *testing.B) {
data, err := os.ReadFile("testdata/full.ini")
require.NoError(b, err)
benchmarkLoad(b, data, AllowBooleanKeys(true))
}

func Benchmark_Load_Multiline(b *testing.B) {
data, err := os.ReadFile("testdata/multiline.ini")
require.NoError(b, err)
benchmarkLoad(b, data, AllowPythonMultilineValues(true))
}
31 changes: 17 additions & 14 deletions data_source.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ini

import (
"bytes"
"fmt"
"io"
"os"
Expand All @@ -13,49 +12,53 @@ var (
_ dataSource = (*sourceReadCloser)(nil)
)

// dataSource is an interface that returns object which can be read and closed.
// dataSource is a source of INI input.
type dataSource interface {
ReadCloser() (io.ReadCloser, error)
// read returns the whole input.
read() (string, error)
}

// sourceFile represents an object that contains content on the local file system.
type sourceFile struct {
name string
}

func (s sourceFile) ReadCloser() (_ io.ReadCloser, err error) {
return os.Open(s.name)
func (s sourceFile) read() (string, error) {
data, err := os.ReadFile(s.name)
if err != nil {
return "", err
}
return string(data), nil
}

// sourceData represents an object that contains content in memory.
type sourceData struct {
data []byte
}

func (s *sourceData) ReadCloser() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(s.data)), nil
func (s *sourceData) read() (string, error) {
// Copy, as the caller may change the slice later.
return string(s.data), nil
}

// sourceReadCloser represents an input stream with Close method. A stream
// can be read only once, so its data is kept for reloading.
type sourceReadCloser struct {
reader io.ReadCloser
data []byte
data string
err error
}

func (s *sourceReadCloser) ReadCloser() (io.ReadCloser, error) {
func (s *sourceReadCloser) read() (string, error) {
if s.reader != nil {
s.data, s.err = io.ReadAll(s.reader)
data, err := io.ReadAll(s.reader)
s.data, s.err = string(data), err
if err := s.reader.Close(); s.err == nil {
s.err = err
}
s.reader = nil
}
if s.err != nil {
return nil, s.err
}
return io.NopCloser(bytes.NewReader(s.data)), nil
return s.data, s.err
}

func newDataSource(source any) (dataSource, error) {
Expand Down
33 changes: 26 additions & 7 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ type File struct {
// them, in the same order, by name.
sections []*Section
byName map[string][]*Section

// keySlab holds the unused keys of the last chunk newKey allocated, so
// a file with many keys doesn't allocate each one. keyChunk is the size
// of that chunk.
keySlab []Key
keyChunk int
}

// newKey returns a new key of section s, which belongs to f.
func (f *File) newKey(s *Section, name, val string) *Key {
if len(f.keySlab) == 0 {
f.keyChunk = min(max(2*f.keyChunk, 8), 128)
f.keySlab = make([]Key, f.keyChunk)
}
k := &f.keySlab[0]
f.keySlab = f.keySlab[1:]
k.s, k.name, k.value = s, name, val
return k
}

// newFile initializes File object with given data sources.
Expand Down Expand Up @@ -387,13 +405,11 @@ func (f *File) DeleteSectionWithIndex(name string, index int) error {

// loadSource parses one data source into f.
func (f *File) loadSource(s dataSource) error {
r, err := s.ReadCloser()
src, err := s.read()
if err != nil {
return err
}
defer r.Close()

return f.parse(r)
return f.parse(src)
}

// Reload reads all data sources again and replaces the contents of the file
Expand Down Expand Up @@ -424,11 +440,14 @@ func (f *File) Reload() error {
// the lock.
o := old[i]
for j, k := range sec.keys {
if ok := o.byName[k.name]; ok != nil {
if ok := o.keyByName(k.name); ok != nil {
ok.comment, ok.value, ok.shadows = k.comment, k.value, k.shadows
ok.isAutoIncrement, ok.isBooleanType = k.isAutoIncrement, k.isBooleanType
ok.nestedValues = k.nestedValues
sec.keys[j], sec.byName[k.name] = ok, ok
sec.keys[j] = ok
if sec.byName != nil {
sec.byName[k.name] = ok
}
} else {
k.s = o
}
Expand All @@ -455,7 +474,7 @@ func (f *File) load(sources []dataSource) error {
if err := f.loadSource(s); err != nil {
// In loose mode, we create an empty default section for nonexistent files.
if os.IsNotExist(err) && f.options.Loose {
_ = f.parse(bytes.NewBuffer(nil))
_ = f.parse("")
continue
}
return err
Expand Down
15 changes: 10 additions & 5 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ func newKey(s *Section, name, val string) *Key {
// holds the lock.
func (k *Key) current() *Key {
if k.missing {
if key := k.s.current().byName[k.name]; key != nil {
return key
}
return k.currentMissing()
}
return k
}

// currentMissing is current for a missing k. It's apart so current inlines.
func (k *Key) currentMissing() *Key {
if key := k.s.current().keyByName(k.name); key != nil {
return key
}
return k
}
Expand All @@ -62,8 +68,7 @@ func (k *Key) attach() *Key {
if c == k && k.missing {
k.missing = false
sec := k.s.attach()
sec.keys = append(sec.keys, k)
sec.byName[k.name] = k
sec.appendKey(k)
}
return c
}
Expand Down
Loading
Loading