Skip to content

forward mapped value in object::insert_or_assign assign branch - #1186

Open
Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:insert-or-assign-move
Open

forward mapped value in object::insert_or_assign assign branch#1186
Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:insert-or-assign-move

Conversation

@Ramya-9353

Copy link
Copy Markdown
Contributor

Repro: insert_or_assign(key, std::move(v)) on an existing key, with v on the object's own resource, deep-copies v and leaves it intact instead of moving it. For a 100-element array the assign branch runs 101 allocations and the source array is still populated afterwards.
Cause: the assign branch casts the mapped value to M rather than M&&, so for an rvalue argument it selects value(value const&), which allocates a copy on the source's storage before the copy into sp_. The insert branch one line above already forwards with M&&.
Fix: cast to M&& to match the insert branch. The stored value is unchanged; the redundant copy and the ignored move go away.

@cppalliance-bot

Copy link
Copy Markdown

An automated preview of the documentation is available at https://1186.json.prtest2.cppalliance.org/libs/json/doc/html/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-08-15 12:04:03 UTC

@cppalliance-bot

Copy link
Copy Markdown

GCOVR code coverage report https://1186.json.prtest2.cppalliance.org/gcovr/index.html
LCOV code coverage report https://1186.json.prtest2.cppalliance.org/genhtml/index.html
Coverage Diff Report https://1186.json.prtest2.cppalliance.org/diff-report/index.html

Build time: 2026-08-15 12:18:33 UTC

@codecov

codecov Bot commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.71%. Comparing base (e2f976c) to head (d6c4e06).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1186   +/-   ##
========================================
  Coverage    93.71%   93.71%           
========================================
  Files           85       85           
  Lines         8971     8971           
========================================
  Hits          8407     8407           
  Misses         564      564           
Files with missing lines Coverage Δ
include/boost/json/impl/object.hpp 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e2f976c...d6c4e06. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cppalliance-bot

Copy link
Copy Markdown

@grisumbras

Copy link
Copy Markdown
Member

Ouch. Thanks for catching this.

Comment thread test/object.cpp
value v = { 4, 5, 6 };
o.insert_or_assign("k2", std::move(v));
BOOST_TEST(o.at("k2") == (array{4, 5, 6}));
BOOST_TEST(v.as_array().empty());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think, a better check for whether v was actually moved is storing v.as_array().data() and comparing it to o["k2"].as_array().data().

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.

3 participants