// Plan-flip detection for the cache: a template planned blind (constants// lifted to parameters) can choose a structurally different operator tree// than value-sighted planning would -- exact scan cardinalities, resolved// anchor degrees, and rewrite gates priced from those exacts all go blind// under auto-parameterization, and a flipped plan can be arbitrarily worse// (the census measured one 17.5x allocation regression). The cache detects// the flip once, when a template is first planned, by also planning the// literal text and comparing the trees value-blind; a flipped template is// marked so execution routes through sighted planning instead of the// cached tree. Non-flipped templates -- the overwhelming majority -- keep// the full cache win.
package gqlimport ()var ( blindStrLit = regexp.MustCompile(`'(?:[^'\\]|\\.)*'`) blindParam = regexp.MustCompile(`\$\w+`) blindNumber = regexp.MustCompile(`\b\d+(?:\.\d+)?\b`))// valueBlind normalizes a canonical plan rendering so a literal and its// lifted parameter compare equal: strings, params, and numbers all become// `?`, and the [anchor: ...] provenance note drops entirely -- it records// WHICH SIGNAL decided the anchor (exact vs average), which legitimately// differs between sighted and blind planning even when the chosen// operator tree is identical. Both sides normalize identically, so// structure alone decides.func valueBlind( []string) string { := [:0]for , := range {ifstrings.HasPrefix(strings.TrimSpace(), "[anchor:") {continue } = append(, ) } = := strings.Join(, "\n") = blindStrLit.ReplaceAllString(, "?") = blindParam.ReplaceAllString(, "?") = blindNumber.ReplaceAllString(, "?")return}// planFlipped reports whether the template plan the cache would execute// (post adaptive anchor choice for this call's values) differs// structurally from the plan the literal query text produces.func planFlipped( string, *plan.Plan, *graph.SnapshotGraph) bool { , := parseDesugar()if != nil {returnfalse } , := plan.Build(, )if != nil {returnfalse }returnvalueBlind(explain.Canonical(, plan.Estimate(, ))) !=valueBlind(explain.Canonical(, plan.Estimate(, )))}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.