From c66ffd85527b50ee43aad3b49f55406a1222a130 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Thu, 19 Nov 2026 02:27:26 +0000 Subject: [PATCH 1/3] Speed up loading: fewer allocations and a faster lexer Profiling Load showed half the time in allocation and GC, with per-section key maps the largest single cost, then the copy of the input through a growing strings.Builder and strings.IndexAny on every line. - Sections search their keys until they have more than 8, and only then index them in a map, sized once. - Keys are allocated in chunks of up to 128 per file. - Data sources return the input as a string, without the extra copy for files and readers. - The lexer looks up comment symbols and delimiters in precomputed byte sets and trims ASCII whitespace without the unicode package. - Key.current keeps the missing-key path apart so it still inlines. Load is 18-22% faster with 54-98% fewer allocations, and lookups through Section.Key without locking 40% faster. Add Load benchmarks and refresh the README performance table. --- README.md | 20 ++++---- bench_test.go | 57 +++++++++++++++++++++++ data_source.go | 38 +++++++++------ file.go | 33 ++++++++++--- key.go | 15 ++++-- lexer.go | 122 +++++++++++++++++++++++++++++++++++++------------ options.go | 5 ++ section.go | 54 +++++++++++++++++----- 8 files changed, 267 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 0ae19a6..2025b8e 100644 --- a/README.md +++ b/README.md @@ -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 42× 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. @@ -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 42 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 | 84 ms | 2.0 ms (42× faster) | 40.2k → 0.2k | +| Load 10,000 keys in 1,000 sections | 6.8 ms | 2.1 ms (3.2× faster) | 55.1k → 8.1k | +| Load 10,000 keys, mixed syntax | 6.7 ms | 2.3 ms (3.0× faster) | 54.8k → 8.4k | +| Write 10,000 keys | 1.7–2.7 ms | 3.2–4.0 ms (1.5–1.8× slower) | 17–8.9k → 10.0k–22.8k | +| `MapTo` a 13-field struct | 2.8 µs | 4.9 µs | 50 → 48 | +| `ReflectFrom` the same struct | 2.9 µs | 4.8 µs | 51 → 48 | +| Read one key | 45 ns | 56 ns, or 31 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. +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 diff --git a/bench_test.go b/bench_test.go index 57af0b6..39d4455 100644 --- a/bench_test.go +++ b/bench_test.go @@ -1,6 +1,9 @@ package ini import ( + "fmt" + "os" + "strings" "testing" "github.com/stretchr/testify/require" @@ -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)) +} diff --git a/data_source.go b/data_source.go index 9b1f467..e182915 100644 --- a/data_source.go +++ b/data_source.go @@ -1,10 +1,10 @@ package ini import ( - "bytes" "fmt" "io" "os" + "unsafe" ) var ( @@ -13,9 +13,16 @@ 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) +} + +// ownedString returns b as a string without copying. Nothing may change b +// afterwards. +func ownedString(b []byte) string { + return unsafe.String(unsafe.SliceData(b), len(b)) } // sourceFile represents an object that contains content on the local file system. @@ -23,8 +30,12 @@ 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 ownedString(data), nil } // sourceData represents an object that contains content in memory. @@ -32,30 +43,29 @@ 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 = ownedString(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) { diff --git a/file.go b/file.go index df7777b..eb12f32 100644 --- a/file.go +++ b/file.go @@ -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. @@ -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 @@ -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 } @@ -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 diff --git a/key.go b/key.go index 5eeb854..9ec0fb3 100644 --- a/key.go +++ b/key.go @@ -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 } @@ -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 } diff --git a/lexer.go b/lexer.go index ab4af40..2c86e28 100644 --- a/lexer.go +++ b/lexer.go @@ -3,7 +3,6 @@ package ini import ( "errors" "fmt" - "io" "strconv" "strings" "unicode" @@ -59,10 +58,10 @@ func (l *lexer) next() (token, error) { column := func(s string) int { return utf8.RuneCountInString(raw[:len(raw)-len(s)]) + 1 } if l.opts.AllowNestedValues && l.lastValueEmpty && raw != "" && (raw[0] == ' ' || raw[0] == '\t') { value := strings.TrimSpace(raw) - return token{kind: tokNestedValue, value: value, line: lineNum, column: column(strings.TrimLeftFunc(raw, unicode.IsSpace))}, nil + return token{kind: tokNestedValue, value: value, line: lineNum, column: column(trimLeftSpace(raw))}, nil } - line := strings.TrimLeftFunc(raw, unicode.IsSpace) + line := trimLeftSpace(raw) var tok token var err error switch { @@ -83,7 +82,7 @@ func (l *lexer) next() (token, error) { continue default: var ok bool - if tok, ok, err = l.key(line); !ok && err == nil { + if ok, err = l.key(line, &tok); !ok && err == nil { continue } } @@ -121,7 +120,7 @@ func (l *lexer) unread(line string) { } func (l *lexer) startsWithCommentSymbol(line string) bool { - return startsWithAny(line, l.opts.CommentSymbols) + return l.opts.commentSet.hasPrefix(line) } // startsWithAny reports whether the first character of s is one of chars. @@ -130,6 +129,70 @@ func startsWithAny(s, chars string) bool { return r != utf8.RuneError && strings.ContainsRune(chars, r) } +// charSet is a set of characters, such as the comment symbols, that finds +// them in a string faster than strings.IndexAny. The parser looks for them +// on every line. +type charSet struct { + chars string + ascii bool // chars are all ASCII + bits [4]uint64 // the ASCII characters of chars +} + +func newCharSet(chars string) charSet { + c := charSet{chars: chars, ascii: true} + for i := 0; i < len(chars); i++ { + if b := chars[i]; b < utf8.RuneSelf { + c.bits[b>>6] |= 1 << (b & 63) + } else { + c.ascii = false + } + } + return c +} + +// has reports whether b is an ASCII character in c. +func (c *charSet) has(b byte) bool { + return c.bits[b>>6]&(1<<(b&63)) != 0 +} + +// index is strings.IndexAny(s, c.chars). +func (c *charSet) index(s string) int { + if !c.ascii { + return strings.IndexAny(s, c.chars) + } + for i := 0; i < len(s); i++ { + if c.has(s[i]) { + return i + } + } + return -1 +} + +// hasPrefix is startsWithAny(s, c.chars). +func (c *charSet) hasPrefix(s string) bool { + if s != "" && s[0] < utf8.RuneSelf { + return c.has(s[0]) + } + return !c.ascii && startsWithAny(s, c.chars) +} + +// asciiSpace marks the ASCII characters unicode.IsSpace reports. +var asciiSpace = [utf8.RuneSelf]bool{'\t': true, '\n': true, '\v': true, '\f': true, '\r': true, ' ': true} + +// trimLeftSpace is strings.TrimLeftFunc(s, unicode.IsSpace), faster for +// ASCII. +func trimLeftSpace(s string) string { + for i := 0; i < len(s); i++ { + if s[i] >= utf8.RuneSelf { + return strings.TrimLeftFunc(s[i:], unicode.IsSpace) + } + if !asciiSpace[s[i]] { + return s[i:] + } + } + return "" +} + func (l *lexer) takeComment() string { c := strings.TrimSpace(strings.Join(l.comment, "")) l.comment = l.comment[:0] @@ -149,7 +212,7 @@ func (l *lexer) section(line string) (token, error) { return token{}, fmt.Errorf("unclosed section: %s", line) } name := line[1:closeIdx] - if i := strings.IndexAny(line[closeIdx+1:], l.opts.CommentSymbols); i > -1 { + if i := l.opts.commentSet.index(line[closeIdx+1:]); i > -1 { l.comment = append(l.comment, line[closeIdx+1+i:]) } @@ -165,30 +228,33 @@ func (l *lexer) section(line string) (token, error) { } // key reads a "name = value" line, or a boolean key when the line has no -// delimiter. ok is false for a line that SkipUnrecognizableLines skips. -func (l *lexer) key(line string) (_ token, ok bool, _ error) { - name, offset, err := readKeyName(l.opts.KeyValueDelimiters, line) +// delimiter, into tok. ok is false for a line that SkipUnrecognizableLines +// skips. +func (l *lexer) key(line string, tok *token) (ok bool, _ error) { + name, offset, err := readKeyName(&l.opts.delimiterSet, line) switch { case err == nil: value, err := l.readValue(line[offset:]) if err != nil { - return token{}, false, err + return false, err } l.lastValueEmpty = value == "" - return token{kind: tokKey, name: name, value: value, comment: l.takeComment()}, true, nil + tok.kind, tok.name, tok.value, tok.comment = tokKey, name, value, l.takeComment() + return true, nil case errors.Is(err, ErrDelimiterNotFound) && l.opts.AllowBooleanKeys: // The whole line is the key name, read like a value. name, err := l.readValue(line) if err != nil { - return token{}, false, err + return false, err } - return token{kind: tokBooleanKey, name: name, comment: l.takeComment()}, true, nil + tok.kind, tok.name, tok.comment = tokBooleanKey, name, l.takeComment() + return true, nil case (errors.Is(err, ErrDelimiterNotFound) || errors.Is(err, ErrEmptyKeyName)) && l.opts.SkipUnrecognizableLines: - return token{}, false, nil + return false, nil } - return token{}, false, err + return false, err } // readValue reads a value that starts in "in", the rest of the current line, @@ -198,7 +264,7 @@ func (l *lexer) key(line string) (_ token, ok bool, _ error) { // before surrounding quotes are stripped, so `"a;b" ;c` is `a;b` with comment // `;c`, but `"a;b" x` is `"a` with comment `;b" x`. func (l *lexer) readValue(in string) (string, error) { - line := strings.TrimLeftFunc(in, unicode.IsSpace) + line := trimLeftSpace(in) if line == "" { if l.opts.AllowPythonMultilineValues && strings.HasSuffix(in, "\n") { return l.readPythonMultilines(""), nil @@ -278,7 +344,7 @@ func (l *lexer) readQuoted(line, q string) (string, error) { b.WriteString(next) default: b.WriteString(next[:i]) - if j := strings.IndexAny(next[i:], l.opts.CommentSymbols); j > -1 { + if j := l.opts.commentSet.index(next[i:]); j > -1 { l.comment = append(l.comment, strings.TrimSpace(next[i+j:])) } return b.String(), nil @@ -314,10 +380,10 @@ func (l *lexer) cutInlineComment(line string) string { // with SpaceBeforeInlineComment of the first one after a space, or -1. func (l *lexer) inlineCommentIndex(s string) int { if !l.opts.SpaceBeforeInlineComment { - return strings.IndexAny(s, l.opts.CommentSymbols) + return l.opts.commentSet.index(s) } for i := 0; i < len(s); i++ { - if s[i] == ' ' && startsWithAny(s[i+1:], l.opts.CommentSymbols) { + if s[i] == ' ' && l.opts.commentSet.hasPrefix(s[i+1:]) { return i } } @@ -389,14 +455,10 @@ func skipBOM(src string) string { } } -// parse reads all of reader and adds its sections and keys to f. The caller -// holds the lock, or f isn't shared yet. -func (f *File) parse(reader io.Reader) error { - var b strings.Builder - if _, err := io.Copy(&b, reader); err != nil { - return err - } - l := &lexer{src: skipBOM(b.String()), opts: &f.options} +// parse adds the sections and keys of src to f. The caller holds the lock, +// or f isn't shared yet. +func (f *File) parse(src string) error { + l := &lexer{src: skipBOM(src), opts: &f.options} // Ignore error because default section name is never empty string. section, _ := f.addSection(f.options.DefaultSection) @@ -466,7 +528,7 @@ func (f *File) parse(reader io.Reader) error { // readKeyName returns the key name at the start of line and the offset of // the value after its delimiter. -func readKeyName(delimiters string, line string) (string, int, error) { +func readKeyName(delimiters *charSet, line string) (string, int, error) { // Check if key name surrounded by quotes. var keyQuote string switch line[0] { @@ -493,7 +555,7 @@ func readKeyName(delimiters string, line string) (string, int, error) { pos += startIdx // Find key-value delimiter - i := strings.IndexAny(line[pos+startIdx:], delimiters) + i := delimiters.index(line[pos+startIdx:]) if i < 0 { return "", -1, fmt.Errorf("%w: %s", ErrDelimiterNotFound, line) } @@ -501,7 +563,7 @@ func readKeyName(delimiters string, line string) (string, int, error) { return strings.TrimSpace(line[startIdx:pos]), endIdx + startIdx + 1, nil } - endIdx = strings.IndexAny(line, delimiters) + endIdx = delimiters.index(line) if endIdx < 0 { return "", -1, fmt.Errorf("%w: %s", ErrDelimiterNotFound, line) } diff --git a/options.go b/options.go index 5744e25..643986f 100644 --- a/options.go +++ b/options.go @@ -41,6 +41,10 @@ type loadOptions struct { Locking bool NameMapper NameMapper ValueMapper ValueMapper + + // commentSet and delimiterSet hold CommentSymbols and + // KeyValueDelimiters for the parser. + commentSet, delimiterSet charSet } func newLoadOptions(opts []LoadOption) loadOptions { @@ -54,6 +58,7 @@ func newLoadOptions(opts []LoadOption) loadOptions { for _, opt := range opts { opt(&o) } + o.commentSet, o.delimiterSet = newCharSet(o.CommentSymbols), newCharSet(o.KeyValueDelimiters) return o } diff --git a/section.go b/section.go index 6ee54cd..b2339fa 100644 --- a/section.go +++ b/section.go @@ -12,7 +12,9 @@ type Section struct { f *File name string comment string - // keys are in the order they were added, and byName indexes them by name. + // keys are in the order they were added. byName indexes them by name + // once there are more than maxUnindexedKeys of them; until then, finding + // a key searches keys. keys []*Key byName map[string]*Key @@ -34,10 +36,41 @@ type Section struct { func newSection(f *File, name string) *Section { return &Section{ - f: f, - name: name, - keys: make([]*Key, 0, 10), - byName: make(map[string]*Key), + f: f, + name: name, + keys: make([]*Key, 0, maxUnindexedKeys), + } +} + +// maxUnindexedKeys is the most keys a section has before it indexes them +// in byName. Searching a few keys is faster than hashing, and most sections +// don't need a map. +const maxUnindexedKeys = 8 + +// keyByName returns the key of s named name, or nil. +func (s *Section) keyByName(name string) *Key { + if s.byName != nil { + return s.byName[name] + } + for _, k := range s.keys { + if k.name == name { + return k + } + } + return nil +} + +// appendKey adds k, whose name s has no key of, to s. +func (s *Section) appendKey(k *Key) { + s.keys = append(s.keys, k) + switch { + case s.byName != nil: + s.byName[k.name] = k + case len(s.keys) > maxUnindexedKeys: + s.byName = make(map[string]*Key, len(s.keys)) + for _, k := range s.keys { + s.byName[k.name] = k + } } } @@ -126,7 +159,7 @@ func (s *Section) addKey(name, val string) (*Key, error) { name = strings.ToLower(name) } - if key := s.byName[name]; key != nil { + if key := s.keyByName(name); key != nil { if s.f.options.AllowShadows { if err := key.addShadow(val); err != nil { return nil, err @@ -137,9 +170,8 @@ func (s *Section) addKey(name, val string) (*Key, error) { return key, nil } - key := newKey(s, name, val) - s.keys = append(s.keys, key) - s.byName[name] = key + key := s.f.newKey(s, name, val) + s.appendKey(key) return key, nil } @@ -173,7 +205,7 @@ func (s *Section) GetKey(name string) (*Key, error) { if s.f.options.Insensitive || s.f.options.InsensitiveKeys { name = strings.ToLower(name) } - key := s.current().byName[name] + key := s.current().keyByName(name) if s.f.locking { s.f.mu.RUnlock() } @@ -304,7 +336,7 @@ func (s *Section) DeleteKey(name string) { } s = s.current() - if key := s.byName[name]; key != nil { + if key := s.keyByName(name); key != nil { delete(s.byName, name) s.keys = slices.DeleteFunc(s.keys, func(k *Key) bool { return k == key }) } From 15d272b4d511cbfbcf676073af20f92c478c5407 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Thu, 19 Nov 2026 02:50:03 +0000 Subject: [PATCH 2/3] Read sources without unsafe --- data_source.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/data_source.go b/data_source.go index e182915..c03e048 100644 --- a/data_source.go +++ b/data_source.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "os" - "unsafe" ) var ( @@ -19,12 +18,6 @@ type dataSource interface { read() (string, error) } -// ownedString returns b as a string without copying. Nothing may change b -// afterwards. -func ownedString(b []byte) string { - return unsafe.String(unsafe.SliceData(b), len(b)) -} - // sourceFile represents an object that contains content on the local file system. type sourceFile struct { name string @@ -35,7 +28,7 @@ func (s sourceFile) read() (string, error) { if err != nil { return "", err } - return ownedString(data), nil + return string(data), nil } // sourceData represents an object that contains content in memory. @@ -59,7 +52,7 @@ type sourceReadCloser struct { func (s *sourceReadCloser) read() (string, error) { if s.reader != nil { data, err := io.ReadAll(s.reader) - s.data, s.err = ownedString(data), err + s.data, s.err = string(data), err if err := s.reader.Close(); s.err == nil { s.err = err } From 03313664d849c32200cdae1017a8ed0897adfce1 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Thu, 19 Nov 2026 02:59:56 +0000 Subject: [PATCH 3/3] README: measure loads from files --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2025b8e..c9160c7 100644 --- a/README.md +++ b/README.md @@ -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 42× 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. @@ -60,23 +60,23 @@ Options change the syntax that is accepted, such as `ini.Load("app.ini", ini.All ## Performance -v2 loads files 3 to 42 times faster than v1, with a seventh to a two-hundredth 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 | 84 ms | 2.0 ms (42× faster) | 40.2k → 0.2k | -| Load 10,000 keys in 1,000 sections | 6.8 ms | 2.1 ms (3.2× faster) | 55.1k → 8.1k | -| Load 10,000 keys, mixed syntax | 6.7 ms | 2.3 ms (3.0× faster) | 54.8k → 8.4k | -| Write 10,000 keys | 1.7–2.7 ms | 3.2–4.0 ms (1.5–1.8× slower) | 17–8.9k → 10.0k–22.8k | -| `MapTo` a 13-field struct | 2.8 µs | 4.9 µs | 50 → 48 | -| `ReflectFrom` the same struct | 2.9 µs | 4.8 µs | 51 → 48 | -| Read one key | 45 ns | 56 ns, or 31 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`. -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. +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