Skip to content

ytypes, util: optimise leafref node matching and scalar equality - #1123

Draft
robshakir wants to merge 1 commit into
masterfrom
robjs-optimise-leafref-matching
Draft

robshakir wants to merge 1 commit into
masterfrom
robjs-optimise-leafref-matching

Conversation

@robshakir

Copy link
Copy Markdown
Member

Summary

This PR reduces CPU and heap allocations during ytypes.Validate leafref target matching and reflection value checks:

  • Guard debug formatting in ytypes/leafref.go: Check util.IsDebugLibraryEnabled() before evaluating util.ValueStrDebug arguments and defer util.StripModulePrefixesStr(schema.Type.Path) to debug or error paths.
  • Zero-allocation util.IsNilOrInvalidValue: Inspect v.Kind() (and v.Elem().Kind() for interfaces) directly instead of calling IsValueNil(v.Interface()), eliminating heap allocations (packEface) when checking non-pointer structs, enums, and scalar values during tree walks.
  • Fast-path scalar comparison in util.DeepEqualDerefPtrs: Add a type-switch fast path for pointers to primitive scalars (*string, *uint*, *int*, *bool, *float64) and use reflect.Value.Equal for comparable values before falling back to reflect.DeepEqual.
  • Indexed string lookup in util.PathQueryResult: Populate StringSet when a path query returns >= 4 string or *string targets, reducing string leafref validation in matchesNodesResult from O(N * M) reflection scans to O(1) map lookups.

- Guard util.DbgPrint calls in ValidateLeafRefData, dataNodesResultAtPath,
  and matchSingleNode with util.IsDebugLibraryEnabled(), and defer
  util.StripModulePrefixesStr(schema.Type.Path) to debug/error paths.
- Update util.IsNilOrInvalidValue(v) to inspect v.Kind() directly rather
  than calling IsValueNil(v.Interface()), avoiding heap-boxing of structs,
  enums, and scalar values on every check.
- Add zero-allocation type-switch fast paths in util.DeepEqualDerefPtrs for
  pointers to primitive scalars and reflect.Value.Equal for comparable
  values before falling back to reflect.DeepEqual.
- Index dereferenced string values into PathQueryResult.StringSet when
  querying >= 4 string target nodes so matchesNodesResult performs O(1)
  zero-allocation map lookups.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 88.372% (-0.4%) from 88.775% — robjs-optimise-leafref-matching into master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants