Skip to content

explore boost's flat_map #8076

Closed
Closed

Description

@cbielow

to increase performance for

and increase merging performance for existing flat_maps

flat_maps have .merge(), but with non-const RHS...

Alternative: Effective merging draft (more efficient than flat_map1.insert(flat_map2.begin(), flat_map2.end()):

#include <boost/container/flat_map.hpp>
#include <vector>
#include <iostream>
#include <iterator> // for std::make_move_iterator

int main() {
    using namespace boost::container;
    using pair_type = std::pair<int, std::string>;

    flat_map<int, std::string> a = {
        {1, "one"},
        {2, "two"}
    };

    flat_map<int, std::string> b = {
        {2, "zwei"},
        {3, "three"}
    };

    std::vector<pair_type> merged;
    merged.reserve(a.size() + b.size());

    auto ita = a.begin(), enda = a.end();
    auto itb = b.begin(), endb = b.end();

    // Merge with deduplication: keep values from `a`
    while (ita != enda && itb != endb) {
        if (ita->first < itb->first) {
            merged.push_back(*ita++);
        } else if (itb->first < ita->first) {
            merged.push_back(*itb++);
        } else {
            merged.push_back(*ita++); // prefer a
            ++itb;
        }
    }
    merged.insert(merged.end(), ita, enda);
    merged.insert(merged.end(), itb, endb);

    // Move elements into the new flat_map
    flat_map<int, std::string> result(
        std::make_move_iterator(merged.begin()),
        std::make_move_iterator(merged.end())
    );

    // Print result
    for (const auto& [k, v] : result) {
        std::cout << k << ": " << v << '\n';
    }

    return 0;
}

Activity

  1. timosachsenberg commented on Jul 2, 2025

    @timosachsenberg
    Contributor

    boost 1.80 did quite a lot of rewrites to its hash maps (especially unordered ones with open addressing)

  2. github-actions commented on Jan 9, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  3. timosachsenberg commented on Jan 9, 2026

    @timosachsenberg
    Contributor

    I will assign this to Claude. Let's see if something useful comes out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions