Repository: prometheus/prometheus
File: promql/engine.go (Go)

Code before:
	type seriesAndTimestamp struct {
		Series
		ts int64
	}
	seriess := make(map[uint64]seriesAndTimestamp, biggestLen) // Output series by series hash.
	tempNumSamples := ev.currentSamples

	var (
		seriesHelpers [][]EvalSeriesHelper
		bufHelpers    [][]EvalSeriesHelper // Buffer updated on each step
	)

	// If the series preparation function is provided, we should run it for
	// every single series in the matrix.
	if prepSeries != nil {
		seriesHelpers = make([][]EvalSeriesHelper, len(exprs))
		bufHelpers = make([][]EvalSeriesHelper, len(exprs))

		seenStrSigs := make(map[string]int)

		for i := range exprs {
			seriesHelpers[i] = make([]EvalSeriesHelper, len(matrixes[i]))
			bufHelpers[i] = make([]EvalSeriesHelper, len(matrixes[i]))

			for si, series := range matrixes[i] {
				strSig := prepSeries(series.Metric)

				if seenAt, ok := seenStrSigs[strSig]; ok {
					seriesHelpers[i][si] = EvalSeriesHelper{signature: seenAt}
					continue
				}

				seenStrSigs[strSig] = enh.numSigs
				seriesHelpers[i][si] = EvalSeriesHelper{signature: enh.numSigs}
				enh.numSigs++
			}
		}
	}

	for ts := ev.startTimestamp; ts <= ev.endTimestamp; ts += ev.interval {
		if err := contextDone(ctx, "expression evaluation"); err != nil {
			ev.error(err)
		}
		// Reset number of samples in memory after each timestamp.
		ev.currentSamples = tempNumSamples
		// Gather input vectors for this timestamp.
		for i := range exprs {
			var bh, sh []EvalSeriesHelper
			if prepSeries != nil {
				bh = bufHelpers[i][:0]
				sh = seriesHelpers[i]

Review comment (refactor):
ISTM that this is no longer a "preparation" function but a signature function.
And, since it is only ever called with one non-nil implementation, you may as well collapse the parameter to a boolean and hard-code the whole thing here.

I tried a refactor at #16698 which split `rangeEval` and avoided passing the parameter; it also simplifies the logic since we don't have an apparently arbitrary set of expressions to join but exactly 2.  However it makes the code longer, so is perhaps undesirable.

Code after:
		vectors[i] = make(Vector, 0, len(matrixes[i]))
		if len(matrixes[i]) > biggestLen {
			biggestLen = len(matrixes[i])
		}
	}
	enh := &EvalNodeHelper{Out: make(Vector, 0, biggestLen), enableDelayedNameRemoval: ev.enableDelayedNameRemoval}
	type seriesAndTimestamp struct {
		Series
		ts int64
	}
	seriess := make(map[uint64]seriesAndTimestamp, biggestLen) // Output series by series hash.
	tempNumSamples := ev.currentSamples

	var (
		seriesHelpers [][]EvalSeriesHelper
		bufHelpers    [][]EvalSeriesHelper // Buffer updated on each step
	)

	if useSignatures {
		var (
			// Function to compute the join signature for each series.
			sigf  func(labels.Labels) string
			buf   = make([]byte, 0, 1024)
			names = slices.Clone(matching.MatchingLabels)
		)
		if matching.On {
			slices.Sort(names)
			sigf = func(lset labels.Labels) string {
				return string(lset.BytesWithLabels(buf, names...))
			}
		} else { // "without"
			names = append([]string{labels.MetricName}, names...)
			slices.Sort(names)
			sigf = func(lset labels.Labels) string {
				return string(lset.BytesWithoutLabels(buf, names...))
			}
		}

		seriesHelpers = make([][]EvalSeriesHelper, len(exprs))
		bufHelpers = make([][]EvalSeriesHelper, len(exprs))

		signatureToOrdinal := make(map[string]int)

		for i := range exprs {
			seriesHelpers[i] = make([]EvalSeriesHelper, len(matrixes[i]))
			bufHelpers[i] = make([]EvalSeriesHelper, len(matrixes[i]))

			for si, series := range matrixes[i] {
				strSig := sigf(series.Metric)

				if sigOrd, ok := signatureToOrdinal[strSig]; ok {