From 4d61f1fac228d74912b70147362c5daa4c1d4936 Mon Sep 17 00:00:00 2001 From: Dmitri Aleksandrov Date: Mon, 19 Oct 2026 11:06:59 +0300 Subject: [PATCH 1/2] perf: read the fields by the offsets of fmap without reflect The getters of the fields called fmap.GetPtr, it builds the pointer by reflect.NewAt on every read. The address is computed by the offsets of fmap once per rule set: the offset from the root struct or from the nearest ancestor stored by pointer, the pointers of these ancestors are followed, a nil one is an absent value. A valid object: 463 ns to 407 ns in the bench module, 0 allocations. --- README.md | 2 +- docs/performance.md | 4 ++-- shared/field.go | 34 ++++++++++++++++++++++++++++++++-- shared/slice.go | 3 ++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fd0f0f9..f9797b2 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ The same struct and rules, Intel Core i9-14900HX, Go 1.27.0, see [Performance](h | | valid object | invalid object, 6 errors | |---|---|---| | hand-written code | 286 ns, 0 allocs | 590 ns, without messages | -| **valigo** | **458 ns, 0 allocs** | **2 205 ns, 14 allocs, translated messages** | +| **valigo** | **407 ns, 0 allocs** | **2 205 ns, 14 allocs, translated messages** | | go-playground/validator | 1 785 ns, 16 allocs | 4 150 ns, 69 allocs, with `Error()` | | ozzo-validation | 2 950 ns, 38 allocs | 12 665 ns, 140 allocs, with `Error()` | diff --git a/docs/performance.md b/docs/performance.md index e792de9..8b91a4f 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -19,7 +19,7 @@ A valid object: | Library | ns/op | B/op | allocs/op | |---|---|---|---| | hand-written code, the lower bound | 286 | 0 | 0 | -| valigo | **458** | **0** | **0** | +| valigo | **407** | **0** | **0** | | zog | 1 690 | 6 | 2 | | go-playground/validator | 1 785 | 296 | 16 | | ozzo-validation | 2 950 | 2 000 | 38 | @@ -37,7 +37,7 @@ An invalid object with 6 errors: | ozzo-validation, with `Error()` | 12 665 | 11 026 | 140 | | gookit/validate | 13 250 | 9 569 | 120 | -valigo is about 1.6 times slower than the same rules written by hand on a valid object, it pays for the closures of the rules and the generic reading of the fields; it doesn't allocate either. Code generators of validators produce code like the hand-written one, so they are expected to be about as fast as it. +valigo is about 1.4 times slower than the same rules written by hand on a valid object, it pays for the closures of the rules; it doesn't allocate either. The fields are read by the offsets of fmap without reflect, following the pointers of nested structs. Code generators of validators produce code like the hand-written one, so they are expected to be about as fast as it. - valigo builds the rules once in `Configure` and reads the fields by precomputed accessors, so valid objects cost no allocations; go-playground/validator parses the tags once too, but works through `reflect` on every call, ozzo-validation builds its rules on every call of `Validate`. - gookit/validate creates a validation per call, it is its API, and reports the errors of the elements of a slice under one key `Tags.*`; zog builds the messages of its issues. diff --git a/shared/field.go b/shared/field.go index 38ff414..d5b0d0b 100644 --- a/shared/field.go +++ b/shared/field.go @@ -173,9 +173,10 @@ func valueGetter[T any](field fmap.Field) func(obj any) *T { t = t.Elem() } mustLayout[T](field, t) + addr := fieldAddr(field) if isPtr { return func(obj any) *T { - p := (**T)(dataPtr(field.GetPtr(obj))) + p := (**T)(addr(dataPtr(obj))) if p == nil { return nil } @@ -183,7 +184,36 @@ func valueGetter[T any](field fmap.Field) func(obj any) *T { } } return func(obj any) *T { - return (*T)(dataPtr(field.GetPtr(obj))) + return (*T)(addr(dataPtr(obj))) + } +} + +// fieldAddr returns a function that computes the address of the field in the struct at root by the offsets of fmap, +// without reflect: the offset of a field is from the root struct or from the struct of its nearest ancestor stored +// by pointer, so the address follows the pointers of these ancestors. It returns nil if one of them is nil. +func fieldAddr(field fmap.Field) func(root unsafe.Pointer) unsafe.Pointer { + // the offsets of the ancestors stored by pointer, from the root down to the field + var owners []uintptr + for parent := field.GetParent(); parent != nil; parent = parent.GetParent() { + if t := parent.GetType(); t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct { + owners = append([]uintptr{parent.GetOffset()}, owners...) + } + } + offset := field.GetOffset() + if len(owners) == 0 { + return func(root unsafe.Pointer) unsafe.Pointer { + return unsafe.Add(root, offset) + } + } + return func(root unsafe.Pointer) unsafe.Pointer { + base := root + for _, owner := range owners { + base = *(*unsafe.Pointer)(unsafe.Add(base, owner)) + if base == nil { + return nil + } + } + return unsafe.Add(base, offset) } } diff --git a/shared/slice.go b/shared/slice.go index ee55b3d..ba77b07 100644 --- a/shared/slice.go +++ b/shared/slice.go @@ -67,8 +67,9 @@ func elemsGetter[E any](field fmap.Field) func(obj any) (Elems[E], bool) { elem = elem.Elem() } mustLayout[E](field, elem) + addr := fieldAddr(field) return func(obj any) (Elems[E], bool) { - p := dataPtr(field.GetPtr(obj)) + p := addr(dataPtr(obj)) if p != nil && slicePtr { p = *(*unsafe.Pointer)(p) } From f6b39871957bbcf8b2ac2078140e1cdb7040e302 Mon Sep 17 00:00:00 2001 From: Dmitri Aleksandrov Date: Mon, 19 Oct 2026 11:08:37 +0300 Subject: [PATCH 2/2] docs: the gap to hand-written code is 1.4x in Why valigo? --- docs/why-valigo.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/why-valigo.md b/docs/why-valigo.md index a8daba2..8d12bb3 100644 --- a/docs/why-valigo.md +++ b/docs/why-valigo.md @@ -23,7 +23,7 @@ The configurator knows nothing about strings, numbers or uuids: they are convers ### Validation of valid objects doesn't allocate -The rules are built once by `Configure`. `Validate` reads the fields through precomputed accessors and calls the rules of every field, allocations happen only to build errors. A struct with string, number, uuid and slice rules, conditions and a struct level custom rule is validated with 0 allocations — the tests check it. On the same rules a valid object is validated about 4 times faster than by go-playground/validator and 1.6 times slower than by the same rules written by hand, an invalid one with translated messages — about 2 times faster than go-playground/validator builds its messages, see [Performance](performance.md). +The rules are built once by `Configure`. `Validate` reads the fields through precomputed accessors and calls the rules of every field, allocations happen only to build errors. A struct with string, number, uuid and slice rules, conditions and a struct level custom rule is validated with 0 allocations — the tests check it. On the same rules a valid object is validated about 4 times faster than by go-playground/validator and 1.4 times slower than by the same rules written by hand, an invalid one with translated messages — about 2 times faster than go-playground/validator builds its messages, see [Performance](performance.md). ### Absent values are handled, not panicked on