Skip to content

Commit 9362b94

Browse files
committed
Optimize buffer sizes
1 parent b543af9 commit 9362b94

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

Source/Processors/RecordNode/BinaryFormat/BinaryRecording.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ class BinaryRecording : public RecordEngine
146146
std::map<uint64, int64> firstSampleNumber;
147147
std::map<uint64, bool> wroteFirstSampleNumber;
148148

149-
const int samplesPerBlock { 4096 };
149+
const int samplesPerBlock { 8192 }; // Larger blocks reduce memory block operations
150150
};
151151
#endif

Source/Processors/RecordNode/BinaryFormat/SequentialBlockFile.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ SequentialBlockFile::SequentialBlockFile (int nChannels, int samplesPerBlock) :
3333
m_memBlocks.ensureStorageAllocated (blockArrayInitSize);
3434
for (int i = 0; i < nChannels; i++)
3535
m_currentBlock.add (-1);
36+
37+
// Pre-allocate batch buffers and cache tile config to avoid hot-path allocations
38+
m_batchChannelPtrs.reserve (nChannels);
39+
m_tileConfig = SIMDConverter::getRecommendedTileConfig (nChannels);
3640
}
3741

3842
SequentialBlockFile::~SequentialBlockFile()
@@ -141,7 +145,6 @@ bool SequentialBlockFile::writeChannelBatch (uint64 startPos, int16* const* chan
141145
// Batch writing requires all channels - return false to signal caller should use per-channel writes
142146
if (numChannels != m_nChannels)
143147
{
144-
printf ("[RN]SequentialBlockFile::writeChannelBatch channel count mismatch: expected %d, got %d\n", m_nChannels, numChannels);
145148
return false;
146149
}
147150

@@ -164,32 +167,33 @@ bool SequentialBlockFile::writeChannelBatch (uint64 startPos, int16* const* chan
164167
int dataIdx = 0;
165168
int lastBlockIdx = m_memBlocks.size() - 1;
166169

167-
// Get recommended tile configuration for cache-efficient interleaving
168-
auto tileConfig = SIMDConverter::getRecommendedTileConfig (m_nChannels);
169-
170170
while (writtenSamples < nSamples)
171171
{
172172
int16* blockPtr = m_memBlocks[bIndex]->getData();
173173
int samplesToWrite = jmin ((nSamples - writtenSamples), (m_samplesPerBlock - int (startIdx)));
174174

175-
// Create adjusted channel data pointers for the current data offset
176-
std::vector<const int16_t*> adjustedChannelPtrs (m_nChannels);
175+
// Use pre-allocated channel pointer array to avoid allocation in hot path
176+
// Resize only if needed (should rarely happen after first call)
177+
if (m_batchChannelPtrs.size() != static_cast<size_t> (m_nChannels))
178+
m_batchChannelPtrs.resize (m_nChannels);
179+
177180
for (int ch = 0; ch < m_nChannels; ch++)
178181
{
179-
adjustedChannelPtrs[ch] = channelData[ch] + dataIdx;
182+
m_batchChannelPtrs[ch] = channelData[ch] + dataIdx;
180183
}
181184

182185
// Calculate output position in the block
183186
int16_t* outputPtr = blockPtr + startIdx * m_nChannels;
184187

185188
// Use SIMD-optimized interleaving with cache-blocked tiles
189+
// Use cached tile config to avoid repeated lookups
186190
SIMDConverter::interleaveInt16 (
187-
adjustedChannelPtrs.data(),
191+
m_batchChannelPtrs.data(),
188192
outputPtr,
189193
m_nChannels,
190194
samplesToWrite,
191-
tileConfig.tileSamples,
192-
tileConfig.tileChannels);
195+
m_tileConfig.tileSamples,
196+
m_tileConfig.tileChannels);
193197

194198
writtenSamples += samplesToWrite;
195199
dataIdx += samplesToWrite;

Source/Processors/RecordNode/BinaryFormat/SequentialBlockFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "../../../Utils/Utils.h"
2828
#include "FileMemoryBlock.h"
29+
#include "SIMDConverter.h"
2930

3031
#include "../../PluginManager/PluginClass.h"
3132

@@ -94,5 +95,9 @@ class PLUGIN_API SequentialBlockFile
9495
/** Compile-time params */
9596
const int streamBufferSize { 65536 }; // 64KB buffer to reduce system calls
9697
const int blockArrayInitSize { 128 };
98+
99+
/** Pre-allocated buffers for batch writing to avoid hot-path allocations */
100+
mutable std::vector<const int16_t*> m_batchChannelPtrs;
101+
SIMDConverter::TileConfig m_tileConfig; // Cached tile config for this channel count
97102
};
98103
#endif // !SEQUENTIALBLOCKFILE_H

0 commit comments

Comments
 (0)