Source file src/simd/archsimd/_gen/unify/filter.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package unify
     6  
     7  // A valueFilter quickly rules out values that cannot unify with a given value,
     8  // without unifying them.
     9  //
    10  // Unifying a variable with a value unifies each of the variable's values with
    11  // that value. When the variable comes from a large !sum, few of these
    12  // typically succeed, and most fail because of a String field that doesn't
    13  // match. Unifying each value is expensive, since it partitions the environment
    14  // set, but comparing these String fields is cheap. So we perform this small,
    15  // heuristic piece of unification first, and throw out most of the values
    16  // before we do the full unification on the rest.
    17  //
    18  // As a heuristic, the filter only compares the String fields of Defs, since
    19  // that's a very common way for values to differ in practice. It doesn't rule
    20  // out anything else.
    21  type valueFilter struct {
    22  	def Def // The value to filter against
    23  }
    24  
    25  // newValueFilter returns a filter for values that may unify with w, or nil if
    26  // it can't filter anything.
    27  func newValueFilter(w *Value) *valueFilter {
    28  	if debugNoFastPaths {
    29  		return nil
    30  	}
    31  	d, ok := w.Domain.(Def)
    32  	if !ok || len(d.fields) == 0 {
    33  		// We only filter against Defs with fields.
    34  		return nil
    35  	}
    36  	return &valueFilter{def: d}
    37  }
    38  
    39  // mayUnify returns false if v definitely does not unify with the value f
    40  // filters against. If f is nil, it always returns true.
    41  //
    42  // It returns false only if unifying the two Defs would visit a pair of String
    43  // fields that don't match before visiting any other fields that could report an
    44  // error. Hence, if mayUnify returns false, unification would fail without an
    45  // error.
    46  func (f *valueFilter) mayUnify(v *Value) bool {
    47  	if f == nil {
    48  		return true
    49  	}
    50  	d, ok := v.Domain.(Def)
    51  	if !ok {
    52  		return true
    53  	}
    54  	// Visit the keys in the same order as Def.unify.
    55  	for _, key := range f.def.keys {
    56  		dv, ok := d.fields[key]
    57  		if !ok {
    58  			continue
    59  		}
    60  		fv := f.def.fields[key]
    61  		_, dTop := dv.Domain.(Top)
    62  		_, fTop := fv.Domain.(Top)
    63  		if dTop || fTop {
    64  			// Unifying with Top always succeeds.
    65  			continue
    66  		}
    67  		ds, ok1 := dv.Domain.(String)
    68  		fs, ok2 := fv.Domain.(String)
    69  		if !ok1 || !ok2 {
    70  			// Unifying these fields could report an error, which we
    71  			// mustn't hide.
    72  			return true
    73  		}
    74  		if ds.kind == stringRegex && fs.kind == stringRegex {
    75  			// String.unify doesn't check if regexps intersect.
    76  			continue
    77  		}
    78  		// Both fields are Strings, so we can unify just these two fields
    79  		// cheaply, without touching the environment. If they don't match,
    80  		// neither do the Defs.
    81  		if ds.unify(fs) == nil {
    82  			return false
    83  		}
    84  	}
    85  	return true
    86  }
    87  

View as plain text