Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::move (C++11) in map and set adaptors where possible. #279

Merged
merged 1 commit into from
May 18, 2015

Conversation

jpetso
Copy link
Contributor

@jpetso jpetso commented May 6, 2015

Using std::move for map/set is potentially more efficient (depending on the type), but also allows converting to move-only types where previously the types had to have a copy constructor.

For insert(), moving will actually prevent the copy if possible. This is the important part of the patch.

swap() is very similar to a move assignment, but needs to ensure that the tmp variable will afterwards contain the original contents of the target v variable, which we don't care about but requires a temporary variable for three-way swapping anyway, so it's a bit less optimal.

I made the moves dependent on the standard macro for C++11 since there's no need to depend on any msgpack defines in these cases, and in the cpp11/ directory there are no #ifdefs. I did not adapt any code in the tr1/ directory since the TR1 types did not have move constructors or move assignment operators anyway (move got introduced very late in the standardization process).

}
tmp.swap(v);
v = std::move(v);
Copy link
Contributor

Choose a reason for hiding this comment

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

Some of tests are failed. See https://travis-ci.org/msgpack/msgpack-c/jobs/62302916
It seems that

v = std::move(v)

should be

v = std::move(tmp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, sorry. I only discovered today that I had previously forgotten this occurrence, and apparently exercised less caution on this one. Uploaded an updated commit, the tests are now passing locally for me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for fixing. I'm on a business trip now. I will merge the PR next week.

In the std::map adaptor, remove the complicated iterator/insert
code and go with the cpp11/unordered_map.hpp approach instead.
Makes the code more consistent, avoids an extra copy, and the
previous complexity was unnecessary since std::map only maps to
a single element per key, unlike std::multimap.
redboltz added a commit that referenced this pull request May 18, 2015
Use std::move (C++11) in map and set adaptors where possible.
@redboltz redboltz merged commit 432c9cc into msgpack:master May 18, 2015
@jpetso
Copy link
Contributor Author

jpetso commented May 19, 2015

Thanks!

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