Skip to content

Commit

Permalink
For union, add naive mutex-locked std::[unordered_]map alternative to…
Browse files Browse the repository at this point in the history
… measure
  • Loading branch information
hsutter committed Jan 3, 2025
1 parent 7beaeb4 commit bbee890
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
8 changes: 2 additions & 6 deletions experimental/extrinsic_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <map>
#include <iostream>
#include <ranges>
#include <source_location>
#include <string>
#include <type_traits>

Expand Down
76 changes: 76 additions & 0 deletions experimental/extrinsic_storage_std_locked.h
Original file line number Diff line number Diff line change
@@ -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 <map>
#include <mutex>
#include <string>
#include <unordered_map>


//-----------------------------------------------------------------------------------
// 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 <typename Data>
class extrinsic_storage {
std::mutex mut;
//std::map<void*,Data> data;
std::unordered_map<void*,Data> 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
4 changes: 4 additions & 0 deletions experimental/union_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <chrono>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <source_location>
#include <thread>
#include <vector>

Expand Down

0 comments on commit bbee890

Please sign in to comment.