Skip to content

Commit bbee890

Browse files
committed
For union, add naive mutex-locked std::[unordered_]map alternative to measure
1 parent 7beaeb4 commit bbee890

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

experimental/extrinsic_storage.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,21 @@
88
#ifndef CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_H
99
#define CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_H
1010

11-
1211
// *****************************************************************
1312
// Enable/disable debug instrumentation and statistics printing here
14-
constexpr auto debug_instrumentation = true;
13+
constexpr inline auto debug_instrumentation = false;
1514
// *****************************************************************
1615

17-
1816
#include <algorithm>
1917
#include <array>
2018
#include <atomic>
2119
#include <cassert>
2220
#include <cstdint>
23-
#include <fstream>
2421
#include <functional>
22+
#include <iostream>
2523
#include <memory>
2624
#include <map>
27-
#include <iostream>
2825
#include <ranges>
29-
#include <source_location>
3026
#include <string>
3127
#include <type_traits>
3228

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
// Copyright 2022-2024 Herb Sutter
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://github.com/hsutter/cppfront/blob/main/LICENSE for license information.
7+
8+
#ifndef CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_STD_LOCKED_H
9+
#define CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_STD_LOCKED_H
10+
11+
#include <map>
12+
#include <mutex>
13+
#include <string>
14+
#include <unordered_map>
15+
16+
17+
//-----------------------------------------------------------------------------------
18+
// Some helpers
19+
//
20+
auto print(std::integral auto val) -> std::string {
21+
auto ret = std::to_string(val % 10);
22+
auto pos = 0;
23+
while ((val /= 10) > 0) {
24+
if ((++pos % 3) == 0) { ret = ',' + ret; }
25+
ret = std::to_string(val % 10) + ret;
26+
}
27+
return ret;
28+
}
29+
30+
31+
//-----------------------------------------------------------------------------------
32+
// A "brute-force" locked implementation to measure against
33+
//
34+
// NOTE: For performance comparison only, not recommended
35+
//
36+
template <typename Data>
37+
class extrinsic_storage {
38+
std::mutex mut;
39+
//std::map<void*,Data> data;
40+
std::unordered_map<void*,Data> data;
41+
public:
42+
//--------------------------------------------------------------------------
43+
// find_or_insert( pobj ) - returns the data entry for pobj
44+
//
45+
// If pobj does not yet have an entry, creates it
46+
//
47+
auto find_or_insert(void* pobj) -> Data* {
48+
auto _ = std::lock_guard{mut};
49+
return &data[pobj];
50+
}
51+
52+
//--------------------------------------------------------------------------
53+
// find( pobj ) - returns the data entry for pobj or null if not present
54+
//
55+
auto find(void* pobj) noexcept -> Data* {
56+
auto _ = std::lock_guard{mut};
57+
if (auto iter = data.find(pobj);
58+
iter != data.end()
59+
)
60+
{
61+
return &iter->second;
62+
}
63+
// Else
64+
return nullptr;
65+
}
66+
67+
//--------------------------------------------------------------------------
68+
// erase( pobj ) - removes the entry for pobj
69+
//
70+
auto erase(void* pobj) noexcept -> void {
71+
auto _ = std::lock_guard{mut};
72+
data.erase(pobj);
73+
}
74+
};
75+
76+
#endif

experimental/union_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
// Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.
66
// See https://github.com/hsutter/cppfront/blob/main/LICENSE for license information.
77

8+
//#include "extrinsic_storage_std_locked.h"
89
#include "extrinsic_storage.h"
910

1011
#include <chrono>
1112
#include <cstdint>
13+
#include <fstream>
14+
#include <iostream>
15+
#include <source_location>
1216
#include <thread>
1317
#include <vector>
1418

0 commit comments

Comments
 (0)