Skip to content

Avoid merging unrelated parameters in sort checks - #862

Merged
garak merged 3 commits into
KnpLabs:masterfrom
andreybolonin:perf/sort-parameter-lookup
Oct 30, 2026
Merged

garak merged 3 commits into
KnpLabs:masterfrom
andreybolonin:perf/sort-parameter-lookup

Conversation

@andreybolonin

Copy link
Copy Markdown
Contributor

Improvement

Q A
New Feature no
RFC no
BC Break no

Summary

isSorted() currently merges every route/query parameter to inspect one sorting field. This repeats for sortable table headers and copies the merged array when overrides are present. Read only the selected string field, using array_key_exists() so an explicit null override still clears the base value. Keep the existing merge path for numeric and other parameter names to preserve key reindexing and existing behavior. No parameter-count limits or persistent caches are introduced.

Validation

  • composer test: 42 tests, 53 assertions passed on PHP 8.5.11 / PHPUnit 13.4.3.
  • vendor/bin/phpstan analyse --no-progress: passed (PHPStan 2.3.1, repository configuration).
  • PHP-CS-Fixer 3.95.27: repository configuration, full config, src, and tests scan, dry run clean.
  • composer validate --strict --no-check-lock and git diff --check: passed.
  • Local validation used PHP 8.5; other PHP/dependency combinations are left to the repository CI matrix.

Benchmark method

Baseline: a91a854720df2dbfd04cbf8c44c3dad6ce22fdca (current upstream master). Environment: macOS 27.0, Darwin 27.0.0, arm64; PHP 8.5.11 NTS; knp-components 5.3.0. Two runs: CLI OPcache disabled/enabled; JIT disabled. Both implementations load from files, with only the baseline class renamed, and opcache.file_update_protection=0 gives them equal eligibility for caching. Benchmark processes ran sequentially.

The included tests/Benchmark/SortParameter.php checks equal outputs, warms each implementation for 1,000 calls, then measures nine samples per case with hrtime(true), alternating baseline/candidate order. Tables report median [minimum–maximum] nanoseconds per call and (1 - candidate / baseline) * 100 time reduction. Dynamic invocation and loop overhead are included equally. Sub-microsecond differences and near-zero changes should be treated as measurement noise.

The benchmark uses sort=title, 0/5/20/100/1000 additional string-keyed parameters, and four scenarios: matching without overrides; matching with ['sort' => 'title']; a different sort key; and [null, ['sort' => null]]. Each time sample contains 100,000 calls. Numeric-name compatibility is covered by PHPUnit; it retains the original algorithm and has no claimed speedup.

Memory is measured separately after warmup and GC, over 1,000 calls with discarded outputs. Extra peak bytes are memory_get_peak_usage(false) - memory_get_usage(false) after resetting the peak. This is additional PHP-managed memory during the call loop, not total process RSS or OPcache storage. Retained bytes are the change in active memory after the loop; allocator peak bytes use the corresponding true counters. Both retained bytes and extra allocator-page peak bytes were zero for every measured case and implementation. Inputs and object setup are outside the measurement; both classes have the same instance properties.

The improvement depends on parameters and call shape: small inputs without overrides do not consistently reach 20%. The common override path exceeds 20% in these measurements; larger parameter sets gain more. These are method timings, not an application/request latency claim.

Results: CLI OPcache off

Case Baseline ns/call: median [min–max] Candidate ns/call: median [min–max] Time reduction Extra peak bytes: baseline → candidate
0 extra params; match, no overrides 118.9 [116.7–120.8] 101.9 [101.5–103.9] 14.3% 40 → 40
0 extra params; match, override 148.1 [147.3–150.5] 100.3 [99.9–102.2] 32.3% 376 → 40
0 extra params; mismatch 117.8 [116.9–118.5] 100.7 [99.7–102.1] 14.5% 40 → 40
0 extra params; null override 109.7 [109.3–112.1] 91.3 [90.9–92.9] 16.8% 376 → 40
5 extra params; match, no overrides 121.6 [120.9–123.9] 102.5 [102.1–108.7] 15.8% 40 → 40
5 extra params; match, override 178.1 [177.0–183.0] 100.5 [99.9–101.8] 43.6% 376 → 40
5 extra params; mismatch 121.1 [120.4–122.8] 101.6 [100.2–103.3] 16.1% 40 → 40
5 extra params; null override 138.6 [137.3–139.2] 91.3 [90.9–92.8] 34.1% 376 → 40
20 extra params; match, no overrides 127.7 [127.2–129.9] 102.6 [102.1–103.7] 19.7% 40 → 40
20 extra params; match, override 267.2 [265.5–270.0] 100.5 [99.8–102.9] 62.4% 1336 → 40
20 extra params; mismatch 127.4 [127.0–129.7] 101.6 [101.0–103.3] 20.2% 40 → 40
20 extra params; null override 228.0 [226.6–229.0] 91.9 [90.8–93.7] 59.7% 1336 → 40
100 extra params; match, no overrides 176.1 [173.9–177.5] 102.0 [101.5–103.5] 42.1% 40 → 40
100 extra params; match, override 687.9 [685.6–688.7] 100.1 [99.8–102.3] 85.4% 8248 → 40
100 extra params; mismatch 175.3 [173.7–177.0] 101.0 [100.3–101.6] 42.4% 40 → 40
100 extra params; null override 650.8 [649.2–897.6] 91.9 [90.9–93.0] 85.9% 8248 → 40
1000 extra params; match, no overrides 710.4 [707.0–716.8] 103.6 [103.3–105.8] 85.4% 40 → 40
1000 extra params; match, override 5365.9 [5335.9–5432.9] 100.5 [99.9–107.3] 98.1% 41016 → 40
1000 extra params; mismatch 712.1 [710.7–818.0] 103.4 [102.4–104.2] 85.5% 40 → 40
1000 extra params; null override 5304.0 [5295.8–5343.6] 91.4 [90.9–92.5] 98.3% 41016 → 40

Results: CLI OPcache on

Case Baseline ns/call: median [min–max] Candidate ns/call: median [min–max] Time reduction Extra peak bytes: baseline → candidate
0 extra params; match, no overrides 104.0 [103.6–105.3] 88.1 [87.3–89.1] 15.3% 40 → 40
0 extra params; match, override 121.9 [120.3–128.5] 82.9 [82.1–85.8] 32.0% 376 → 40
0 extra params; mismatch 106.3 [105.6–107.5] 90.0 [89.6–90.7] 15.3% 40 → 40
0 extra params; null override 93.1 [92.3–94.4] 78.7 [78.6–79.1] 15.5% 376 → 40
5 extra params; match, no overrides 109.3 [108.9–111.6] 90.1 [89.7–91.3] 17.5% 40 → 40
5 extra params; match, override 153.5 [152.5–154.3] 82.6 [82.2–84.0] 46.2% 376 → 40
5 extra params; mismatch 111.0 [110.6–112.1] 92.2 [92.0–93.6] 17.0% 40 → 40
5 extra params; null override 124.8 [124.2–127.2] 78.8 [78.5–79.8] 36.9% 376 → 40
20 extra params; match, no overrides 115.2 [114.8–116.6] 90.0 [89.7–90.5] 21.9% 40 → 40
20 extra params; match, override 245.5 [244.3–247.0] 82.8 [82.0–84.6] 66.3% 1336 → 40
20 extra params; mismatch 117.9 [116.7–118.7] 92.1 [91.9–93.2] 21.9% 40 → 40
20 extra params; null override 216.3 [215.1–222.4] 78.3 [77.7–79.4] 63.8% 1336 → 40
100 extra params; match, no overrides 161.9 [161.2–163.2] 88.6 [88.0–89.4] 45.3% 40 → 40
100 extra params; match, override 668.9 [667.8–672.5] 82.3 [81.9–83.6] 87.7% 8248 → 40
100 extra params; mismatch 164.6 [163.6–173.9] 91.1 [90.3–93.2] 44.6% 40 → 40
100 extra params; null override 645.7 [638.7–680.7] 78.8 [77.4–87.1] 87.8% 8248 → 40
1000 extra params; match, no overrides 699.1 [697.7–701.2] 91.5 [91.0–93.0] 86.9% 40 → 40
1000 extra params; match, override 5364.2 [5352.7–5384.6] 82.7 [82.2–84.5] 98.5% 41016 → 40
1000 extra params; mismatch 701.0 [699.6–704.9] 94.8 [93.5–104.0] 86.5% 40 → 40
1000 extra params; null override 5320.8 [5299.7–5560.3] 81.1 [78.4–83.8] 98.5% 41016 → 40

Reproduce

Run from this PR checkout after composer install:

git show a91a854720df2dbfd04cbf8c44c3dad6ce22fdca:src/Pagination/SlidingPagination.php | sed 's/final class SlidingPagination /final class BenchmarkBaselinePagination /' > /tmp/knp-baseline.php
php -d opcache.enable_cli=0 -d opcache.file_update_protection=0 tests/Benchmark/SortParameter.php /tmp/knp-baseline.php > /tmp/knp-sort-off.json
php -d opcache.enable_cli=1 -d opcache.file_update_protection=0 tests/Benchmark/SortParameter.php /tmp/knp-baseline.php > /tmp/knp-sort-on.json

The JSON includes all nine raw samples, call counts, environment details and all three memory metrics for each implementation.

Raw measurements: CLI OPcache off
{"php":"8.5.11","os":"macOS 27.0 / Darwin 27.0.0 / arm64","opcache_cli":"0","jit":"disable","jit_buffer_size":"64M","warmup_calls_per_variant":1000,"rounds":9,"memory_calls_per_variant":1000,"results":[{"case":"0 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":118.86375,"min_ns":116.67291,"max_ns":120.78042,"samples_ns":[117.77834,116.67291,118.6025,120.28834,118.79542,119.12167,120.35792,120.78042,118.86375],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":101.87625,"min_ns":101.51709,"max_ns":103.93875,"samples_ns":[101.54083,103.4375,102.99709,102.70166,103.93875,101.87625,101.51709,101.51959,101.67834],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":14.291573335015928},{"case":"0 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":148.14625,"min_ns":147.27167,"max_ns":150.54583,"samples_ns":[148.14625,150.54583,147.98125,147.27167,148.47792,148.73333,148.16875,147.65792,148.04292],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":100.31708,"min_ns":99.85958,"max_ns":102.17042,"samples_ns":[99.90333,99.85958,101.22958,100.53,99.86917,100.58333,100.31708,102.17042,99.91667],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":32.28510340288735},{"case":"0 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":117.7575,"min_ns":116.88375,"max_ns":118.53834,"samples_ns":[117.05375,117.89666,117.925,117.11458,117.7575,118.53834,118.08708,116.88375,117.15125],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":100.66125,"min_ns":99.70959,"max_ns":102.0925,"samples_ns":[101.33458,99.70959,101.36708,100.52917,100.3225,100.18209,100.66125,102.0925,101.50209],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":14.51818355518757},{"case":"0 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":109.72125,"min_ns":109.26458,"max_ns":112.12292,"samples_ns":[109.61125,112.12292,109.5325,109.74708,109.29333,109.72125,110.37083,109.26458,111.02667],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":91.26959,"min_ns":90.93167,"max_ns":92.94958,"samples_ns":[91.36834,90.93167,90.96084,92.60916,91.60791,90.99625,92.94958,91.26959,91.01459],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":16.81685179488932},{"case":"5 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":121.63125,"min_ns":120.93833,"max_ns":123.85458,"samples_ns":[121.27417,121.09542,122.17,122.25625,120.93833,121.59375,123.85458,123.31541,121.63125],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":102.46125,"min_ns":102.12375,"max_ns":108.71666,"samples_ns":[103.74666,102.46125,102.12375,102.38542,102.14833,103.09041,106.28083,108.71666,102.22],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":15.760752273778312},{"case":"5 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":178.1275,"min_ns":177.04292,"max_ns":182.99833,"samples_ns":[177.17875,179.57166,182.99833,178.7575,177.22041,177.04292,178.1275,177.31166,179.54917],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":100.46959,"min_ns":99.86375,"max_ns":101.84167,"samples_ns":[101.84167,100.10916,100.44875,99.86375,101.18791,100.46959,100.01917,100.495,100.72],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":43.59681127282424},{"case":"5 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":121.08375,"min_ns":120.41875,"max_ns":122.75542,"samples_ns":[120.61208,121.42292,121.38917,122.75542,120.57292,121.36666,120.71792,120.41875,121.08375],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":101.55375,"min_ns":100.15917,"max_ns":103.33958,"samples_ns":[103.33958,100.15917,101.31834,101.25833,101.80459,101.30917,102.33084,101.55375,102.17959],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":16.12933197064016},{"case":"5 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":138.5875,"min_ns":137.30334,"max_ns":139.20292,"samples_ns":[138.40666,138.14,138.60959,139.20292,138.68083,138.5875,138.16375,137.30334,138.88167],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":91.33292,"min_ns":90.90875,"max_ns":92.83125,"samples_ns":[92.57208,91.7525,91.16334,92.83125,91.07416,91.08792,91.61417,90.90875,91.33292],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":34.09728871651484},{"case":"20 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":127.65958,"min_ns":127.23125,"max_ns":129.895,"samples_ns":[127.93042,127.64041,128.53084,127.93917,127.29917,127.64083,127.23125,127.65958,129.895],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":102.55709,"min_ns":102.10167,"max_ns":103.65041,"samples_ns":[103.12916,102.23291,102.55709,102.10167,102.28667,102.58667,103.65041,103.00542,102.4075],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":19.663616314576625},{"case":"20 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":267.17042,"min_ns":265.51541,"max_ns":269.95625,"samples_ns":[267.16542,268.79042,266.63208,267.17042,267.36417,267.53042,266.00541,269.95625,265.51541],"memory":{"peak_extra_bytes":1336,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":100.4675,"min_ns":99.82791,"max_ns":102.9275,"samples_ns":[99.885,99.82791,99.845,102.89125,102.9275,102.20584,101.84,100.38583,100.4675],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":62.39572479618065},{"case":"20 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":127.41583,"min_ns":126.98667,"max_ns":129.70542,"samples_ns":[127.29333,128.08417,127.39208,126.98667,127.64584,129.70542,127.41583,127.47583,127.28],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":101.62625,"min_ns":101.005,"max_ns":103.29208,"samples_ns":[101.005,102.89,102.90875,101.37459,101.12167,101.71833,101.05,103.29208,101.62625],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":20.240483462690626},{"case":"20 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":228.00917,"min_ns":226.55333,"max_ns":228.99958,"samples_ns":[226.55333,228.99958,227.10791,228.78,228.09042,228.00917,227.27833,227.2225,228.78084],"memory":{"peak_extra_bytes":1336,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":91.87667,"min_ns":90.78167,"max_ns":93.73708,"samples_ns":[93.73708,91.28334,91.87667,91.36125,91.68041,92.50333,92.55167,93.12083,90.78167],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":59.704835555517356},{"case":"100 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":176.11,"min_ns":173.93583,"max_ns":177.49291,"samples_ns":[177.49291,177.16375,175.09125,176.11,174.22792,177.0325,176.60916,176.00667,173.93583],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":102.04584,"min_ns":101.47333,"max_ns":103.50125,"samples_ns":[101.55916,102.04584,103.50125,101.8575,101.98958,103.18083,102.60209,101.47333,103.06167],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":42.05562432570553},{"case":"100 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":687.88916,"min_ns":685.61708,"max_ns":688.73333,"samples_ns":[687.88958,688.20125,688.01875,688.73333,686.87042,685.61708,687.88916,687.79792,687.52083],"memory":{"peak_extra_bytes":8248,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":100.14667,"min_ns":99.77291,"max_ns":102.31833,"samples_ns":[99.81791,100.14667,100.66916,99.77291,99.78,100.48416,100.57083,99.83708,102.31833],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":85.44145251540233},{"case":"100 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":175.27833,"min_ns":173.74,"max_ns":176.96125,"samples_ns":[175.27833,174.37625,175.60834,175.32209,173.74,174.66,175.92083,176.96125,174.41125],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":100.97541,"min_ns":100.34083,"max_ns":101.55583,"samples_ns":[101.11709,101.55583,100.88708,100.47084,101.08042,100.34083,100.96917,101.09833,100.97541],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":42.39138973996387},{"case":"100 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":650.83416,"min_ns":649.155,"max_ns":897.625,"samples_ns":[650.83416,665.43875,649.93375,649.155,656.2675,649.15916,649.83125,897.625,655.95584],"memory":{"peak_extra_bytes":8248,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":91.885,"min_ns":90.89458,"max_ns":92.99791,"samples_ns":[91.09666,91.61708,92.515,92.25459,91.885,90.95209,90.89458,92.99791,92.53542],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":85.8819641550468},{"case":"1000 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":710.39167,"min_ns":706.99125,"max_ns":716.82375,"samples_ns":[712.92125,716.82375,712.91583,706.99125,709.98333,710.26208,710.39167,709.065,714.30917],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":103.63292,"min_ns":103.26,"max_ns":105.83625,"samples_ns":[104.33083,103.50542,103.70958,104.215,103.26,103.45333,103.63292,103.30833,105.83625],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":85.41186160023526},{"case":"1000 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":5365.88875,"min_ns":5335.88208,"max_ns":5432.94083,"samples_ns":[5335.88208,5345.99375,5346.03542,5354.32167,5432.94083,5418.11625,5381.6125,5418.5575,5365.88875],"memory":{"peak_extra_bytes":41016,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":100.47625,"min_ns":99.85042,"max_ns":107.33375,"samples_ns":[101.45417,100.42542,101.86084,99.85042,106.82375,107.33375,100.47625,100.13292,100.34083],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":98.12750031390419},{"case":"1000 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":712.1,"min_ns":710.68292,"max_ns":817.96084,"samples_ns":[712.72583,711.65583,711.09792,710.68292,712.1,711.20375,714.20833,817.96084,731.40625],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":103.395,"min_ns":102.40958,"max_ns":104.17792,"samples_ns":[102.40958,102.7375,104.17792,103.47042,102.70333,103.395,102.84709,103.5225,103.58334],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":85.48026962505266},{"case":"1000 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":5304.00125,"min_ns":5295.78125,"max_ns":5343.61583,"samples_ns":[5343.61583,5298.52625,5305.36333,5303.40125,5311.86667,5303.16334,5305.4725,5304.00125,5295.78125],"memory":{"peak_extra_bytes":41016,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":91.35833,"min_ns":90.90208,"max_ns":92.54791,"samples_ns":[90.90208,92.23625,91.55041,90.9275,92.54791,91.26167,91.35833,91.2425,92.07167],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":98.27755828677454}]}
Raw measurements: CLI OPcache on
{"php":"8.5.11","os":"macOS 27.0 / Darwin 27.0.0 / arm64","opcache_cli":"1","jit":"disable","jit_buffer_size":"64M","warmup_calls_per_variant":1000,"rounds":9,"memory_calls_per_variant":1000,"results":[{"case":"0 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":103.9575,"min_ns":103.56458,"max_ns":105.3475,"samples_ns":[103.9575,103.85875,104.10334,104.89416,103.56458,103.78583,103.85292,103.9675,105.3475],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":88.07666,"min_ns":87.28917,"max_ns":89.09333,"samples_ns":[87.42208,88.07666,87.28917,88.675,88.87083,87.77583,88.09667,87.38167,89.09333],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":15.276281172594564},{"case":"0 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":121.94,"min_ns":120.33625,"max_ns":128.45916,"samples_ns":[120.77833,120.64375,120.65209,123.33667,122.89375,128.45916,121.94,122.57958,120.33625],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":82.94292,"min_ns":82.14708,"max_ns":85.79375,"samples_ns":[82.14708,84.12459,83.06208,82.94292,83.98208,85.79375,82.445,82.2625,82.57666],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":31.98054781039855},{"case":"0 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":106.345,"min_ns":105.57542,"max_ns":107.51833,"samples_ns":[106.0925,105.68166,107.51833,107.315,105.78583,107.35792,106.345,106.82375,105.57542],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":90.04666,"min_ns":89.58458,"max_ns":90.73417,"samples_ns":[90.73417,90.66583,89.69208,89.65833,90.20875,90.04666,89.58458,90.66958,89.91917],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":15.32591095020922},{"case":"0 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":93.0525,"min_ns":92.26667,"max_ns":94.42708,"samples_ns":[93.0525,92.56292,93.99041,92.56875,93.98708,92.91333,93.35084,92.26667,94.42708],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":78.66291,"min_ns":78.57541,"max_ns":79.12292,"samples_ns":[78.595,79.105,79.12292,78.735,78.62875,78.57541,78.66291,78.62583,78.98375],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":15.463947771419361},{"case":"5 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":109.26041,"min_ns":108.88959,"max_ns":111.61167,"samples_ns":[109.11042,108.88959,108.91,111.61167,110.69709,108.94291,109.52875,111.2575,109.26041],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":90.11875,"min_ns":89.66708,"max_ns":91.30125,"samples_ns":[90.71041,90.78584,91.30125,90.215,89.77542,89.66708,90.11875,90.00958,89.83333],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":17.519300906888404},{"case":"5 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":153.51541,"min_ns":152.49292,"max_ns":154.28459,"samples_ns":[154.13084,153.51541,152.89583,153.93416,152.49292,152.62708,153.94667,154.28459,153.07083],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":82.58417,"min_ns":82.18042,"max_ns":84.0425,"samples_ns":[82.75916,82.50708,82.62167,84.0425,83.30917,82.18042,82.58417,82.21584,82.25042],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":46.20463834868435},{"case":"5 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":110.99125,"min_ns":110.61167,"max_ns":112.10875,"samples_ns":[112.10875,111.49583,110.81625,111.33917,110.99125,110.9325,110.79458,110.61167,111.79375],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":92.15541,"min_ns":91.99083,"max_ns":93.55875,"samples_ns":[91.99083,91.99208,92.15541,93.55875,92.08834,92.82,92.51041,91.995,92.49875],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":16.97056299483066},{"case":"5 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":124.82958,"min_ns":124.21875,"max_ns":127.21334,"samples_ns":[124.58417,124.82958,124.28334,126.40375,127.21334,124.21875,124.94,124.96,124.67459],"memory":{"peak_extra_bytes":376,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":78.78875,"min_ns":78.48166,"max_ns":79.78792,"samples_ns":[78.76792,79.78792,79.22375,78.48792,79.56083,78.53666,78.78875,78.48166,78.88083],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":36.88294873699007},{"case":"20 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":115.16959,"min_ns":114.75375,"max_ns":116.64417,"samples_ns":[116.39208,114.76,115.7975,115.44792,114.89709,116.64417,115.16959,114.75375,115.04708],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":89.9725,"min_ns":89.71458,"max_ns":90.52916,"samples_ns":[90.38541,89.82084,89.79459,89.71458,90.21333,89.9725,89.96625,90.52916,90.51041],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":21.87824928438141},{"case":"20 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":245.47083,"min_ns":244.27292,"max_ns":246.97708,"samples_ns":[245.32,245.7225,244.27292,245.79125,246.65833,244.4375,245.47083,245.06,246.97708],"memory":{"peak_extra_bytes":1336,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":82.84541,"min_ns":81.99542,"max_ns":84.62667,"samples_ns":[83.54167,82.15333,82.84541,82.34583,82.03083,83.05708,84.62667,83.45667,81.99542],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":66.25040539440063},{"case":"20 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":117.90083,"min_ns":116.74584,"max_ns":118.67708,"samples_ns":[118.25792,118.27042,117.90083,118.67708,116.74667,117.56792,116.74584,117.13916,118.59167],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":92.12083,"min_ns":91.87417,"max_ns":93.15875,"samples_ns":[91.89791,92.50417,92.12083,91.97542,92.08417,93.15875,93.09083,92.21625,91.87417],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":21.865834193024767},{"case":"20 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":216.33291,"min_ns":215.09458,"max_ns":222.39208,"samples_ns":[215.09458,222.39208,216.33291,215.2425,216.54708,216.50958,215.59583,216.18125,218.42833],"memory":{"peak_extra_bytes":1336,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":78.33542,"min_ns":77.71666,"max_ns":79.44791,"samples_ns":[79.44791,78.1225,77.71666,78.79084,79.05583,78.33542,77.77459,77.90917,79.435],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":63.789411421498464},{"case":"100 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":161.89708,"min_ns":161.18584,"max_ns":163.20583,"samples_ns":[161.18584,162.24917,163.00459,163.20583,161.43708,161.25708,161.89708,163.02,161.63125],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":88.60166,"min_ns":87.98625,"max_ns":89.39792,"samples_ns":[87.98625,88.62167,88.37958,88.96333,88.1375,89.06333,89.39792,88.60166,88.01625],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":45.272848651748376},{"case":"100 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":668.93458,"min_ns":667.835,"max_ns":672.47,"samples_ns":[668.72875,668.44916,670.61458,670.69,672.42709,668.93458,667.835,668.89125,672.47],"memory":{"peak_extra_bytes":8248,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":82.27208,"min_ns":81.87417,"max_ns":83.64833,"samples_ns":[82.27208,83.64459,83.45208,82.03791,82.16292,81.87417,82.29625,83.64833,82.0275],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":87.70102750555967},{"case":"100 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":164.62625,"min_ns":163.60875,"max_ns":173.89708,"samples_ns":[164.36834,165.29542,173.89708,164.62625,163.91917,163.84167,165.98625,164.85917,163.60875],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":91.12875,"min_ns":90.33375,"max_ns":93.18417,"samples_ns":[91.55875,91.11209,91.93208,90.33375,91.12875,90.48166,90.42458,93.18417,91.72583],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":44.64506723563223},{"case":"100 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":645.7025,"min_ns":638.74875,"max_ns":680.65708,"samples_ns":[680.65708,655.63583,639.36167,639.71083,638.74875,645.7025,676.33792,647.99292,641.11583],"memory":{"peak_extra_bytes":8248,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":78.83125,"min_ns":77.40333,"max_ns":87.10583,"samples_ns":[87.10583,82.48,77.47167,77.40333,77.44583,79.75875,78.9325,78.61875,78.83125],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":87.79139774121984},{"case":"1000 extra params; match, no overrides","iterations_per_sample":100000,"baseline":{"median_ns":699.13834,"min_ns":697.65083,"max_ns":701.24917,"samples_ns":[698.6675,700.25209,699.80875,699.13834,701.24917,699.6125,698.47708,698.14542,697.65083],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":91.46042,"min_ns":91.01666,"max_ns":92.96916,"samples_ns":[91.34791,92.95083,91.20333,91.01666,91.25833,92.96916,91.57792,91.46042,92.24084],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":86.91812267082935},{"case":"1000 extra params; match, override","iterations_per_sample":100000,"baseline":{"median_ns":5364.15375,"min_ns":5352.6775,"max_ns":5384.63292,"samples_ns":[5352.71375,5352.6775,5375.36792,5373.39292,5363.89,5364.15375,5367.98667,5358.61959,5384.63292],"memory":{"peak_extra_bytes":41016,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":82.735,"min_ns":82.20083,"max_ns":84.5375,"samples_ns":[82.735,82.20083,83.205,82.41708,83.34667,82.425,82.29375,83.5,84.5375],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":98.45763183055668},{"case":"1000 extra params; mismatch","iterations_per_sample":100000,"baseline":{"median_ns":700.975,"min_ns":699.62958,"max_ns":704.85292,"samples_ns":[700.16584,700.74875,700.29875,702.44791,700.975,701.88708,704.85292,699.62958,701.39792],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":94.79584,"min_ns":93.51375,"max_ns":103.95583,"samples_ns":[96.58959,93.76459,98.99291,103.95583,93.51375,93.57125,95.14583,94.79584,93.625],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":86.47657334427048},{"case":"1000 extra params; null override","iterations_per_sample":100000,"baseline":{"median_ns":5320.75584,"min_ns":5299.68,"max_ns":5560.30625,"samples_ns":[5309.02292,5299.68,5319.2775,5361.615,5320.75584,5390.99416,5358.425,5560.30625,5304.86667],"memory":{"peak_extra_bytes":41016,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"candidate":{"median_ns":81.05791,"min_ns":78.3825,"max_ns":83.77583,"samples_ns":[81.07083,79.11166,83.705,83.77583,78.67125,78.5875,81.05791,81.39458,78.3825],"memory":{"peak_extra_bytes":40,"retained_bytes":0,"allocator_peak_extra_bytes":0}},"time_reduction_percent":98.4765715165761}]}

@garak

garak commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

Correct, and the perf case is better than the benchmark shows. Verified by brute force rather than inspection: 16 parameter names (including '', '0', '01', '1.2', 7, null, true), 17 base/override param sets, 11 key shapes, 50,864 combinations. Zero mismatches.

Two things that look risky but check out:

  • isset() => null !== $value is faithful, because array_merge lets a later null overwrite an earlier value and isset() then returns false. The explicit-null-override case already behaved this way.
  • The numeric guard is sound. Every string key PHP canonicalises to an int (decimal, optional minus, no leading zeros or spaces) is is_numeric() === true, so the guard is a strict superset of the keys reindexing can affect. '01', '1.2' and ' 5' take the slow path needlessly but stay correct.

Worth noting for anyone judging the benchmark: Processor::sortable() already does array_merge($pagination->getParams(), $params) at src/Helper/Processor.php:78, then calls isSorted($key, $params) at line 89. That is the only caller in the bundle, and the templates receive sorted from it rather than calling isSorted() themselves. So the sole real caller always hits the redundant second merge, which is exactly the "match, override" case measured at 32-98%, once per sortable column header. Side effect: the $this->params[$name] ?? null fallback is dead on that path and only matters for direct calls.

Three things before this goes in:

  • Drop tests/Benchmark/SortParameter.php. Same problem as Compute pagination range boundaries from endpoints #863: PHPUnit skips it (phpunit.xml filters on suffix="Test.php"), PHPStan does not see it (paths: [src]), and it needs a hand-built baseline in /tmp. The two benchmark files also share ~90% identical scaffolding, so landing both PRs leaves two copies of a harness nobody runs.
  • Reconsider the numeric branch. getSort(), getDirection() and getPage() all read $this->params[$name] directly with no merge, so they never see reindexing. A numeric sort_field_name therefore already makes getSort() and isSorted() disagree. The branch locks that in and adds a second copy of the is_array($key) => implode('+') normalisation, so there are now two places to keep in sync. sort_field_name is a scalarNode defaulting to 'sort'; dropping the merge outright gives one code path consistent with the other three accessors.
  • The redundant merging is upstream too. Processor::sortable() merges at line 78, isSorted() merges again, and there is a third merge further down the same method. This removes one of three.

The tests here are good, and a clear step up from #863: explicit expectations, and the numeric cases deliberately pin the reindexing behaviour instead of recomputing the implementation.

@garak
garak merged commit da1774d into KnpLabs:master Oct 30, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants