Remove unnecessary copies flagged by static analysis - #13591
Draft
bryancall wants to merge 2 commits into
Draft
Conversation
Add std::move where a local's last use was a copy into a container or a by-value parameter, and bind a few config-parse locals by const reference where the expression already returns a reference into longer-lived storage. Each site was checked individually: the moved-from objects are never read again, and the const-reference binds point into yaml-cpp node storage owned by the document's shared memory holder rather than into the temporary Node handle, so they cannot dangle. Findings in test helpers and a few sites where the copy was either required or not actually a copy were left alone.
GCC's -Wdangling-reference cannot prove that the reference returned by YAML::Node::Scalar() does not point into the temporary Node returned by operator[], and the tree builds with -Werror. The copies these avoided are small and the analysis findings behind them are not worth a suppression, so keep the by-value locals.
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.
Static analysis flagged a set of unnecessary copies. This fixes the ones that are real and leaves the ones that are not.
What changed
std::moveon a local's last use inplugins/header_rewrite/parser.cc,plugins/cachekey/pattern.cc, andplugins/experimental/rate_limit/limiter.h, where a localstd::string(or an element of a local vector that is about to be destroyed) was copied into a container or into a by-value parameter.autotoauto const &insrc/proxy/http/remap/NextHopSelectionStrategy.ccandNextHopConsistentHash.cc, whereYAML::Node::Scalar()already returnsconst std::string &and the result was being copied into a local that is only read.15 changes across 5 files.
Why these and not the others
Every site was checked individually rather than applied in bulk, and most of the reported findings were left alone:
parser.ccthetokensvector is a by-value parameter (the caller deliberately passes a copy), so moving out of its elements cannot be observed by the caller.Node::Scalar()returns a reference intodetail::nodestorage owned by the document's shared memory holder, not into the temporaryNodehandle returned byoperator[], and theMap ¶meter keeps that holder alive for the whole function.std::string_view, or where the expression returns by value so a const-reference bind would remove no copy at all, were skipped as false positives.A larger group of
Big parameter passed by valuefindings on this same code was deliberately not addressed. Those are almost entirelyConfigContextandYAML::Node, which are reference-counted handles that a size-based heuristic flags bysizeof.ConfigContextdocuments at its declaration that copies are intentional and that move is suppressed sostd::movesilently copies, whichexecute_reload()depends on;const YAML::Node &would also changeoperator[]semantics, since the const overload does not create missing keys. Converting them would risk a silent config-parsing change for no measurable gain on a path that runs a handful of times per reload.Testing
Builds clean with experimental plugins enabled. All 166 unit tests pass.