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

Code before:
		LastFourSamples = append(LastFourSamples, Sample{
			F: samples.Floats[i].F,
			T: samples.Floats[i].T,
		})
	}
	// Add last 2 histogram samples if exists
	for i := len(samples.Histograms) - 1; i >= max(0, len(samples.Histograms)-2); i-- {
		LastFourSamples = append(LastFourSamples, Sample{
			H: samples.Histograms[i].H,
			T: samples.Histograms[i].T,
		})
	}

	// No sense in trying to compute a rate without at least two points. Drop
	// this Vector element.
	// TODO: add RangeTooShortWarning
	if len(LastFourSamples) < 2 {
		return out, nil
	}
	// sort the last four samples according to their timestamps
	sort.Slice(LastFourSamples, func(i, j int) bool {
		return LastFourSamples[i].T < LastFourSamples[j].T
	})

	lastSample := LastFourSamples[len(LastFourSamples)-1]
	prevSample := LastFourSamples[len(LastFourSamples)-2]
	resultSample := lastSample
	sampledInterval := lastSample.T - prevSample.T
	if sampledInterval == 0 {
		// Avoid dividing by 0.
		return out, nil
	}
	switch {
	case lastSample.H == nil && prevSample.H == nil:
		if isRate && lastSample.F < prevSample.F {
			// Counter reset.
		} else {
			resultSample.F = lastSample.F - prevSample.F
		}
	case lastSample.H != nil && prevSample.H != nil:
		resultSample.H = lastSample.H.Copy()
		// irate should only be applied to counters.
		if isRate && (lastSample.H.CounterResetHint == histogram.GaugeType || prevSample.H.CounterResetHint == histogram.GaugeType) {
			annos.Add(annotations.NewNativeHistogramNotCounterWarning(metricName, args.PositionRange()))
		}
		// idelta should only be applied to gauges.
		if !isRate && (lastSample.H.CounterResetHint != histogram.GaugeType || prevSample.H.CounterResetHint != histogram.GaugeType) {
			annos.Add(annotations.NewNativeHistogramNotGaugeWarning(metricName, args.PositionRange()))
		}
		_, err := resultSample.H.Sub(prevSample.H)
		if err != nil {

Review comment (suggestion):
This is doing what we need but it feels a bit heavy-handed. How about something like this:

- Create a slice like `ss := make([]Sample, 0, 2)`.
- Go through the last two elements of `samples.Floats` and append them as `Sample` to `ss` (similar to before).
- Go through the last two elements of `samples.Histograms` and then go through the existing samples in `ss` and insert the histogram samples at the right place (the following instructions can probably be condensed in a more elegant fashion, but you can see what I'm up to):
  - If its `T` is earlier than the current 1st element, discard.
  - If its `T` is between the two `T`s already in `ss`, replace the 1st element.
  - If its `T` is later than the current 2nd element, make the 2nd element the 1st, and insert as 2nd.
  - If `ss` is still empty, just append.
  - If `ss` has only one element, insert as 1st or 2nd, depending on the existing `T` and move the existing element around as needed.
- If `ss` now hasn't at least 2 elements, return.
- I would then not assign to `lastSample` and `prevSample`, because you have access to the nice and shart `ss[0]` and `ss[1]` from now on.
- Continue as before. 

Code after:
	// No sense in trying to compute a rate without at least two points. Drop
	// this Vector element.
	// TODO: add RangeTooShortWarning
	if len(samples.Floats)+len(samples.Histograms) < 2 {
		return out, nil
	}

	// Add the last 2 float samples if they exist.
	for i := max(0, len(samples.Floats)-2); i < len(samples.Floats); i++ {
		ss = append(ss, Sample{
			F: samples.Floats[i].F,
			T: samples.Floats[i].T,
		})
	}

	// Add the last 2 histogram samples into their correct position if they exist.
	for i := max(0, len(samples.Histograms)-2); i < len(samples.Histograms); i++ {
		s := Sample{
			H: samples.Histograms[i].H,
			T: samples.Histograms[i].T,
		}
		switch {
		case len(ss) == 0:
			ss = append(ss, s)
		case len(ss) == 1:
			if s.T < ss[0].T {
				ss = append([]Sample{s}, ss...)
			} else {
				ss = append(ss, s)
			}
		case s.T < ss[0].T:
			// s is older than 1st, so discard it.
		case s.T > ss[1].T:
			// s is newest, so add it as 2nd and make the old 2nd the new 1st.
			ss[0] = ss[1]
			ss[1] = s
		default:
			// In all other cases, we just make s the new 1st.
			// This establishes a correct order, even in the (irregular)
			// case of equal timestamps.
			ss[0] = s
		}
	}

	resultSample := ss[1]
	sampledInterval := ss[1].T - ss[0].T
	if sampledInterval == 0 {
		// Avoid dividing by 0.
		return out, nil
	}
	switch {