1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/snapshot/snapshot-compression.h"
6 
7 #include "src/base/platform/elapsed-timer.h"
8 #include "src/utils/memcopy.h"
9 #include "src/utils/utils.h"
10 #include "third_party/zlib/google/compression_utils_portable.h"
11 
12 namespace v8 {
13 namespace internal {
14 
GetUncompressedSize(const Bytef * compressed_data)15 uint32_t GetUncompressedSize(const Bytef* compressed_data) {
16   uint32_t size;
17   MemCopy(&size, compressed_data, sizeof(size));
18   return size;
19 }
20 
Compress(const SnapshotData * uncompressed_data)21 SnapshotData SnapshotCompression::Compress(
22     const SnapshotData* uncompressed_data) {
23   SnapshotData snapshot_data;
24   base::ElapsedTimer timer;
25   if (FLAG_profile_deserialization) timer.Start();
26 
27   static_assert(sizeof(Bytef) == 1, "");
28   const uLongf input_size =
29       static_cast<uLongf>(uncompressed_data->RawData().size());
30   uint32_t payload_length =
31       static_cast<uint32_t>(uncompressed_data->RawData().size());
32 
33   uLongf compressed_data_size = compressBound(input_size);
34 
35   // Allocating >= the final amount we will need.
36   snapshot_data.AllocateData(
37       static_cast<uint32_t>(sizeof(payload_length) + compressed_data_size));
38 
39   byte* compressed_data = const_cast<byte*>(snapshot_data.RawData().begin());
40   // Since we are doing raw compression (no zlib or gzip headers), we need to
41   // manually store the uncompressed size.
42   MemCopy(compressed_data, &payload_length, sizeof(payload_length));
43 
44   CHECK_EQ(zlib_internal::CompressHelper(
45                zlib_internal::ZRAW, compressed_data + sizeof(payload_length),
46                &compressed_data_size,
47                bit_cast<const Bytef*>(uncompressed_data->RawData().begin()),
48                input_size, Z_DEFAULT_COMPRESSION, nullptr, nullptr),
49            Z_OK);
50 
51   // Reallocating to exactly the size we need.
52   snapshot_data.Resize(static_cast<uint32_t>(compressed_data_size) +
53                        sizeof(payload_length));
54   DCHECK_EQ(payload_length,
55             GetUncompressedSize(snapshot_data.RawData().begin()));
56 
57   if (FLAG_profile_deserialization) {
58     double ms = timer.Elapsed().InMillisecondsF();
59     PrintF("[Compressing %d bytes took %0.3f ms]\n", payload_length, ms);
60   }
61   return snapshot_data;
62 }
63 
Decompress(base::Vector<const byte> compressed_data)64 SnapshotData SnapshotCompression::Decompress(
65     base::Vector<const byte> compressed_data) {
66   SnapshotData snapshot_data;
67   base::ElapsedTimer timer;
68   if (FLAG_profile_deserialization) timer.Start();
69 
70   const Bytef* input_bytef = bit_cast<const Bytef*>(compressed_data.begin());
71 
72   // Since we are doing raw compression (no zlib or gzip headers), we need to
73   // manually retrieve the uncompressed size.
74   uint32_t uncompressed_payload_length = GetUncompressedSize(input_bytef);
75   input_bytef += sizeof(uncompressed_payload_length);
76 
77   snapshot_data.AllocateData(uncompressed_payload_length);
78 
79   uLongf uncompressed_size = uncompressed_payload_length;
80   CHECK_EQ(zlib_internal::UncompressHelper(
81                zlib_internal::ZRAW,
82                bit_cast<Bytef*>(snapshot_data.RawData().begin()),
83                &uncompressed_size, input_bytef,
84                static_cast<uLong>(compressed_data.size() -
85                                   sizeof(uncompressed_payload_length))),
86            Z_OK);
87 
88   if (FLAG_profile_deserialization) {
89     double ms = timer.Elapsed().InMillisecondsF();
90     PrintF("[Decompressing %d bytes took %0.3f ms]\n",
91            uncompressed_payload_length, ms);
92   }
93   return snapshot_data;
94 }
95 
96 }  // namespace internal
97 }  // namespace v8
98