v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
snapshot-compression.cc
Go to the documentation of this file.
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
6
8#include "src/utils/memcopy.h"
9#include "src/utils/utils.h"
10#include "third_party/zlib/google/compression_utils_portable.h"
11
12namespace v8 {
13namespace internal {
14
15uint32_t GetUncompressedSize(const Bytef* compressed_data) {
16 uint32_t size;
17 MemCopy(&size, compressed_data, sizeof(size));
18 return size;
19}
20
22 const SnapshotData* uncompressed_data) {
23 SnapshotData snapshot_data;
25 if (v8_flags.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 uint8_t* compressed_data =
40 const_cast<uint8_t*>(snapshot_data.RawData().begin());
41 // Since we are doing raw compression (no zlib or gzip headers), we need to
42 // manually store the uncompressed size.
43 MemCopy(compressed_data, &payload_length, sizeof(payload_length));
44
46 zlib_internal::CompressHelper(
47 zlib_internal::ZRAW, compressed_data + sizeof(payload_length),
48 &compressed_data_size,
49 reinterpret_cast<const Bytef*>(uncompressed_data->RawData().begin()),
50 input_size, Z_DEFAULT_COMPRESSION, nullptr, nullptr),
51 Z_OK);
52
53 // Reallocating to exactly the size we need.
54 snapshot_data.Resize(static_cast<uint32_t>(compressed_data_size) +
55 sizeof(payload_length));
56 DCHECK_EQ(payload_length,
57 GetUncompressedSize(snapshot_data.RawData().begin()));
58
59 if (v8_flags.profile_deserialization) {
60 double ms = timer.Elapsed().InMillisecondsF();
61 PrintF("[Compressing %d bytes took %0.3f ms]\n", payload_length, ms);
62 }
63 return snapshot_data;
64}
65
67 base::Vector<const uint8_t> compressed_data) {
68 SnapshotData snapshot_data;
70 if (v8_flags.profile_deserialization) timer.Start();
71
72 const Bytef* input_bytef =
73 reinterpret_cast<const Bytef*>(compressed_data.begin());
74
75 // Since we are doing raw compression (no zlib or gzip headers), we need to
76 // manually retrieve the uncompressed size.
77 uint32_t uncompressed_payload_length = GetUncompressedSize(input_bytef);
78 input_bytef += sizeof(uncompressed_payload_length);
79
80 snapshot_data.AllocateData(uncompressed_payload_length);
81
82 uLongf uncompressed_size = uncompressed_payload_length;
83 CHECK_EQ(zlib_internal::UncompressHelper(
84 zlib_internal::ZRAW,
85 const_cast<Bytef*>(snapshot_data.RawData().begin()),
86 &uncompressed_size, input_bytef,
87 static_cast<uLong>(compressed_data.size() -
88 sizeof(uncompressed_payload_length))),
89 Z_OK);
90
91 if (v8_flags.profile_deserialization) {
92 double ms = timer.Elapsed().InMillisecondsF();
93 PrintF("[Decompressing %d bytes took %0.3f ms]\n",
94 uncompressed_payload_length, ms);
95 }
96 return snapshot_data;
97}
98
99} // namespace internal
100} // namespace v8
constexpr size_t size() const
Definition vector.h:70
constexpr T * begin() const
Definition vector.h:96
void AllocateData(uint32_t size)
static V8_EXPORT_PRIVATE SnapshotData Decompress(base::Vector< const uint8_t > compressed_data)
static V8_EXPORT_PRIVATE SnapshotData Compress(const SnapshotData *uncompressed_data)
void Resize(uint32_t size)
base::Vector< const uint8_t > RawData() const
void PrintF(const char *format,...)
Definition utils.cc:39
uint32_t GetUncompressedSize(const Bytef *compressed_data)
V8_EXPORT_PRIVATE FlagValues v8_flags
void MemCopy(void *dest, const void *src, size_t size)
Definition memcopy.h:124
#define CHECK_EQ(lhs, rhs)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485