diff --git a/experimental/extrinsic_storage.h b/experimental/extrinsic_storage.h index a1d1822772..719b98ea92 100644 --- a/experimental/extrinsic_storage.h +++ b/experimental/extrinsic_storage.h @@ -8,25 +8,21 @@ #ifndef CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_H #define CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_H - // ***************************************************************** // Enable/disable debug instrumentation and statistics printing here -constexpr auto debug_instrumentation = true; +constexpr inline auto debug_instrumentation = false; // ***************************************************************** - #include #include #include #include #include -#include #include +#include #include #include -#include #include -#include #include #include diff --git a/experimental/extrinsic_storage_std_locked.h b/experimental/extrinsic_storage_std_locked.h new file mode 100644 index 0000000000..bfbea55336 --- /dev/null +++ b/experimental/extrinsic_storage_std_locked.h @@ -0,0 +1,76 @@ + +// Copyright 2022-2024 Herb Sutter +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://github.com/hsutter/cppfront/blob/main/LICENSE for license information. + +#ifndef CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_STD_LOCKED_H +#define CPP2_EXPERIMENTAL_EXTRINSIC_STORAGE_STD_LOCKED_H + +#include +#include +#include +#include + + +//----------------------------------------------------------------------------------- +// Some helpers +// +auto print(std::integral auto val) -> std::string { + auto ret = std::to_string(val % 10); + auto pos = 0; + while ((val /= 10) > 0) { + if ((++pos % 3) == 0) { ret = ',' + ret; } + ret = std::to_string(val % 10) + ret; + } + return ret; +} + + +//----------------------------------------------------------------------------------- +// A "brute-force" locked implementation to measure against +// +// NOTE: For performance comparison only, not recommended +// +template +class extrinsic_storage { + std::mutex mut; + //std::map data; + std::unordered_map data; +public: + //-------------------------------------------------------------------------- + // find_or_insert( pobj ) - returns the data entry for pobj + // + // If pobj does not yet have an entry, creates it + // + auto find_or_insert(void* pobj) -> Data* { + auto _ = std::lock_guard{mut}; + return &data[pobj]; + } + + //-------------------------------------------------------------------------- + // find( pobj ) - returns the data entry for pobj or null if not present + // + auto find(void* pobj) noexcept -> Data* { + auto _ = std::lock_guard{mut}; + if (auto iter = data.find(pobj); + iter != data.end() + ) + { + return &iter->second; + } + // Else + return nullptr; + } + + //-------------------------------------------------------------------------- + // erase( pobj ) - removes the entry for pobj + // + auto erase(void* pobj) noexcept -> void { + auto _ = std::lock_guard{mut}; + data.erase(pobj); + } +}; + +#endif diff --git a/experimental/union_test.cpp b/experimental/union_test.cpp index 77d5365a9d..33e2fff1f3 100644 --- a/experimental/union_test.cpp +++ b/experimental/union_test.cpp @@ -5,10 +5,14 @@ // Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions. // See https://github.com/hsutter/cppfront/blob/main/LICENSE for license information. +//#include "extrinsic_storage_std_locked.h" #include "extrinsic_storage.h" #include #include +#include +#include +#include #include #include