Skip to content

LWG-4490 Allow std ranges size in std ranges count - #6508

Open
Syed Maroof Hussain (smhussa22) wants to merge 2 commits into
microsoft:mainfrom
smhussa22:allow-std-ranges-size-in-std-ranges-count
Open

Syed Maroof Hussain (smhussa22) wants to merge 2 commits into
microsoft:mainfrom
smhussa22:allow-std-ranges-size-in-std-ranges-count

Conversation

@smhussa22

Copy link
Copy Markdown

Disclosure

Extension of/implements part of #6320

Intention of this PR

The wording changes in LWG-4490 allow for range overloads of algorithms to calculate the end of a range as begin + size, instead of a full walk to it.

This PR intends to take advantage of this only for std::ranges::count, and is related to #6477, which has attempted this for some other ranges algorithms.

PR summary/explanation

The implementation for std::ranges::count already has a fast vectorized path, but it only runs when the end of a range can be subtracted from the beginning.

Ranges that know their size, but whose end is not a sized sentinel, could only use the non-vectorized version which would simply do a walk of the entire range one element at a time.

However, LWG-4490 changed the wording such that now the end can be calculated as begin + size.

Thus, this PR changes std::ranges::count to calculate the end as begin + size when a range:

  1. Knows its own size
  2. Is random access
  3. Has an end who is not a sized sentinel

If any of the three conditions above are false, this change doesn't apply and the range behaves as it did before. Ranges who's end is already a sized sentinel keep using the vectorized path.

Benchmarks and tests

This PR has added a benchmark that compares the speeds of std::ranges::count on two ranges holding the exact same data:

  1. A plain vector (already uses the vectorized path)
  2. A test range that knows its size, but whose end is not a sized sentinel

Note that range 2 would have to use the non-vectorized path prior to the changes, but now qualifies for the vectorized path after the changes.

I ran this benchmark five times for both before and after the change, and the following table shows the medians of each of those runs:

Elements Range 2 before Range 2 after Speedup vector before vector after
7 6.17 ns 5.39 ns 1.1x 5.11 ns 5.29 ns
63 34.8 ns 5.90 ns 5.9x 5.86 ns 5.97 ns
512 238 ns 36.9 ns 6.4x 36.4 ns 41.2 ns
8,021 3,748 ns 470 ns 8.0x 461 ns 464 ns
65,536 31,418 ns 3,993 ns 7.9x 3,963 ns 4,015 ns
1,048,576 495,362 ns 71,802 ns 6.9x 72,061 ns 69,545 ns

Noticeably, range 2 runs at about the same speed as a plain vector and has experienced a speedup overall.

vector hasn't experienced any change (apart from usual variability), which is to be expected.

This PR also extends an existing test for std::ranges::count, with a test range that knows its size but who's end is not a sized sentinel, and checks:

  1. The result of std::ranges::count is still correct
  2. The end is never compared, confirming the calculated end is being used

This test fails without the changes made in this PR, because the end is compared while walking the range. It passes with the changesmade in this PR.

Copilot AI balanced review requested due to automatic review settings November 7, 2026 03:28
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Nov 7, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@smhussa22

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the change enabling vectorized std::ranges::count on eligible sized ranges with unsized sentinels.

No open issues 🎉

What changed in this PR

Implements part of LWG-4490 (#6320) for std::ranges::count, allowing sized ranges with random-access iterators and unsized sentinels to use the existing vectorized path.

Changes:

  • Calculates the end iterator from the range’s size when eligible, preserving the existing path otherwise.
  • Adds compile-time and runtime tests for correct counts and avoidance of sentinel comparisons.
  • Adds a benchmark comparing an eligible unsized-sentinel range with a plain vector.
File Description
tests/​std/​tests/​P0896R4_ranges_alg_count/​test.cpp Tests sized ranges with unsized sentinels, including projections and varying lengths.
stl/​inc/​algorithm Computes an iterator endpoint from the size for eligible ranges::count calls.
benchmarks/​src/​ranges_count_sized_range_unsized_sentinel.cpp Compares vector and unsized-sentinel range counting performance.
benchmarks/​CMakeLists.txt Registers the new benchmark target.

Review effort: Balanced

@smhussa22 Syed Maroof Hussain (smhussa22) changed the title Allow std ranges size in std ranges count LWG-4490 Allow std ranges size in std ranges count Nov 7, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

/azp run STL-CI

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Must go faster ranges C++20/23 ranges

Projects

Status: Maintainer Review

Development

Successfully merging this pull request may close these issues.

3 participants