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

Code before:
	annos.Merge(enh.resetHistograms(inVec, args[1]))

	for _, q := range qs {
		var qStr string

		getOrCreateLblsWithQuantile := func(lbls labels.Labels) labels.Labels {
			if enh.signatureToLabelsWithQuantile == nil {
				enh.signatureToLabelsWithQuantile = make(map[string]map[float64]labels.Labels)
			}

			enh.lblBuf = lbls.Bytes(enh.lblBuf)
			cachedLbls, ok := enh.signatureToLabelsWithQuantile[string(enh.lblBuf)]
			if !ok {
				enh.signatureToLabelsWithQuantile[string(enh.lblBuf)] = make(map[float64]labels.Labels, len(qs))
			}

			cachedLblsWithQuantile, ok := cachedLbls[q]
			if !ok {
				if qStr == "" {
					qStr = fmt.Sprintf("%g", q)
				}
				cachedLblsWithQuantile = labels.NewBuilder(lbls).
					Set(quantileLabel, qStr).
					Labels()

				enh.signatureToLabelsWithQuantile[string(enh.lblBuf)] = cachedLbls
			}

			return cachedLblsWithQuantile
		}

		// Deal with the native histograms.
		for _, sample := range enh.nativeHistogramSamples {
			if sample.H == nil {
				// Native histogram conflicts with classic histogram at the same timestamp, ignore.
				continue
			}
			if !enh.enableDelayedNameRemoval {
				sample.Metric = sample.Metric.DropReserved(schema.IsMetadataLabel)
			}
			hq, hqAnnos := HistogramQuantile(q, sample.H, sample.Metric.Get(model.MetricNameLabel), args[0].PositionRange())
			annos.Merge(hqAnnos)
			enh.Out = append(enh.Out, Sample{
				Metric:   getOrCreateLblsWithQuantile(sample.Metric),
				F:        hq,
				DropName: true,
			})
		}

		// Deal with classic histograms that have already been filtered for conflicting native histograms.
		for _, mb := range enh.signatureToMetricWithBuckets {

Review comment (performance):
I believe this should be updating `cachedLbls[q]`?
```suggestion
				cachedLbls[q] = cachedLblsWithQuantile
```

Code after:
		})
	}

	// Deal with classic histograms that have already been filtered for conflicting native histograms.
	for _, mb := range enh.signatureToMetricWithBuckets {
		if len(mb.buckets) == 0 {
			continue
		}
		if !enh.enableDelayedNameRemoval {
			mb.metric = mb.metric.DropReserved(schema.IsMetadataLabel)
		}

		enh.Out = append(enh.Out, Sample{
			Metric:   mb.metric,
			F:        BucketFraction(lower, upper, mb.buckets),
			DropName: true,
		})
	}

	return enh.Out, annos
}

// === histogram_quantile(k parser.ValueTypeScalar, Vector parser.ValueTypeVector) (Vector, Annotations) ===
func funcHistogramQuantile(vectorVals []Vector, _ Matrix, args parser.Expressions, enh *EvalNodeHelper) (Vector, annotations.Annotations) {
	if len(vectorVals) < 2 || len(vectorVals[0]) == 0 {
		return enh.Out, nil
	}
	q := vectorVals[0][0].F
	inVec := vectorVals[1]
	var annos annotations.Annotations

	if err := validateQuantile(q, args[0]); err != nil {
		annos.Add(err)
	}
	annos.Merge(enh.resetHistograms(inVec, args[1]))

	// Deal with the native histograms.
	for _, sample := range enh.nativeHistogramSamples {
		if sample.H == nil {
			// Native histogram conflicts with classic histogram at the same timestamp, ignore.
			continue
		}
		if !enh.enableDelayedNameRemoval {
			sample.Metric = sample.Metric.DropReserved(schema.IsMetadataLabel)
		}
		hq, hqAnnos := HistogramQuantile(q, sample.H, getMetricName(sample.Metric), args[0].PositionRange())
		annos.Merge(hqAnnos)
		enh.Out = append(enh.Out, Sample{
			Metric:   sample.Metric,
			F:        hq,
			DropName: true,