forked from Samsung/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressibleString.cpp
More file actions
315 lines (256 loc) · 10.3 KB
/
CompressibleString.cpp
File metadata and controls
315 lines (256 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright (c) 2019-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#if defined(ENABLE_COMPRESSIBLE_STRING)
#include "Escargot.h"
#include "CompressibleString.h"
#include "runtime/Context.h"
#include "runtime/VMInstance.h"
#include "runtime/Global.h"
#include "lz4.h"
namespace Escargot {
void* CompressibleString::operator new(size_t size)
{
static MAY_THREAD_LOCAL bool typeInited = false;
static MAY_THREAD_LOCAL GC_descr descr;
if (!typeInited) {
GC_word obj_bitmap[GC_BITMAP_SIZE(CompressibleString)] = { 0 };
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(CompressibleString, m_bufferData.buffer));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(CompressibleString, m_vmInstance));
GC_set_bit(obj_bitmap, GC_WORD_OFFSET(CompressibleString, m_compressedData));
descr = GC_make_descriptor(obj_bitmap, GC_WORD_LEN(CompressibleString));
typeInited = true;
}
return GC_MALLOC_EXPLICITLY_TYPED(size, descr);
}
CompressibleString::CompressibleString(VMInstance* instance)
: String()
, m_isOwnerMayFreed(false)
, m_isCompressed(false)
, m_refCount(0)
, m_vmInstance(instance)
, m_lastUsedTickcount(fastTickCount())
{
m_bufferData.hasSpecialImpl = true;
auto& v = instance->compressibleStrings();
v.push_back(this);
GC_REGISTER_FINALIZER_NO_ORDER(this, [](void* obj, void*) {
CompressibleString* self = (CompressibleString*)obj;
ASSERT(self->refCount() == 0);
size_t strLength = self->m_bufferData.length * (self->m_bufferData.has8BitContent ? 1 : 2);
if (self->isCompressed()) {
self->clearCompressedData();
} else {
deallocateStringDataBuffer(const_cast<void*>(self->m_bufferData.buffer), strLength);
}
if (!self->m_isOwnerMayFreed) {
self->m_vmInstance->compressibleStringsUncomressedBufferSize() -= self->decomressedBufferSize();
self->m_vmInstance->decreaseSourceSize(strLength);
auto& v = self->m_vmInstance->compressibleStrings();
v.erase(std::find(v.begin(), v.end(), self));
}
},
nullptr, nullptr, nullptr);
}
CompressibleString::CompressibleString(VMInstance* instance, const char* str, size_t len)
: CompressibleString(instance)
{
char* buf = (char*)allocateStringDataBuffer(sizeof(char) * len);
memcpy(buf, str, len);
initBufferAccessData(buf, len, true);
instance->increaseSourceSize(len);
}
CompressibleString::CompressibleString(VMInstance* instance, const LChar* str, size_t len)
: CompressibleString(instance)
{
char* buf = (char*)allocateStringDataBuffer(sizeof(char) * len);
memcpy(buf, str, len);
initBufferAccessData(buf, len, true);
instance->increaseSourceSize(len);
}
CompressibleString::CompressibleString(VMInstance* instance, const char16_t* str, size_t len)
: CompressibleString(instance)
{
char* buf = (char*)allocateStringDataBuffer(sizeof(char) * len * 2);
memcpy(buf, str, len * 2);
initBufferAccessData(buf, len, false);
instance->increaseSourceSize(len * 2);
}
CompressibleString::CompressibleString(VMInstance* instance, void* buffer, size_t stringLength, bool is8bit)
: CompressibleString(instance)
{
RELEASE_ASSERT_NOT_REACHED();
initBufferAccessData(buffer, stringLength, is8bit);
}
void CompressibleString::initBufferAccessData(void* data, size_t len, bool is8bit)
{
m_bufferData.has8BitContent = is8bit;
m_bufferData.length = len;
m_bufferData.buffer = data;
m_vmInstance->compressibleStringsUncomressedBufferSize() += decomressedBufferSize();
}
UTF8StringDataNonGCStd CompressibleString::toNonGCUTF8StringData(int options) const
{
return bufferAccessData().toUTF8String<UTF8StringDataNonGCStd>();
}
UTF8StringData CompressibleString::toUTF8StringData() const
{
return bufferAccessData().toUTF8String<UTF8StringData>();
}
UTF16StringData CompressibleString::toUTF16StringData() const
{
auto data = bufferAccessData();
if (data.has8BitContent) {
UTF16StringData ret;
ret.resizeWithUninitializedValues(data.length);
for (size_t i = 0; i < data.length; i++) {
ret[i] = data.uncheckedCharAtFor8Bit(i);
}
return ret;
} else {
return UTF16StringData(data.bufferAs16Bit, data.length);
}
}
void* CompressibleString::allocateStringDataBuffer(size_t byteLength)
{
return GC_MALLOC_ATOMIC(byteLength);
}
void CompressibleString::deallocateStringDataBuffer(void* ptr, size_t byteLength)
{
GC_FREE(ptr);
}
void CompressibleString::clearCompressedData()
{
for (size_t i = 0; i < m_compressedData.size(); i++) {
GC_FREE(m_compressedData[i].compressedBuffer);
}
m_compressedData.clear();
}
bool CompressibleString::compress()
{
ASSERT(!m_isCompressed);
if (UNLIKELY(!m_bufferData.length || !!m_refCount)) {
ESCARGOT_LOG_INFO("Compression Failed due to string usage in stack\n");
return false;
}
bool has8Bit = m_bufferData.has8BitContent;
if (has8Bit) {
return compressWorker<LChar>();
} else {
return compressWorker<char16_t>();
}
}
void CompressibleString::decompress()
{
ASSERT(m_isCompressed);
ASSERT(m_bufferData.length);
uint64_t currentTick = fastTickCount();
bool has8Bit = m_bufferData.has8BitContent;
if (has8Bit) {
decompressWorker<LChar>();
} else {
decompressWorker<char16_t>();
}
Global::addHeapProfileDecompTime(fastTickCount() - currentTick);
Global::increaseHeapProfileDecompCount();
}
constexpr static const size_t g_compressChunkSize = 1044465;
static_assert(LZ4_COMPRESSBOUND(g_compressChunkSize) == 1024 * 1024, "");
template <typename StringType>
bool CompressibleString::compressWorker()
{
ASSERT(!m_isCompressed && !m_refCount);
ASSERT(m_bufferData.length > 0);
ASSERT(!GC_is_disabled());
uint64_t currentTick = fastTickCount();
GC_disable();
size_t originByteLength = m_bufferData.length * sizeof(StringType);
size_t totalDecompLength = 0;
int lastBoundLength = 0;
char* tempBuffer = nullptr;
ESCARGOT_LOG_INFO("COMPRESS TICK: %" PRIu64 " Size: %zu\n", currentTick, originByteLength);
for (size_t srcIndex = 0; srcIndex < originByteLength; srcIndex += g_compressChunkSize) {
int srcSize = (int)std::min(g_compressChunkSize, originByteLength - srcIndex);
int boundLength = LZ4::LZ4_compressBound(srcSize);
if (boundLength > lastBoundLength) {
delete[] tempBuffer;
tempBuffer = new char[boundLength];
lastBoundLength = boundLength;
}
int compressedLength = LZ4::LZ4_compress_default(m_bufferData.bufferAs8Bit + srcIndex, (char*)tempBuffer, srcSize, boundLength);
if (!compressedLength) {
// compression fail
ESCARGOT_LOG_ERROR("Compression Failed\n");
delete[] tempBuffer;
GC_enable();
return false;
}
ASSERT(compressedLength > 0);
totalDecompLength += compressedLength;
char* compBuffer = reinterpret_cast<char*>(GC_MALLOC_ATOMIC(compressedLength));
memcpy(compBuffer, tempBuffer, compressedLength);
m_compressedData.push_back(CompressedElement(compBuffer, compressedLength));
}
delete[] tempBuffer;
m_vmInstance->compressibleStringsUncomressedBufferSize() -= decomressedBufferSize();
ASSERT(originByteLength > totalDecompLength);
m_vmInstance->decreaseSourceSize(originByteLength - totalDecompLength);
GC_enable();
// immediately free the original string after compression when there is no reference on stack
deallocateStringDataBuffer(const_cast<void*>(m_bufferData.buffer), m_bufferData.length * (m_bufferData.has8BitContent ? 1 : 2));
m_bufferData.buffer = nullptr;
m_isCompressed = true;
/*
size_t compressedSize = 0;
for (size_t i = 0; i < m_compressedData.size(); i ++) {
compressedSize += m_compressedData[i].size();
}
ESCARGOT_LOG_INFO("CompressibleString::compressWorker %fKB -> %fKB\n", originByteLength / 1024.f, compressedSize / 1024.f);
*/
Global::addHeapProfileCompTime(fastTickCount() - currentTick);
Global::increaseHeapProfileCompCount();
return true;
}
template <typename StringType>
void CompressibleString::decompressWorker()
{
ASSERT(m_isCompressed);
size_t originByteLength = m_bufferData.length * sizeof(StringType);
size_t totalDecompLength = 0;
char* dstBuffer = (char*)allocateStringDataBuffer(originByteLength);
int dstIndex = 0;
for (size_t srcIndex = 0, bufIndex = 0; srcIndex < originByteLength; srcIndex += g_compressChunkSize, bufIndex++) {
int srcSize = (int)std::min(g_compressChunkSize, originByteLength - srcIndex);
totalDecompLength += m_compressedData[bufIndex].compressedLength;
int decompressedLength = LZ4::LZ4_decompress_safe(m_compressedData[bufIndex].compressedBuffer, dstBuffer + dstIndex, m_compressedData[bufIndex].compressedLength, srcSize);
if (!decompressedLength) {
// decompress fail
RELEASE_ASSERT_NOT_REACHED();
}
dstIndex += srcSize;
}
clearCompressedData();
m_bufferData.bufferAs8Bit = const_cast<const char*>(dstBuffer);
m_isCompressed = false;
m_vmInstance->compressibleStringsUncomressedBufferSize() += decomressedBufferSize();
ASSERT(originByteLength > totalDecompLength);
m_vmInstance->increaseSourceSize(originByteLength - totalDecompLength);
ESCARGOT_LOG_INFO("DECOMPRESS TICK: %" PRIu64 " Size: %zu\n", fastTickCount(), originByteLength);
}
} // namespace Escargot
#endif // ENABLE_COMPRESSIBLE_STRING