Skip to content

Commit a283ae6

Browse files
committed
Switch WAV export to statically allocated log buffer
This reduces performance and memory overhead. The modulo logic is fiddly, but I confirmed it works with various log item counts (by reducing LOG_SIZE to 4, pushing items with and without overflow, and verifying the tail output is correct).
1 parent 028cb90 commit a283ae6

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Source/SoundGen.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "MIDI.h"
5151
#include "ChannelFactory.h" // // // test
5252
#include "DetuneTable.h" // // //
53+
#include <array>
5354
#include <cstdio>
5455
#include <filesystem>
5556
#include <iostream>
@@ -80,27 +81,31 @@ struct LogEntry {
8081

8182
namespace {
8283
class Log {
84+
static constexpr size_t LOG_SIZE = 256;
85+
8386
public:
8487
explicit Log() {}
8588

8689
void log(const char /*'static*/* event) {
8790
auto lock = std::unique_lock(_mtx);
8891

89-
constexpr size_t MAX_ENTRY = 256;
90-
if (_log.size() >= MAX_ENTRY) {
91-
_log.pop_front();
92-
}
93-
94-
//std::time_t result = std::time(nullptr);
9592
const auto tid = GetCurrentThreadId();
9693
const auto child_tid = (tid == theApp.m_nThreadID)
9794
? 0
9895
: tid;
9996

100-
_log.push_back(LogEntry{
97+
_log[wrap(_end2)] = LogEntry{
10198
/*.child_tid = */ child_tid,
10299
/*.event = */ event,
103-
});
100+
};
101+
_end2++;
102+
if (_end2 - _begin2 > LOG_SIZE) {
103+
_begin2 = _end2 - LOG_SIZE;
104+
if (_begin2 >= LOG_SIZE) {
105+
_begin2 -= LOG_SIZE;
106+
_end2 -= LOG_SIZE;
107+
}
108+
}
104109
}
105110

106111
void dump() {
@@ -128,7 +133,9 @@ class Log {
128133
}
129134

130135
std::string s;
131-
for (LogEntry const& entry : _log) {
136+
for (unsigned index2 = _begin2; index2 != _end2; index2++) {
137+
LogEntry const& entry = _log[wrap(index2)];
138+
132139
// pray it works. if not, nothing we can do.
133140
fprintf(f, "(%d) %s\n", entry.child_tid, entry.event);
134141
}
@@ -146,10 +153,18 @@ class Log {
146153
MB_OK | MB_ICONERROR);
147154
}
148155

156+
private:
157+
unsigned wrap(unsigned index) const {
158+
return index % LOG_SIZE;
159+
}
160+
149161
// fields
150162
private:
151163
std::mutex _mtx;
152-
std::deque<LogEntry> _log; // TODO replace with fixed-length ringbuf?
164+
std::array<LogEntry, LOG_SIZE> _log{};
165+
unsigned _begin2 = 0;
166+
unsigned _end2 = 0;
167+
153168
int _generation = 0;
154169
};
155170

0 commit comments

Comments
 (0)