Skip to content

Commit 99bb731

Browse files
committed
Refactor TempBuffer to use static thread-local cache per template instantiation to fix segmentation fault in LayerTest.TempBufferCorrectness
1 parent 37d5883 commit 99bb731

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

include/neuralnetwork/common/tempbuffer.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
#pragma once
22
#include <vector>
33
#include <algorithm>
4+
#include "../libraries/instrumentor.h"
45

56
namespace myoddweb::nn
67
{
7-
struct ThreadBufferCache
8-
{
9-
std::vector<double> caches[10];
10-
};
11-
12-
inline ThreadBufferCache& get_thread_buffer_cache() noexcept
13-
{
14-
static thread_local ThreadBufferCache instance;
15-
return instance;
16-
}
17-
188
template <typename T, int Tag = 0>
199
class TempBuffer
2010
{
@@ -24,10 +14,11 @@ class TempBuffer
2414
_temp(),
2515
_ptr(nullptr)
2616
{
17+
MYODDWEB_PROFILE_FUNCTION("TempBuffer");
2718
// Capped at 1,048,576 elements (~8MB for double) to prevent TLS bloat
2819
if (size <= 1048576)
2920
{
30-
auto& cache = get_thread_buffer_cache().caches[Tag];
21+
std::vector<T>& cache = get_cache();
3122
if (cache.size() < size)
3223
{
3324
cache.resize(size);
@@ -54,11 +45,12 @@ class TempBuffer
5445

5546
inline void assign(size_t size, const T& val)
5647
{
48+
MYODDWEB_PROFILE_FUNCTION("TempBuffer");
5749
_size = size;
5850
// Capped at 1,048,576 elements (~8MB for double) to prevent TLS bloat
5951
if (size <= 1048576)
6052
{
61-
auto& cache = get_thread_buffer_cache().caches[Tag];
53+
std::vector<T>& cache = get_cache();
6254
if (cache.size() < size)
6355
{
6456
cache.resize(size);
@@ -104,6 +96,13 @@ class TempBuffer
10496
}
10597

10698
private:
99+
static std::vector<T>& get_cache() noexcept
100+
{
101+
MYODDWEB_PROFILE_FUNCTION("TempBuffer");
102+
static thread_local std::vector<T> cache;
103+
return cache;
104+
}
105+
107106
size_t _size;
108107
std::vector<T> _temp;
109108
std::vector<T>* _ptr;

0 commit comments

Comments
 (0)