* Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <optional>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "bench_harness.h"
#include "ibmw_selfdev/concurrent/concurrent_hash_map.h"
#include "oneapi/tbb/concurrent_hash_map.h"
namespace {
namespace bench = ibmw::selfdev::bench;
struct Value {
uint64_t v[8];
};
static_assert(sizeof(Value) == 64);
Value MakeValue(uint32_t k)
{
Value v{};
v.v[0] = k;
v.v[7] = k * 7u;
return v;
}
struct SelfdevMap {
ibmw::selfdev::concurrent::ConcurrentHashMap<uint32_t, Value> m;
bool Emplace(uint32_t k, const Value& v) { return m.Emplace(k, v); }
std::optional<Value> Take(uint32_t k) { return m.Take(k); }
bool Contains(uint32_t k) const { return m.Contains(k); }
std::optional<Value> Get(uint32_t k) const { return m.Get(k); }
};
struct TbbMap {
using Map = tbb::concurrent_hash_map<uint32_t, Value>;
Map m;
bool Emplace(uint32_t k, const Value& v) { return m.emplace(k, v); }
std::optional<Value> Take(uint32_t k)
{
Map::accessor ac;
if (!m.find(ac, k)) {
return std::nullopt;
}
auto out = std::make_optional<Value>(std::move(ac->second));
m.erase(ac);
return out;
}
bool Contains(uint32_t k) const { return m.count(k) != 0; }
std::optional<Value> Get(uint32_t k) const
{
Map::const_accessor ac;
if (!m.find(ac, k)) {
return std::nullopt;
}
return ac->second;
}
};
struct XorShift {
uint64_t s;
uint32_t Next()
{
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
return static_cast<uint32_t>(s);
}
};
constexpr uint32_t kPrefillBase = 0x80000000u;
template <class M>
void Prefill(M& m, uint32_t live)
{
for (uint32_t i = 0; i < live; ++i) {
m.Emplace(kPrefillBase + i, MakeValue(i));
}
}
template <class Fn>
double RunThreads(int threads, Fn fn)
{
std::atomic<int> ready{0};
std::atomic<bool> go{false};
std::vector<std::thread> ts;
for (int t = 0; t < threads; ++t) {
ts.emplace_back([&ready, &go, &fn, t] {
ready.fetch_add(1);
while (!go.load(std::memory_order_acquire)) {
}
fn(t);
});
}
while (ready.load() != threads) {
}
const bench::Stopwatch sw;
go.store(true, std::memory_order_release);
for (auto& th : ts) {
th.join();
}
return sw.ElapsedNs();
}
template <class M>
bench::Sample EmplaceTakeDisjoint(int threads, uint32_t live, uint64_t pairsPerThread)
{
constexpr uint64_t kWindow = 8;
M m;
Prefill(m, live);
const double ns = RunThreads(threads, [&pairsPerThread, &m, &threads](int t) {
auto key = [&threads, &t](uint64_t i) {
return static_cast<uint32_t>(i * static_cast<uint64_t>(threads) + static_cast<uint64_t>(t));
};
uint64_t sink = 0;
for (uint64_t i = 0; i < pairsPerThread + kWindow; ++i) {
if (i < pairsPerThread) {
m.Emplace(key(i), MakeValue(key(i)));
}
if (i >= kWindow) {
auto v = m.Take(key(i - kWindow));
sink += v ? v->v[0] : 0;
}
}
bench::DoNotOptimize(sink);
});
return {pairsPerThread * static_cast<uint64_t>(threads), ns};
}
template <class M>
bench::Sample EmplaceTakeOverlap(int threads, uint32_t live, uint64_t pairsPerThread)
{
M m;
Prefill(m, live);
const double ns = RunThreads(threads, [&pairsPerThread, &m](int t) {
XorShift rng{0x9e3779b97f4a7c15ULL * static_cast<uint64_t>(t + 1)};
uint64_t sink = 0;
for (uint64_t i = 0; i < pairsPerThread; ++i) {
const uint32_t k = rng.Next() & 1023u;
m.Emplace(k, MakeValue(k));
auto v = m.Take(rng.Next() & 1023u);
sink += v ? v->v[0] : 0;
}
bench::DoNotOptimize(sink);
});
return {pairsPerThread * static_cast<uint64_t>(threads), ns};
}
template <class M>
bench::Sample ReadMostly(int threads, uint32_t live, uint64_t opsPerThread)
{
M m;
Prefill(m, live);
const double ns = RunThreads(threads, [&opsPerThread, &live, &m](int t) {
XorShift rng{0xd1b54a32d192ed03ULL * static_cast<uint64_t>(t + 1)};
uint64_t sink = 0;
uint32_t own = static_cast<uint32_t>(t) << 20;
for (uint64_t i = 0; i < opsPerThread; ++i) {
const uint32_t r = rng.Next();
const uint32_t pick = r % 100u;
const uint32_t k = kPrefillBase + (r >> 8) % live;
if (pick < 45) {
sink += m.Contains(k) ? 1 : 0;
} else if (pick < 90) {
auto v = m.Get(k);
sink += v ? v->v[7] : 0;
} else {
m.Emplace(own, MakeValue(own));
auto v = m.Take(own);
sink += v ? v->v[0] : 0;
++own;
}
}
bench::DoNotOptimize(sink);
});
return {opsPerThread * static_cast<uint64_t>(threads), ns};
}
}
int main(int argc, char** argv)
{
bench::Runner r(bench::ParseOptions(argc, argv));
for (uint32_t live : {100u, 100'000u}) {
const std::string sz = live == 100 ? "live100" : "live100k";
for (int t : {1, 4, 8, 16}) {
const std::string th = std::to_string(t) + "T";
const uint64_t per = r.N(2'000'000) / static_cast<uint64_t>(t);
r.Compare(
"map/emplace+take/disjoint/" + sz + "/" + th,
[&t, &live, &per] { return EmplaceTakeDisjoint<SelfdevMap>(t, live, per); },
[&t, &live, &per] { return EmplaceTakeDisjoint<TbbMap>(t, live, per); }, "tbb");
r.Compare(
"map/emplace+take/overlap1024/" + sz + "/" + th,
[&t, &live, &per] { return EmplaceTakeOverlap<SelfdevMap>(t, live, per); },
[&t, &live, &per] { return EmplaceTakeOverlap<TbbMap>(t, live, per); }, "tbb");
r.Compare(
"map/read-mostly 90%/" + sz + "/" + th,
[&t, &live, &per] { return ReadMostly<SelfdevMap>(t, live, per * 2); },
[&t, &live, &per] { return ReadMostly<TbbMap>(t, live, per * 2); }, "tbb");
}
}
return 0;
}