More optimize inserts using reserve()

Signed-off-by: German Semenov <GermanAizek@yandex.ru>
This commit is contained in:
Герман Семенов 2023-08-24 19:08:06 +03:00 committed by German Semenov
parent d4e60e0c03
commit 1cbb61ec27
3 changed files with 42 additions and 3 deletions

View file

@ -950,12 +950,13 @@ bool DivEngine::addSystem(DivSystem which) {
unsigned int outs=disCont[i].dispatch->getOutputCount();
if (outs>16) outs=16;
if (outs<2) {
song.patchbay.reserve(DIV_MAX_OUTPUTS);
for (unsigned int j=0; j<DIV_MAX_OUTPUTS; j++) {
song.patchbay.push_back((i<<20)|j);
}
} else {
song.patchbay.reserve(outs);
for (unsigned int j=0; j<outs; j++) {
song.patchbay.push_back((i<<20)|(j<<16)|j);
}
}
@ -1062,9 +1063,12 @@ bool DivEngine::swapSystem(int src, int dest, bool preserveOrder) {
// prepare swap list
int index=0;
swapList.reserve(song.systemLen);
for (int i=0; i<song.systemLen; i++) {
chanList.clear();
for (int j=0; j<getChannelCount(song.system[i]); j++) {
const int channelCount = getChannelCount(song.system[i]);
chanList.reserve(channelCount);
for (int j=0; j<channelCount; j++) {
chanList.push_back(index);
index++;
}
@ -1319,6 +1323,7 @@ void DivEngine::enableCommandStream(bool enable) {
void DivEngine::getCommandStream(std::vector<DivCommand>& where) {
BUSY_BEGIN;
where.clear();
where.reserve(cmdStream.size());
for (DivCommand& i: cmdStream) {
where.push_back(i);
}
@ -2822,23 +2827,26 @@ void DivEngine::autoPatchbay() {
unsigned int outs=disCont[i].dispatch->getOutputCount();
if (outs>16) outs=16;
if (outs<2) {
song.patchbay.reserve(DIV_MAX_OUTPUTS);
for (unsigned int j=0; j<DIV_MAX_OUTPUTS; j++) {
song.patchbay.push_back((i<<20)|j);
}
} else {
song.patchbay.reserve(outs);
for (unsigned int j=0; j<outs; j++) {
song.patchbay.push_back((i<<20)|(j<<16)|j);
}
}
}
// wave/sample preview
song.patchbay.reserve(DIV_MAX_OUTPUTS);
for (unsigned int j=0; j<DIV_MAX_OUTPUTS; j++) {
song.patchbay.push_back(0xffd00000|j);
}
// metronome
song.patchbay.reserve(DIV_MAX_OUTPUTS);
for (unsigned int j=0; j<DIV_MAX_OUTPUTS; j++) {
song.patchbay.push_back(0xffe00000|j);
}

View file

@ -266,6 +266,7 @@ std::vector<DivROMExportOutput> DivExportAmigaValidation::go(DivEngine* e) {
}
// finish
ret.reserve(5);
ret.push_back(DivROMExportOutput("sbook.bin",sbook));
ret.push_back(DivROMExportOutput("wbook.bin",wbook));
ret.push_back(DivROMExportOutput("sample.bin",sample));

View file

@ -342,6 +342,7 @@ bool DivEngine::loadDMF(unsigned char* file, size_t len) {
ds.insLen=16;
}
logI("reading instruments (%d)...",ds.insLen);
ds.ins.reserve(ds.insLen);
for (int i=0; i<ds.insLen; i++) {
DivInstrument* ins=new DivInstrument;
unsigned char mode=0;
@ -669,6 +670,7 @@ bool DivEngine::loadDMF(unsigned char* file, size_t len) {
if (ds.version>0x0b) {
ds.waveLen=(unsigned char)reader.readC();
logI("reading wavetables (%d)...",ds.waveLen);
ds.wave.reserve(ds.waveLen);
for (int i=0; i<ds.waveLen; i++) {
DivWavetable* wave=new DivWavetable;
wave->len=(unsigned char)reader.readI();
@ -838,6 +840,7 @@ bool DivEngine::loadDMF(unsigned char* file, size_t len) {
// it appears this byte stored the YMU759 sample rate
ymuSampleRate=reader.readC();
}
ds.sample.reserve(ds.sampleLen);
for (int i=0; i<ds.sampleLen; i++) {
DivSample* sample=new DivSample;
int length=reader.readI();
@ -2127,6 +2130,7 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
for (int i=0; i<ds.sampleLen; i++) {
samplePtr[i]=reader.readI();
}
patPtr.reserve(numberOfPats);
for (int i=0; i<numberOfPats; i++) patPtr.push_back(reader.readI());
logD("reading orders (%d)...",subSong->ordersLen);
@ -2345,6 +2349,7 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
// patchbay
unsigned int conns=reader.readI();
ds.patchbay.reserve(conns);
for (unsigned int i=0; i<conns; i++) {
ds.patchbay.push_back((unsigned int)reader.readI());
}
@ -2377,6 +2382,7 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
// grooves
unsigned char grooveCount=reader.readC();
ds.grooves.reserve(grooveCount);
for (int i=0; i<grooveCount; i++) {
DivGroovePattern gp;
gp.len=reader.readC();
@ -2477,6 +2483,7 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
// read subsongs
if (ds.version>=95) {
ds.subsong.reserve(numberOfSubSongs);
for (int i=0; i<numberOfSubSongs; i++) {
ds.subsong.push_back(new DivSubSong);
if (!reader.seek(subSongPtr[i],SEEK_SET)) {
@ -2558,6 +2565,7 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
}
// read instruments
ds.ins.reserve(ds.insLen);
for (int i=0; i<ds.insLen; i++) {
DivInstrument* ins=new DivInstrument;
logD("reading instrument %d at %x...",i,insPtr[i]);
@ -2582,6 +2590,7 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
}
// read wavetables
ds.wave.reserve(ds.waveLen);
for (int i=0; i<ds.waveLen; i++) {
DivWavetable* wave=new DivWavetable;
logD("reading wavetable %d at %x...",i,wavePtr[i]);
@ -2606,6 +2615,7 @@ bool DivEngine::loadFur(unsigned char* file, size_t len) {
}
// read samples
ds.sample.reserve(ds.sampleLen);
for (int i=0; i<ds.sampleLen; i++) {
DivSample* sample=new DivSample;
@ -3069,6 +3079,7 @@ bool DivEngine::loadMod(unsigned char* file, size_t len) {
// samples
logD("reading samples... (%d)",insCount);
ds.sample.reserve(insCount);
for (int i=0; i<insCount; i++) {
DivSample* sample=new DivSample;
sample->depth=DIV_SAMPLE_DEPTH_8BIT;
@ -3373,6 +3384,7 @@ bool DivEngine::loadMod(unsigned char* file, size_t len) {
}
// instrument creation
ds.ins.reserve(insCount);
for(int i=0; i<insCount; i++) {
DivInstrument* ins=new DivInstrument;
ins->type=DIV_INS_AMIGA;
@ -3651,6 +3663,7 @@ bool DivEngine::loadS3M(unsigned char* file, size_t len) {
}
// load instruments/samples
ds.ins.reserve(ds.insLen);
for (int i=0; i<ds.insLen; i++) {
DivInstrument* ins=new DivInstrument;
if (!reader.seek(0x4c+insPtr[i]*16,SEEK_SET)) {
@ -3877,6 +3890,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
// sequences
seqLen/=13;
logD("reading sequences... (%d)",seqLen);
seq.reserve(seqLen);
for (unsigned int i=0; i<seqLen; i++) {
FCSequence s;
for (int j=0; j<4; j++) {
@ -3906,6 +3920,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
}
patLen/=64;
logD("reading patterns... (%d)",patLen);
pat.reserve(patLen);
for (unsigned int i=0; i<patLen; i++) {
FCPattern p;
logV("- pattern %d",i);
@ -3926,6 +3941,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
}
freqMacroLen/=64;
logD("reading freq sequences... (%d)",freqMacroLen);
freqMacros.reserve(freqMacroLen);
for (unsigned int i=0; i<freqMacroLen; i++) {
FCMacro m;
reader.read(m.val,64);
@ -3941,6 +3957,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
}
volMacroLen/=64;
logD("reading volume sequences... (%d)",volMacroLen);
volMacros.reserve(volMacroLen);
for (unsigned int i=0; i<volMacroLen; i++) {
FCMacro m;
reader.read(m.val,64);
@ -3955,6 +3972,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
return false;
}
logD("reading samples...");
ds.sample.reserve(10);
for (int i=0; i<10; i++) {
DivSample* s=new DivSample;
s->depth=DIV_SAMPLE_DEPTH_8BIT;
@ -3981,6 +3999,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
return false;
}
logD("reading wavetables...");
ds.wave.reserve(80);
for (int i=0; i<80; i++) {
DivWavetable* w=new DivWavetable;
w->min=0;
@ -4009,6 +4028,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
}
} else {
// generate preset waves
ds.wave.reserve(48);
for (int i=0; i<48; i++) {
DivWavetable* w=new DivWavetable;
generateFCPresetWave(i,w);
@ -4156,6 +4176,7 @@ bool DivEngine::loadFC(unsigned char* file, size_t len) {
// volume sequence
ins->std.volMacro.len=0;
ds.ins.reserve(64 - 5);
for (int j=5; j<64; j++) {
loopMap[j]=ins->std.volMacro.len;
if (m.val[j]==0xe1) { // end
@ -4553,6 +4574,7 @@ bool DivEngine::loadFTM(unsigned char* file, size_t len) {
CHECK_BLOCK_VERSION(4);
unsigned char totalSongs=reader.readC();
logV("%d songs:",totalSongs+1);
ds.subsong.reserve(totalSongs);
for (int i=0; i<=totalSongs; i++) {
String subSongName=reader.readString();
ds.subsong.push_back(new DivSubSong);
@ -5087,12 +5109,14 @@ DivDataErrors DivEngine::readAssetDirData(SafeReader& reader, std::vector<DivAss
unsigned int numDirs=reader.readI();
dir.reserve(numDirs);
for (unsigned int i=0; i<numDirs; i++) {
DivAssetDir d;
d.name=reader.readString();
unsigned short numEntries=reader.readS();
d.entries.reserve(numEntries);
for (unsigned short j=0; j<numEntries; j++) {
d.entries.push_back(((unsigned char)reader.readC()));
}
@ -5425,6 +5449,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary, bool newPatternFormat) {
w->seek(0,SEEK_END);
/// SUBSONGS
subSongPtr.reserve(song.subsong.size() - 1);
for (subSongIndex=1; subSongIndex<song.subsong.size(); subSongIndex++) {
subSong=song.subsong[subSongIndex];
subSongPtr.push_back(w->tell());
@ -5486,6 +5511,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary, bool newPatternFormat) {
}
/// CHIP FLAGS
sysFlagsPtr.reserve(song.systemLen);
for (int i=0; i<song.systemLen; i++) {
String data=song.systemFlags[i].toString();
if (data.empty()) {
@ -5515,6 +5541,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary, bool newPatternFormat) {
putAssetDirData(w,song.sampleDir);
/// INSTRUMENT
insPtr.reserve(song.insLen);
for (int i=0; i<song.insLen; i++) {
DivInstrument* ins=song.ins[i];
insPtr.push_back(w->tell());
@ -5522,6 +5549,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary, bool newPatternFormat) {
}
/// WAVETABLE
wavePtr.reserve(song.waveLen);
for (int i=0; i<song.waveLen; i++) {
DivWavetable* wave=song.wave[i];
wavePtr.push_back(w->tell());
@ -5529,6 +5557,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary, bool newPatternFormat) {
}
/// SAMPLE
samplePtr.reserve(song.sampleLen);
for (int i=0; i<song.sampleLen; i++) {
DivSample* sample=song.sample[i];
samplePtr.push_back(w->tell());
@ -5536,6 +5565,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary, bool newPatternFormat) {
}
/// PATTERN
patPtr.reserve(patsToWrite.size());
for (PatToWrite& i: patsToWrite) {
DivPattern* pat=song.subsong[i.subsong]->pat[i.chan].getPattern(i.pat,false);
patPtr.push_back(w->tell());