GeezerVison is showing github.com — perf: reduce large-sample sorting overhead (#720) · tinylibs/tinybench@89b5ca1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: FAQ.md +19Lines changed: 19 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,25 @@ Manually looping a fast function to "amplify" its duration can lift a sample | 34 | 34 | above the timer resolution, but it adds loop overhead and changes what you | 35 | 35 | measure; a more precise timestamp provider is usually the cleaner fix. | 36 | 36 |
| | 37 | +## Why does a run take longer than the configured `time`? | | 38 | + | | 39 | +`time` is not a wall-clock timeout. In sequential modes, Tinybench accumulates | | 40 | +per-iteration budget costs and keeps running until both `time` and the minimum | | 41 | +`iterations` are satisfied. Warmup, hooks, loop bookkeeping and final statistics | | 42 | +also contribute to the elapsed time. | | 43 | + | | 44 | +Very fast functions can produce large sample arrays. Sorting and analyzing these | | 45 | +samples takes additional time after collection ends. Debugger instrumentation can | | 46 | +increase both collection and analysis costs. | | 47 | + | | 48 | +`warmup: false` skips warmup, but does not remove these other costs. For synchronous | | 49 | +functions and hooks, `runSync()` avoids asynchronous scheduling in the measurement | | 50 | +loop; it still computes statistics. Reducing `time` can collect fewer samples and | | 51 | +reduce analysis work, at the cost of statistical precision. | | 52 | + | | 53 | +See [Concurrency](./README.md#concurrency) for the different scheduling limits in | | 54 | +task-concurrent mode. | | 55 | + | 37 | 56 | ## What is JS JIT de-optimization? | 38 | 57 |
| 39 | 58 | JavaScript engines (V8, SpiderMonkey, JavaScriptCore) compile hot code paths | |
Collapse file Copy file name to clipboardExpand all lines: src/constants.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,3 +24,8 @@ export const defaultMinimumWarmupIterations = 16 | 24 | 24 | export const emptyFunction = Object.freeze(() => { | 25 | 25 | /* no op */ | 26 | 26 | }) | | 27 | + | | 28 | +/** | | 29 | + * Minimum sample count for considering native numeric sorting. | | 30 | + */ | | 31 | +export const nativeSortThreshold = 1024 |
Collapse file Copy file name to clipboardExpand all lines: src/statistics.ts +34-1Lines changed: 34 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { Samples, SortedSamples, Statistics } from './types' | 2 | 2 |
| | 3 | +import { nativeSortThreshold } from './constants' | 3 | 4 | import { studentTCritical } from './student-t' | 4 | 5 |
| 5 | 6 | /** | @@ -20,7 +21,39 @@ export const isValidSamples = ( | 20 | 21 | export function sortSamples ( | 21 | 22 | samples: Samples | 22 | 23 | ): asserts samples is SortedSamples { | 23 | | - samples.sort(sortFn) | | 24 | + const len = samples.length | | 25 | + if (len < nativeSortThreshold) { | | 26 | + samples.sort(sortFn) | | 27 | + return | | 28 | + } | | 29 | + | | 30 | + let ascending = true | | 31 | + let descending = true | | 32 | + let previous = samples[0] | | 33 | + for (let i = 0; i < len; i++) { | | 34 | + const value = samples[i]! // eslint-disable-line @typescript-eslint/no-non-null-assertion | | 35 | + // Preserve comparator semantics for non-finite values and negative zero. | | 36 | + if (!Number.isFinite(value) || Object.is(value, -0)) { | | 37 | + samples.sort(sortFn) | | 38 | + return | | 39 | + } | | 40 | + if (ascending || descending) { | | 41 | + ascending &&= value >= previous | | 42 | + descending &&= value <= previous | | 43 | + previous = value | | 44 | + } | | 45 | + } | | 46 | + if (ascending) return | | 47 | + if (descending) { | | 48 | + samples.reverse() | | 49 | + return | | 50 | + } | | 51 | + | | 52 | + // Native numeric sorting avoids a JavaScript callback per comparison. | | 53 | + const sorted = new Float64Array(samples).sort() | | 54 | + for (let i = 0; i < len; i++) { | | 55 | + samples[i] = sorted[i]! // eslint-disable-line @typescript-eslint/no-non-null-assertion | | 56 | + } | 24 | 57 | } | 25 | 58 |
| 26 | 59 | /** | |
Collapse file Copy file name to clipboard +81-15Lines changed: 81 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { expect, test } from 'vitest' | 2 | 2 |
| 3 | | -import { isValidSamples, sortSamples } from '../src/utils' | 4 | | - | 5 | | -test('sortSamples', () => { | 6 | | - const samples: number[] = [1, 2, 3, 4, 5, 0] | 7 | | - | 8 | | - if (isValidSamples(samples)) { | 9 | | - expect(sortSamples(samples)).toBe(undefined) // eslint-disable-line | 10 | | - expect(samples[0]).toBe(0) | 11 | | - expect(samples[1]).toBe(1) | 12 | | - expect(samples[2]).toBe(2) | 13 | | - expect(samples[3]).toBe(3) | 14 | | - expect(samples[4]).toBe(4) | 15 | | - expect(samples[5]).toBe(5) | 16 | | - } else { | 17 | | - throw new Error('Invalid samples') | | 3 | +import type { Samples } from '../src/types' | | 4 | + | | 5 | +import { sortSamples } from '../src/utils' | | 6 | + | | 7 | +test('sorts a small sample in place', () => { | | 8 | + const samples: Samples = [1, 2, 3, 4, 5, 0] | | 9 | + | | 10 | + sortSamples(samples) | | 11 | + | | 12 | + expect(samples).toEqual([0, 1, 2, 3, 4, 5]) | | 13 | +}) | | 14 | + | | 15 | +test.each(['permuted', 'ascending', 'descending'])( | | 16 | + 'sorts large %s samples of positive and negative fractions', | | 17 | + order => { | | 18 | + const length = 8192 | | 19 | + const samples = Array.from({ length }, (_, i) => { | | 20 | + const index = order === 'permuted' | | 21 | + ? (i * 4051) % length | | 22 | + : order === 'descending' ? length - i - 1 : i | | 23 | + return (index - length / 2) / 16 | | 24 | + }) as Samples | | 25 | + | | 26 | + sortSamples(samples) | | 27 | + | | 28 | + expect(samples).toEqual( | | 29 | + Array.from({ length }, (_, i) => (i - length / 2) / 16) | | 30 | + ) | 18 | 31 | } | | 32 | +) | | 33 | + | | 34 | +test('preserves signed zero order among equal large samples', () => { | | 35 | + const repeated = Array<number>(8192).fill(1) | | 36 | + const samples: Samples = [0, ...repeated, -0, 0, -0, 0] | | 37 | + | | 38 | + sortSamples(samples) | | 39 | + | | 40 | + expect(samples).toEqual([0, -0, 0, -0, 0, ...repeated]) | | 41 | +}) | | 42 | + | | 43 | +test('sorts large samples containing infinities and extreme finite values', () => { | | 44 | + const repeated = Array<number>(8192).fill(1) | | 45 | + const samples: Samples = [ | | 46 | + Number.NEGATIVE_INFINITY, | | 47 | + ...repeated, | | 48 | + Number.POSITIVE_INFINITY, | | 49 | + Number.MAX_VALUE, | | 50 | + -Number.MAX_VALUE, | | 51 | + Number.MIN_VALUE, | | 52 | + -Number.MIN_VALUE, | | 53 | + ] | | 54 | + | | 55 | + sortSamples(samples) | | 56 | + | | 57 | + expect(samples).toEqual([ | | 58 | + Number.NEGATIVE_INFINITY, | | 59 | + -Number.MAX_VALUE, | | 60 | + -Number.MIN_VALUE, | | 61 | + Number.MIN_VALUE, | | 62 | + ...repeated, | | 63 | + Number.MAX_VALUE, | | 64 | + Number.POSITIVE_INFINITY, | | 65 | + ]) | | 66 | +}) | | 67 | + | | 68 | +test('preserves finite extremes and precision in large unsorted samples', () => { | | 69 | + const repeated = Array<number>(8192).fill(1) | | 70 | + const samples: Samples = [ | | 71 | + Number.MAX_VALUE, | | 72 | + ...repeated, | | 73 | + Number.MIN_VALUE, | | 74 | + 0.001, | | 75 | + ] | | 76 | + | | 77 | + sortSamples(samples) | | 78 | + | | 79 | + expect(samples).toEqual([ | | 80 | + Number.MIN_VALUE, | | 81 | + 0.001, | | 82 | + ...repeated, | | 83 | + Number.MAX_VALUE, | | 84 | + ]) | 19 | 85 | }) |
|