GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf: reduce large-sample sorting overhead (#720) · tinylibs/tinybench@89b5ca1

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • FAQ.md
  • src
    • constants.ts
    • statistics.ts
  • test
    • utils-sort-samples.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎FAQ.md‎

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 numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ Manually looping a fast function to "amplify" its duration can lift a sample
3434
above the timer resolution, but it adds loop overhead and changes what you
3535
measure; a more precise timestamp provider is usually the cleaner fix.
3636

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+
3756
## What is JS JIT de-optimization?
3857

3958
JavaScript engines (V8, SpiderMonkey, JavaScriptCore) compile hot code paths
Collapse file

‎src/constants.ts‎

Copy file name to clipboardExpand all lines: src/constants.ts
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ export const defaultMinimumWarmupIterations = 16
2424
export const emptyFunction = Object.freeze(() => {
2525
/* no op */
2626
})
27+
28+
/**
29+
* Minimum sample count for considering native numeric sorting.
30+
*/
31+
export const nativeSortThreshold = 1024
Collapse file

‎src/statistics.ts‎

Copy file name to clipboardExpand all lines: src/statistics.ts
+34-1Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Samples, SortedSamples, Statistics } from './types'
22

3+
import { nativeSortThreshold } from './constants'
34
import { studentTCritical } from './student-t'
45

56
/**
@@ -20,7 +21,39 @@ export const isValidSamples = (
2021
export function sortSamples (
2122
samples: Samples
2223
): 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+
}
2457
}
2558

2659
/**
Collapse file

‎test/utils-sort-samples.test.ts‎

Copy file name to clipboard
+81-15Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,85 @@
11
import { expect, test } from 'vitest'
22

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+
)
1831
}
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+
])
1985
})

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.