Skip to content

yreflect, util, gogen: optimise RangeOrderedMap and Walk node traversal - #1124

Draft
robshakir wants to merge 1 commit into
masterfrom
robjs-optimise-orderedmap-walk
Draft

robshakir wants to merge 1 commit into
masterfrom
robjs-optimise-orderedmap-walk

Conversation

@robshakir

Copy link
Copy Markdown
Member

Summary

This PR reduces reflection overhead and per-node heap allocations during GoOrderedMap iteration and util.ForEachField tree traversal:

  • Cache GoOrderedMap method indices and batch-iterate in yreflect.RangeOrderedMap: Cache reflection method indices (Keys, Values, Get, Append) and key/value types per reflect.Type. Short-circuit when Len() == 0 and iterate over the slices returned by Keys() and Values() in a single pair of reflection calls rather than invoking Get(k) via reflect.Value.Call for every element.
  • Pre-allocate Values() in gogen: Return nil when an ordered map is empty and pre-allocate make([]*T, 0, len(o.keys)) in generated Values() methods.
  • Eliminate walkNodeInfo wrapper allocations in util.Walk: Add NodeInfo() *NodeInfo directly on *util.NodeInfo so WalkNodeFromNodeInfo returns ni without allocating a &walkNodeInfo{} wrapper on every visited node, and guard v.Interface().(goOrderedMap) checks to pointer/interface kinds.
  • Skip child visitor boxing on scalar leaves in forEachFieldMemoVisitor: Detect scalar leaves without children and reuse a shared leafMemo while returning nil from Visit, avoiding per-leaf PathQueryNodeMemo and Visitor interface allocations.

- Cache GoOrderedMap reflection method indices and key/value types per
  reflect.Type in internal/yreflect.
- Update RangeOrderedMap to short-circuit on empty maps and iterate over
  batch Keys() and Values() slices rather than invoking Get(k) via
  reflection for every element.
- Pre-allocate Values() slice capacity in gogen ordered map templates and
  return nil on empty maps.
- Make *util.NodeInfo implement util.WalkNode directly so
  WalkNodeFromNodeInfo returns ni without allocating a walkNodeInfo
  wrapper struct on every visited node.
- Reuse a shared leafMemo and avoid boxing child visitors on scalar leaf
  nodes in forEachFieldMemoVisitor.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 88.667% (-0.1%) from 88.775% — robjs-optimise-orderedmap-walk 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