Skip to content

Commit 7f2a64c

Browse files
Complete rework of pre-allocated static buffer strategy. No buffers are being created more than once
1 parent 7162cb3 commit 7f2a64c

30 files changed

+512
-364
lines changed

.github/workflows/linux.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: Linux
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ "master" ]
6+
branches: [ "master", "experimental" ]
77
pull_request:
8-
branches: [ "master" ]
8+
branches: [ "master", "experimental" ]
99

1010
env:
1111
BUILD_TYPE: Release

.github/workflows/macOS.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: macOS
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ "master" ]
6+
branches: [ "master", "experimental" ]
77
pull_request:
8-
branches: [ "master" ]
8+
branches: [ "master", "experimental" ]
99

1010
jobs:
1111
build:

.github/workflows/static_analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: Static Analysis
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ "master" ]
6+
branches: [ "master", "experimental" ]
77
pull_request:
8-
branches: [ "master" ]
8+
branches: [ "master", "experimental" ]
99

1010
env:
1111
BUILD_TYPE: Release

.github/workflows/windows.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: Windows
33
on:
44
workflow_dispatch:
55
push:
6-
branches: [ "master" ]
6+
branches: [ "master", "experimental" ]
77
pull_request:
8-
branches: [ "master" ]
8+
branches: [ "master", "experimental" ]
99

1010
env:
1111
BUILD_TYPE: Release

Sandbox/Sandbox.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
static size_t allocation_count = 0;
66

77
// Override global new operator
8-
void* operator new(size_t size)
8+
void* operator new(size_t size)
99
{
1010
++allocation_count;
1111
return std::malloc(size); // Use malloc for allocation
@@ -15,23 +15,23 @@ int main()
1515
{
1616
using logger_config = lwlog::configuration<
1717
lwlog::disable_local_time,
18-
lwlog::disable_thread_id,
18+
lwlog::disable_thread_id,
1919
lwlog::disable_process_id,
2020
lwlog::disable_topics
2121
>;
2222

2323
auto console = std::make_shared<
2424
lwlog::logger<
25-
lwlog::default_config,
26-
lwlog::default_memory_buffer_limits,
27-
lwlog::asynchronous_policy<
28-
lwlog::default_overflow_policy,
29-
lwlog::default_async_queue_size,
30-
lwlog::default_thread_affinity
31-
>,
32-
lwlog::immediate_flush_policy,
33-
lwlog::single_threaded_policy,
34-
lwlog::sinks::stdout_sink
25+
lwlog::default_config,
26+
lwlog::default_memory_buffer_limits,
27+
lwlog::asynchronous_policy<
28+
lwlog::default_overflow_policy,
29+
lwlog::default_async_queue_size,
30+
lwlog::default_thread_affinity
31+
>,
32+
lwlog::immediate_flush_policy,
33+
lwlog::single_threaded_policy,
34+
lwlog::sinks::stdout_sink
3535
>
3636
>("CONSOLE");
3737

lwlog/src/buffer_limits.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ namespace lwlog
4040
static constexpr std::size_t arg_count{ ArgCountLimit::Value };
4141
static constexpr std::size_t padding{ PaddingLimit::Value };
4242
static constexpr std::size_t conversion{ ConvLimit::Value };
43+
44+
static constexpr std::size_t pool_size{ 8 };
45+
4346
};
4447

45-
using default_pattern_limit = pattern_limit<512>;
46-
using default_message_limit = message_limit<256>;
47-
using default_argument_limit = argument_limit<24>;
48-
using default_arg_count_limit = arg_count_limit<10>;
48+
using default_pattern_limit = pattern_limit<256>;
49+
using default_message_limit = message_limit<128>;
50+
using default_argument_limit = argument_limit<12>;
51+
using default_arg_count_limit = arg_count_limit<4>;
4952
using default_padding_limit = padding_limit<24>;
5053
using default_conv_limit = conv_limit<64>;
5154

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
namespace lwlog::details
4+
{
5+
template<typename BufferLimits>
6+
class argument_buffers_pool
7+
{
8+
public:
9+
argument_buffers_pool();
10+
std::uint8_t acquire_args_buffer();
11+
void release_args_buffer(std::uint8_t idx);
12+
char(*get_args_buffer(std::uint8_t idx))[BufferLimits::arg_count][BufferLimits::argument];
13+
14+
private:
15+
char m_args_buffers[BufferLimits::pool_size][BufferLimits::arg_count][BufferLimits::argument];
16+
std::uint8_t m_args_buffers_free_indices[BufferLimits::pool_size];
17+
std::atomic<std::uint8_t> m_args_buffers_free_top;
18+
};
19+
}
20+
21+
#include "argument_buffers_pool_impl.h"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
namespace lwlog::details
4+
{
5+
template<typename BufferLimits>
6+
argument_buffers_pool<BufferLimits>::argument_buffers_pool()
7+
: m_args_buffers_free_top{ BufferLimits::pool_size }
8+
{
9+
for (std::uint8_t i = 0; i < BufferLimits::pool_size; ++i)
10+
{
11+
m_args_buffers_free_indices[i] = i;
12+
}
13+
}
14+
15+
template<typename BufferLimits>
16+
std::uint8_t argument_buffers_pool<BufferLimits>::acquire_args_buffer()
17+
{
18+
std::uint8_t old_top{ m_args_buffers_free_top.load(std::memory_order_acquire) };
19+
while (old_top > 0)
20+
{
21+
if (m_args_buffers_free_top.compare_exchange_weak(old_top, old_top - 1, std::memory_order_acq_rel))
22+
{
23+
return m_args_buffers_free_indices[old_top - 1];
24+
}
25+
}
26+
27+
return 0;
28+
}
29+
30+
template<typename BufferLimits>
31+
void argument_buffers_pool<BufferLimits>::release_args_buffer(std::uint8_t idx)
32+
{
33+
std::uint8_t old_top{ m_args_buffers_free_top.load(std::memory_order_acquire) };
34+
while (old_top < static_cast<std::uint8_t>(BufferLimits::pool_size))
35+
{
36+
if (m_args_buffers_free_top.compare_exchange_weak(old_top, old_top + 1, std::memory_order_acq_rel))
37+
{
38+
m_args_buffers_free_indices[old_top] = idx;
39+
break;
40+
}
41+
}
42+
}
43+
template<typename BufferLimits>
44+
char(*argument_buffers_pool<BufferLimits>::get_args_buffer(std::uint8_t idx))[BufferLimits::arg_count][BufferLimits::argument]
45+
{
46+
return &m_args_buffers[idx];
47+
}
48+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)