Skip to content

Commit 82dfcbe

Browse files
committed
Snapshots now store raw size to speed up decompression
1 parent 8ae5d7e commit 82dfcbe

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

Emulator/Media/Snapshot.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Snapshot::Snapshot(isize capacity)
7373
header->minor = SNP_MINOR;
7474
header->subminor = SNP_SUBMINOR;
7575
header->beta = SNP_BETA;
76+
header->rawSize = i32(data.size);
7677
}
7778

7879
Snapshot::Snapshot(Amiga &amiga) : Snapshot(amiga.size())
@@ -175,14 +176,22 @@ Snapshot::uncompress()
175176
{
176177
if (isCompressed()) {
177178

179+
isize expectedSize = getHeader()->rawSize;
180+
178181
debug(SNP_DEBUG, "Uncompressing %ld bytes...", data.size);
179182

180183
{ auto watch = util::StopWatch(SNP_DEBUG, "");
181184

182-
data.uncompress(2, sizeof(SnapshotHeader));
185+
data.uncompress(2, sizeof(SnapshotHeader), expectedSize);
183186
getHeader()->compressed = false;
184187
}
185188
debug(SNP_DEBUG, "Uncompressed size: %ld bytes (hash: 0x%x)\n", data.size, data.fnv32());
189+
190+
if (getHeader()->rawSize != expectedSize) {
191+
192+
warn("Snaphot size: %ld. Expected: %ld\n", data.size, expectedSize);
193+
fatalError;
194+
}
186195
}
187196
}
188197

Emulator/Media/Snapshot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ struct SnapshotHeader {
4343
// Indicates if the snapshot contents is stored in compressed form
4444
bool compressed;
4545

46+
// Size of this snapshot when uncompressed
47+
i32 rawSize;
48+
4649
// Preview image
4750
Thumbnail screenshot;
4851
};

Emulator/Utilities/Buffer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,14 @@ Allocator<T>::compress(isize n, isize offset)
251251
}
252252

253253
template <class T> void
254-
Allocator<T>::uncompress(isize n, isize offset)
254+
Allocator<T>::uncompress(isize n, isize offset, isize expectedSize)
255255
{
256256
T prev = 0;
257257
isize repetitions = 0;
258258
std::vector<T> vec;
259-
vec.reserve(size);
259+
260+
// Speed up by starting with a big enough container
261+
if (expectedSize) vec.reserve(expectedSize);
260262

261263
auto decode = [&](T element, isize count) {
262264

@@ -304,7 +306,7 @@ template void Allocator<T>::copy(T *buf, isize offset, isize len) const; \
304306
template void Allocator<T>::patch(const u8 *seq, const u8 *subst); \
305307
template void Allocator<T>::patch(const char *seq, const char *subst); \
306308
template void Allocator<T>::compress(isize, isize); \
307-
template void Allocator<T>::uncompress(isize, isize);
309+
template void Allocator<T>::uncompress(isize, isize, isize);
308310

309311
INSTANTIATE_ALLOCATOR(u8)
310312
INSTANTIATE_ALLOCATOR(u32)

Emulator/Utilities/Buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ template <class T> struct Allocator {
7070

7171
// Compresses or uncompresses a buffer
7272
void compress(isize n = 2, isize offset = 0);
73-
void uncompress(isize n = 2, isize offset = 0);
73+
void uncompress(isize n = 2, isize offset = 0, isize expectedSize = 0);
7474
};
7575

7676
template <class T> struct Buffer : public Allocator <T> {

0 commit comments

Comments
 (0)