Skip to content

Commit

Permalink
Add Generic-BlockInfo for reducing memory usage of BlockInfo
Browse files Browse the repository at this point in the history
Signed-off-by: Seonghyun Kim <[email protected]>
  • Loading branch information
ksh8281 committed Dec 28, 2023
1 parent b0543cf commit 1b9be45
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 183 deletions.
57 changes: 30 additions & 27 deletions src/codecache/CodeCacheReaderWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,17 @@ void CodeCacheWriter::storeInterpretedCodeBlock(InterpretedCodeBlock* codeBlock)
size = blockInfoVector.size();
m_buffer.ensureSize(sizeof(size_t));
m_buffer.put(size);
// FIXME ignore BlockInfo::m_loc member
for (size_t i = 0; i < size; i++) {
m_buffer.ensureSize(2 * sizeof(bool) + 3 * sizeof(uint16_t));
const InterpretedCodeBlock::BlockInfo* info = blockInfoVector[i];
m_buffer.put(info->m_canAllocateEnvironmentOnStack);
m_buffer.put(info->m_shouldAllocateEnvironment);
m_buffer.put((uint16_t)info->m_nodeType);
m_buffer.put((uint16_t)info->m_parentBlockIndex);
m_buffer.put((uint16_t)info->m_blockIndex);
m_buffer.put(info->canAllocateEnvironmentOnStack());
m_buffer.put(info->shouldAllocateEnvironment());
m_buffer.put(info->fromCatchClauseNode());
m_buffer.put((uint16_t)info->parentBlockIndex());
m_buffer.put((uint16_t)info->blockIndex());

// InterpretedCodeBlock::BlockInfo::m_identifiers
const InterpretedCodeBlock::BlockIdentifierInfoVector& infoVector = info->m_identifiers;
const InterpretedCodeBlock::BlockIdentifierInfoVector& infoVector = info->identifiers();
const size_t vectorSize = infoVector.size();
m_buffer.ensureSize(sizeof(size_t) + vectorSize * (2 * sizeof(bool) + 2 * sizeof(size_t)));
m_buffer.put(vectorSize);
Expand Down Expand Up @@ -738,27 +737,31 @@ InterpretedCodeBlock* CodeCacheReader::loadInterpretedCodeBlock(Context* context
size = m_buffer.get<size_t>();
blockInfoVector.resizeWithUninitializedValues(size);
for (size_t i = 0; i < size; i++) {
InterpretedCodeBlock::BlockInfo* info = new InterpretedCodeBlock::BlockInfo(
#ifndef NDEBUG
ExtendedNodeLOC(0, 0, 0)
#endif
);

info->m_canAllocateEnvironmentOnStack = m_buffer.get<bool>();
info->m_shouldAllocateEnvironment = m_buffer.get<bool>();
info->m_nodeType = (ASTNodeType)m_buffer.get<uint16_t>();
info->m_parentBlockIndex = (LexicalBlockIndex)m_buffer.get<uint16_t>();
info->m_blockIndex = (LexicalBlockIndex)m_buffer.get<uint16_t>();

bool canAllocateEnvironmentOnStack = m_buffer.get<bool>();
bool shouldAllocateEnvironment = m_buffer.get<bool>();
bool fromCatchClauseNode = m_buffer.get<bool>();
LexicalBlockIndex parentBlockIndex = (LexicalBlockIndex)m_buffer.get<uint16_t>();
LexicalBlockIndex blockIndex = (LexicalBlockIndex)m_buffer.get<uint16_t>();
size_t vectorSize = m_buffer.get<size_t>();
info->m_identifiers.resizeWithUninitializedValues(vectorSize);
for (size_t j = 0; j < vectorSize; j++) {
InterpretedCodeBlock::BlockIdentifierInfo idInfo;
idInfo.m_needToAllocateOnStack = m_buffer.get<bool>();
idInfo.m_isMutable = m_buffer.get<bool>();
idInfo.m_indexForIndexedStorage = m_buffer.get<size_t>();
idInfo.m_name = m_stringTable->get(m_buffer.get<size_t>());
info->m_identifiers[j] = idInfo;

InterpretedCodeBlock::BlockInfo* info = InterpretedCodeBlock::BlockInfo::create(
canAllocateEnvironmentOnStack,
shouldAllocateEnvironment,
fromCatchClauseNode,
parentBlockIndex,
blockIndex,
vectorSize);
if (vectorSize) {
ASSERT(!info->isGenericBlockInfo());
info->identifiers().resizeWithUninitializedValues(vectorSize);
for (size_t j = 0; j < vectorSize; j++) {
InterpretedCodeBlock::BlockIdentifierInfo idInfo;
idInfo.m_needToAllocateOnStack = m_buffer.get<bool>();
idInfo.m_isMutable = m_buffer.get<bool>();
idInfo.m_indexForIndexedStorage = m_buffer.get<size_t>();
idInfo.m_name = m_stringTable->get(m_buffer.get<size_t>());
info->identifiers()[j] = idInfo;
}
}

blockInfoVector[i] = info;
Expand Down
2 changes: 1 addition & 1 deletion src/heap/LeakCheckerBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Escargot {
Value builtinRegisterLeakCheck(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (!argv[0].isPointerValue()) {
ErrorObject::throwBuiltinError(state, ErrorObject::None, "builtinRegisterLeakCheck should get pointer-type argument");
ErrorObject::throwBuiltinError(state, ErrorCode::None, "builtinRegisterLeakCheck should get pointer-type argument");
}

PointerValue* ptr = argv[0].asPointerValue();
Expand Down
6 changes: 3 additions & 3 deletions src/interpreter/ByteCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ void ByteCodeBlock::initFunctionDeclarationWithinBlock(ByteCodeGenerateContext*
context->m_isFunctionDeclarationBindingInitialization = true;
}

for (size_t i = 0; i < blockInfo->m_identifiers.size(); i++) {
if (blockInfo->m_identifiers[i].m_name == child->functionName()) {
for (size_t i = 0; i < blockInfo->identifiers().size(); i++) {
if (blockInfo->identifiers()[i].m_name == child->functionName()) {
context->m_isLexicallyDeclaredBindingInitialization = true;
break;
}
Expand All @@ -286,7 +286,7 @@ ByteCodeBlock::ByteCodeLexicalBlockContext ByteCodeBlock::pushLexicalBlock(ByteC
ByteCodeBlock::ByteCodeLexicalBlockContext ctx;
ctx.lexicallyDeclaredNamesCount = context->m_lexicallyDeclaredNames->size();

if (blockInfo->m_shouldAllocateEnvironment) {
if (blockInfo->shouldAllocateEnvironment()) {
ctx.lexicalBlockSetupStartPosition = currentCodeSize();
context->m_recursiveStatementStack.push_back(std::make_pair(ByteCodeGenerateContext::Block, ctx.lexicalBlockSetupStartPosition));
this->pushCode(BlockOperation(ByteCodeLOC(node->m_loc.index), blockInfo), context, SIZE_MAX);
Expand Down
1 change: 1 addition & 0 deletions src/interpreter/ByteCodeBlockData.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Escargot {

class ByteCodeBlockData {
typedef ComputeReservedCapacityFunctionWithLog2<200> ComputeReservedCapacityFunction;

public:
ByteCodeBlockData()
: m_buffer(nullptr)
Expand Down
10 changes: 5 additions & 5 deletions src/interpreter/ByteCodeInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3575,13 +3575,13 @@ NEVER_INLINE void InterpreterSlowPath::replaceBlockLexicalEnvironmentOperation(E

bool shouldUseIndexedStorage = byteCodeBlock->m_codeBlock->canUseIndexedVariableStorage();
InterpretedCodeBlock::BlockInfo* blockInfo = reinterpret_cast<InterpretedCodeBlock::BlockInfo*>(code->m_blockInfo);
ASSERT(blockInfo && blockInfo->m_shouldAllocateEnvironment);
ASSERT(blockInfo && blockInfo->shouldAllocateEnvironment());
if (LIKELY(shouldUseIndexedStorage)) {
newRecord = new DeclarativeEnvironmentRecordIndexed(state, blockInfo);
} else {
newRecord = new DeclarativeEnvironmentRecordNotIndexed(state);

auto& iv = blockInfo->m_identifiers;
auto& iv = blockInfo->identifiers();
auto siz = iv.size();
for (size_t i = 0; i < siz; i++) {
newRecord->createBinding(state, iv[i].m_name, false, iv[i].m_isMutable, false);
Expand All @@ -3608,13 +3608,13 @@ NEVER_INLINE Value InterpreterSlowPath::blockOperation(ExecutionState*& state, B

if (LIKELY(!inPauserResumeProcess)) {
InterpretedCodeBlock::BlockInfo* blockInfo = reinterpret_cast<InterpretedCodeBlock::BlockInfo*>(code->m_blockInfo);
ASSERT(blockInfo->m_shouldAllocateEnvironment);
ASSERT(blockInfo->shouldAllocateEnvironment());
if (LIKELY(shouldUseIndexedStorage)) {
newRecord = new DeclarativeEnvironmentRecordIndexed(*state, blockInfo);
} else {
newRecord = new DeclarativeEnvironmentRecordNotIndexed(*state, false, blockInfo->m_nodeType == ASTNodeType::CatchClause);
newRecord = new DeclarativeEnvironmentRecordNotIndexed(*state, false, blockInfo->fromCatchClauseNode());

auto& iv = blockInfo->m_identifiers;
auto& iv = blockInfo->identifiers();
auto siz = iv.size();
for (size_t i = 0; i < siz; i++) {
newRecord->createBinding(*state, iv[i].m_name, false, iv[i].m_isMutable, false);
Expand Down
Loading

0 comments on commit 1b9be45

Please sign in to comment.