Skip to content

Commit 83c7ad2

Browse files
Yurii ZubrytskyiGerrit Code Review
Yurii Zubrytskyi
authored and
Gerrit Code Review
committed
Merge "[zip] Stop calculating crc if it's not checked"
2 parents 49c5297 + 8d8f637 commit 83c7ad2

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

libziparchive/zip_archive.cc

+11-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
// Used to turn on crc checks - verify that the content CRC matches the values
6161
// specified in the local file header and the central directory.
62-
static const bool kCrcChecksEnabled = false;
62+
static constexpr bool kCrcChecksEnabled = false;
6363

6464
// The maximum number of bytes to scan backwards for the EOCD start.
6565
static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
@@ -1314,11 +1314,15 @@ static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry64* en
13141314
if (!writer->Append(&buf[0], block_size)) {
13151315
return kIoError;
13161316
}
1317-
crc = crc32(crc, &buf[0], block_size);
1317+
if (crc_out) {
1318+
crc = crc32(crc, &buf[0], block_size);
1319+
}
13181320
count += block_size;
13191321
}
13201322

1321-
*crc_out = crc;
1323+
if (crc_out) {
1324+
*crc_out = crc;
1325+
}
13221326

13231327
return 0;
13241328
}
@@ -1331,9 +1335,11 @@ int32_t ExtractToWriter(ZipArchiveHandle handle, const ZipEntry64* entry,
13311335
int32_t return_value = -1;
13321336
uint64_t crc = 0;
13331337
if (method == kCompressStored) {
1334-
return_value = CopyEntryToWriter(handle->mapped_zip, entry, writer, &crc);
1338+
return_value =
1339+
CopyEntryToWriter(handle->mapped_zip, entry, writer, kCrcChecksEnabled ? &crc : nullptr);
13351340
} else if (method == kCompressDeflated) {
1336-
return_value = InflateEntryToWriter(handle->mapped_zip, entry, writer, &crc);
1341+
return_value =
1342+
InflateEntryToWriter(handle->mapped_zip, entry, writer, kCrcChecksEnabled ? &crc : nullptr);
13371343
}
13381344

13391345
if (!return_value && entry->has_data_descriptor) {

libziparchive/zip_archive_benchmark.cpp

+30-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <cstdio>
1818
#include <cstdlib>
1919
#include <cstring>
20-
#include <iostream>
2120
#include <string>
2221
#include <tuple>
2322
#include <vector>
@@ -28,17 +27,20 @@
2827
#include <ziparchive/zip_archive_stream_entry.h>
2928
#include <ziparchive/zip_writer.h>
3029

31-
static TemporaryFile* CreateZip() {
32-
TemporaryFile* result = new TemporaryFile;
30+
static std::unique_ptr<TemporaryFile> CreateZip(int size = 4, int count = 1000) {
31+
auto result = std::make_unique<TemporaryFile>();
3332
FILE* fp = fdopen(result->fd, "w");
3433

3534
ZipWriter writer(fp);
3635
std::string lastName = "file";
37-
for (size_t i = 0; i < 1000; i++) {
36+
for (size_t i = 0; i < count; i++) {
3837
// Make file names longer and longer.
3938
lastName = lastName + std::to_string(i);
4039
writer.StartEntry(lastName.c_str(), ZipWriter::kCompress);
41-
writer.WriteBytes("helo", 4);
40+
while (size > 0) {
41+
writer.WriteBytes("helo", 4);
42+
size -= 4;
43+
}
4244
writer.FinishEntry();
4345
}
4446
writer.Finish();
@@ -106,5 +108,28 @@ static void StartAlignedEntry(benchmark::State& state) {
106108
}
107109
BENCHMARK(StartAlignedEntry)->Arg(2)->Arg(16)->Arg(1024)->Arg(4096);
108110

111+
static void ExtractEntry(benchmark::State& state) {
112+
std::unique_ptr<TemporaryFile> temp_file(CreateZip(1024 * 1024, 1));
113+
114+
ZipArchiveHandle handle;
115+
ZipEntry data;
116+
if (OpenArchive(temp_file->path, &handle)) {
117+
state.SkipWithError("Failed to open archive");
118+
}
119+
if (FindEntry(handle, "file0", &data)) {
120+
state.SkipWithError("Failed to find archive entry");
121+
}
122+
123+
std::vector<uint8_t> buffer(1024 * 1024);
124+
for (auto _ : state) {
125+
if (ExtractToMemory(handle, &data, buffer.data(), uint32_t(buffer.size()))) {
126+
state.SkipWithError("Failed to extract archive entry");
127+
break;
128+
}
129+
}
130+
CloseArchive(handle);
131+
}
132+
133+
BENCHMARK(ExtractEntry)->Arg(2)->Arg(16)->Arg(1024);
109134

110135
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)