Skip to content

Compare predicate collections by structure - #1029

Open
antonmedv wants to merge 6 commits into
masterfrom
predicate-combination-compare
Open

antonmedv wants to merge 6 commits into
masterfrom
predicate-combination-compare

Conversation

@antonmedv

Copy link
Copy Markdown
Member

The optimizer merges all/any/none calls on the same collection, such as all(x, p) && all(x, q). It decided two collections were the same by comparing their String() output, and some different trees still print the same after #1026:

  • BuiltinNode.Map, set by the filterMap pass, is not printed, so map(filter(a, p), f) prints as filter(a, p).
  • A folded (-2) left of ** prints as -2 ** n, the same as -(2 ** n).
  • Folded constants print 2.0 as 2, and NaN like an identifier.

Each of these gave a different result with the optimizer on than with it off. This compares the two collection trees field by field instead. Constants are compared by type and value, floats by bits so that 0.0 and -0.0 stay apart, and anything it can't compare counts as different.

predicateCombination matched collections by String(), which prints some different trees the same: a BuiltinNode's Map field, a folded (-2) left of **, and folded float or NaN constants. Compare the trees field by field instead.
Compare only a ConstantNode's value, not its location and type, so identical constant collections still merge. Bound the depth of the constant walk so a self-referential value cannot overflow the stack.
Remember visited pointer pairs, like reflect.DeepEqual, so constants that reuse a value in many places or refer to themselves compare in linear time. Match map keys that == does not compare exactly, such as 0.0 and -0.0, by searching.
Drop the visited set: a failed map key search left wrong entries in it, and it missed values shared through arrays. Constants now compare only when they hold plain data with exactly comparable map keys, and the whole comparison stops after a fixed number of steps. Anything else counts as different, which only skips the merge.
Treat the very same pointer, map or slice in two constants as the same value, so a cached ConstExpr result like []*User still merges. Build the large-constant test values twice so the comparison walks them.

This branch has not been deployed

No deployments
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.

1 participant