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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()` |

Expand Down
4 changes: 2 additions & 2 deletions docs/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/why-valigo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 32 additions & 2 deletions shared/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,47 @@ 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
}
return *p
}
}
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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion shared/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading