<random>: Avoid copying vectors in distribution comparisons - #6454
Open
A. Jiang (frederick-vs-ja) wants to merge 1 commit into
Open
A. Jiang (frederick-vs-ja) wants to merge 1 commit into
A. Jiang (frederick-vs-ja) wants to merge 1 commit into
Conversation
It is just unreasonable to perform dynamic allocation in such comparisons. Ideally, these `operator==`s (and corresponding `operator!=` until C++20) should also be `noexcept`, but we haven't strengthened exception specification for `vector`'s `operator==` yet.
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
Copilot started reviewing on behalf of
A. Jiang (frederick-vs-ja)
September 17, 2026 07:27
View session
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The direct comparisons preserve behavior while eliminating unnecessary allocations.
Pull request overview
Optimizes random distribution equality comparisons by avoiding temporary param_type objects and their vector allocations.
Changes:
- Compares stored parameter objects directly for three vector-backed distributions.
- Preserves existing equality semantics.
File summaries
| File | Description |
|---|---|
stl/inc/random |
Avoids parameter-vector copies in distribution comparisons. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Stephan T. Lavavej (StephanTLavavej)
requested changes
Sep 18, 2026
|
|
||
| _NODISCARD friend bool operator==(const discrete_distribution& _Left, const discrete_distribution& _Right) { | ||
| return _Left.param() == _Right.param(); | ||
| return _Left._Par == _Right._Par; |
There was a problem hiding this comment.
These might be the only ones where it matters in release mode, but IMO I think we should now be implementing all of the distribution operator== by directly inspecting _Par instead of calling param(). It's more consistent throughout the file, and happens to improve debugging and debug codegen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It is just unreasonable to perform dynamic allocation in such comparisons.
Ideally, these
operator==s (and correspondingoperator!=until C++20) should also benoexcept, but we haven't strengthened exception specification forvector'soperator==yet.