Descriptive statistics: running moments, sliding window moments and exact percentiles over a sample window.
This module exists because statistics were already written in the library by hand, locked inside their users: CProfilerManager carries mean, M2, minimum, maximum and a queue of 254 samples to compute p50/p90/p99, and none of it is reachable by anyone else. A wrong number in statistics does not show up as a failure, it shows up as a plausible number, so the choices are written here.
1. Mean and variance use Welford, not a sum of squares. The naive form, sum(x^2)/n - mean^2, subtracts two large and almost equal numbers. With samples far from zero (and times in microseconds are exactly that) the cancellation eats the significant digits and the variance comes out wrong, sometimes negative. Welford updates the mean and the sum of deviations without ever forming that pair, and costs the same two operations per sample.
2. Both variances are exposed, and neither hides behind the other. Variance divides by (n-1): the sample estimate, for when the samples are a slice of a process that goes on (measuring 200 runs of a function that will run millions of times). PopulationVariance divides by n, for when the samples are the whole population. Picking one silently is a common and invisible mistake: the difference vanishes when n is large and bites when n is small, which is exactly when someone looks at the number.
3. Without samples everything is zero, not NaN. NaN was refused because it propagates: it taints every following computation without complaint, and ends up written as "nan" in a CSV that someone later loads in a spreadsheet. Zero is just as false, but it does not spread, and Count()/IsEmpty() are right there to settle the doubt.
4. The percentile uses linear interpolation between the order statistics, with the formula written out in CSampleWindow::Percentile. Percentile has no single definition (at least nine are in use), so saying "p99" without saying which identifies no number at all.
5. Not here: bucket histograms. They are the usual way to get a percentile without keeping samples, and the usual way to get it wrong: the result depends on where the buckets were cut, and that choice cannot be undone after the data was aggregated. CSampleWindow keeps the samples and gives the exact percentile of the window. When there is a real need to aggregate without keeping samples, the histogram comes with limits declared by the caller, not guessed here.
Nothing here is guaranteed to be thread safe.