Optimize data flushing for FileAccessCompressed and FileAccessEncrypted

This commit is contained in:
BlueCube3310 2025-09-01 18:29:01 +02:00
parent 76dfc53dee
commit ebdf57fbf1
2 changed files with 18 additions and 17 deletions

View file

@ -130,34 +130,37 @@ void FileAccessCompressed::_close() {
f->store_32(0); //compressed sizes, will update later f->store_32(0); //compressed sizes, will update later
} }
Vector<int> block_sizes; uint32_t last_block_size = write_max % block_size;
// Temporary buffer for compressed data blocks.
LocalVector<uint8_t> temp_cblock;
temp_cblock.resize(Compression::get_max_compressed_buffer_size(bc == 1 ? last_block_size : block_size, cmode));
uint8_t *temp_cblock_ptr = temp_cblock.ptr();
// Compress and store the blocks.
LocalVector<uint32_t> block_sizes;
for (uint32_t i = 0; i < bc; i++) { for (uint32_t i = 0; i < bc; i++) {
uint32_t bl = i == (bc - 1) ? write_max % block_size : block_size; uint32_t bl = i == (bc - 1) ? last_block_size : block_size;
uint8_t *bp = &write_ptr[i * block_size]; uint8_t *bp = &write_ptr[i * block_size];
Vector<uint8_t> cblock; const int64_t compressed_size = Compression::compress(temp_cblock_ptr, bp, bl, cmode);
cblock.resize(Compression::get_max_compressed_buffer_size(bl, cmode));
const int64_t compressed_size = Compression::compress(cblock.ptrw(), bp, bl, cmode);
ERR_FAIL_COND_MSG(compressed_size < 0, "FileAccessCompressed: Error compressing data."); ERR_FAIL_COND_MSG(compressed_size < 0, "FileAccessCompressed: Error compressing data.");
f->store_buffer(cblock.ptr(), (uint64_t)compressed_size); f->store_buffer(temp_cblock_ptr, (uint64_t)compressed_size);
block_sizes.push_back(compressed_size); block_sizes.push_back(compressed_size);
} }
f->seek(16); //ok write block sizes f->seek(16); //ok write block sizes
for (uint32_t i = 0; i < bc; i++) { for (uint32_t i = 0; i < bc; i++) {
f->store_32(uint32_t(block_sizes[i])); f->store_32(block_sizes[i]);
} }
f->seek_end(); f->seek_end();
f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too
buffer.clear();
} else { } else {
comp_buffer.clear(); comp_buffer.clear();
buffer.clear();
read_blocks.clear(); read_blocks.clear();
} }
buffer.clear();
f.unref(); f.unref();
} }

View file

@ -140,7 +140,7 @@ void FileAccessEncrypted::_close() {
} }
if (writing) { if (writing) {
Vector<uint8_t> compressed; LocalVector<uint8_t> compressed;
uint64_t len = data.size(); uint64_t len = data.size();
if (len % 16) { if (len % 16) {
len += 16 - (len % 16); len += 16 - (len % 16);
@ -150,10 +150,8 @@ void FileAccessEncrypted::_close() {
ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug? ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug?
compressed.resize(len); compressed.resize(len);
memset(compressed.ptrw(), 0, len); memcpy(compressed.ptr(), data.ptr(), data.size());
for (int i = 0; i < data.size(); i++) { memset(compressed.ptr() + data.size(), 0, len - data.size());
compressed.write[i] = data[i];
}
CryptoCore::AESContext ctx; CryptoCore::AESContext ctx;
ctx.set_encode_key(key.ptrw(), 256); ctx.set_encode_key(key.ptrw(), 256);
@ -166,7 +164,7 @@ void FileAccessEncrypted::_close() {
file->store_64(data.size()); file->store_64(data.size());
file->store_buffer(iv.ptr(), 16); file->store_buffer(iv.ptr(), 16);
ctx.encrypt_cfb(len, iv.ptrw(), compressed.ptrw(), compressed.ptrw()); ctx.encrypt_cfb(len, iv.ptrw(), compressed.ptr(), compressed.ptr());
file->store_buffer(compressed.ptr(), compressed.size()); file->store_buffer(compressed.ptr(), compressed.size());
data.clear(); data.clear();