use try_emplace on std::map with c++17 #20500

issue sidhujag opened this issue on November 26, 2020
  1. sidhujag commented at 2:36 AM on November 26, 2020: none

    Let's have a discussion and see if a structure change is warranted to many places std::maps/unsorted_maps are used in the code.

    Now that we support c++17 as a minimum as of #20413 we should consider using try_emplace to gain efficiency when objects are not added, it also leads to more expressive and safer code(prevents stealing from arguments during failed insertion). For example, it seems FetchCoin uses emplace but does a piecewise_construct however it can be replaced with try_emplace:

    CCoinsMap::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;

    becomes CCoinsMap::iterator ret = cacheCoins.try_emplace(outpoint, std::move(tmp)).first;

    I also noticed emplace was used on dir_locks which is a map to unique_ptrs which is generally discouraged. It is used here and should likely just look like this:

    dir_locks[pathLockFile.string()] = std::move(lock));

  2. sidhujag added the label Feature on Nov 26, 2020
  3. sidhujag referenced this in commit c856f37b2c on Nov 26, 2020
  4. bitcoin deleted a comment on Nov 27, 2020
  5. IamShubhamGupto commented at 9:33 AM on May 21, 2021: none

    Hey, id like to work on this

  6. fanquake commented at 2:03 PM on August 14, 2022: member

    Going to close this for now. Feel free to open a PR with code changes. That is likely a better venue for discussion than opening an issue.

  7. fanquake closed this on Aug 14, 2022

  8. bitcoin locked this on Aug 14, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-29 03:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me