Skip to content

Commit 72b3309

Browse files
committed
Make HashTbl and Scanner an inline instance of the Parser or a stack instance instead of allocating it in the heap
Parser will never have a NULL HashTbl or Scanner.
1 parent 2c7612d commit 72b3309

File tree

9 files changed

+709
-731
lines changed

9 files changed

+709
-731
lines changed

lib/Parser/BackgroundParser.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ BackgroundParser::~BackgroundParser()
3232
static_cast<JsUtil::BackgroundJobProcessor*>(processor)->IterateBackgroundThreads([&](JsUtil::ParallelThreadData *threadData)->bool {
3333
if (threadData->parser)
3434
{
35-
threadData->parser->Release();
35+
// Adelete to make sure dtor are called (since the HashTbl has its NoReleaseAllocator)
36+
Adelete(threadData->threadArena, threadData->parser);
3637
threadData->parser = nullptr;
3738
}
3839
return false;
@@ -79,7 +80,6 @@ bool BackgroundParser::Process(JsUtil::Job *const job, JsUtil::ParallelThreadDat
7980
// the background thread to decommit its pages.
8081
threadData->parser = Anew(threadData->threadArena, Parser, this->scriptContext, backgroundItem->IsStrictMode(), &threadData->backgroundPageAllocator, true);
8182
threadData->pse = Anew(threadData->threadArena, CompileScriptException);
82-
threadData->parser->PrepareScanner(backgroundItem->GetParseContext()->fromExternal);
8383
}
8484

8585
Parser *parser = threadData->parser;
@@ -143,7 +143,8 @@ void BackgroundParser::OnDecommit(JsUtil::ParallelThreadData *threadData)
143143
{
144144
if (threadData->parser)
145145
{
146-
threadData->parser->Release();
146+
// Adelete to make sure dtor are called (since the HashTbl has its NoReleaseAllocator)
147+
Adelete(threadData->threadArena, threadData->parser);
147148
threadData->parser = nullptr;
148149
}
149150
}

lib/Parser/Hash.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@ const HashTbl::ReservedWordInfo HashTbl::s_reservedWordInfo[tkID] =
2626
#include "keywords.h"
2727
};
2828

29-
HashTbl * HashTbl::Create(uint cidHash)
30-
{
31-
HashTbl * phtbl;
32-
33-
if (nullptr == (phtbl = HeapNewNoThrow(HashTbl)))
34-
return nullptr;
35-
if (!phtbl->Init(cidHash))
36-
{
37-
delete phtbl; // invokes overridden operator delete
38-
return nullptr;
39-
}
40-
41-
return phtbl;
42-
}
43-
44-
4529
BOOL HashTbl::Init(uint cidHash)
4630
{
4731
// cidHash must be a power of two
@@ -368,11 +352,9 @@ IdentPtr HashTbl::FindExistingPid(
368352
{
369353
int32 bucketCount;
370354
IdentPtr pid;
371-
IdentPtr *ppid = &m_prgpidName[luHash & m_luMask];
372355

373356
/* Search the hash table for an existing match */
374-
ppid = &m_prgpidName[luHash & m_luMask];
375-
357+
IdentPtr *ppid = &m_prgpidName[luHash & m_luMask];
376358
for (bucketCount = 0; nullptr != (pid = *ppid); ppid = &pid->m_pidNext, bucketCount++)
377359
{
378360
if (pid->m_luHash == luHash && (int)pid->m_cch == cch &&

lib/Parser/Hash.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,17 @@ struct Ident
316316
class HashTbl
317317
{
318318
public:
319-
static HashTbl * Create(uint cidHash);
320-
321-
void Release(void)
319+
HashTbl(uint cidHash = DEFAULT_HASH_TABLE_SIZE)
322320
{
323-
delete this; // invokes overridden operator delete
321+
AssertCanHandleOutOfMemory();
322+
m_prgpidName = nullptr;
323+
memset(&m_rpid, 0, sizeof(m_rpid));
324+
if (!Init(cidHash))
325+
{
326+
Js::Throw::OutOfMemory();
327+
}
324328
}
325-
329+
~HashTbl(void) {}
326330

327331
BOOL TokIsBinop(tokens tk, int *popl, OpCode *pnop)
328332
{
@@ -392,25 +396,15 @@ class HashTbl
392396
}
393397

394398
private:
399+
static const uint DEFAULT_HASH_TABLE_SIZE = 256;
400+
395401
NoReleaseAllocator m_noReleaseAllocator; // to allocate identifiers
396402
Ident ** m_prgpidName; // hash table for names
397403

398404
uint32 m_luMask; // hash mask
399405
uint32 m_luCount; // count of the number of entires in the hash table
400406
IdentPtr m_rpid[tkLimKwd];
401407

402-
HashTbl()
403-
{
404-
m_prgpidName = nullptr;
405-
memset(&m_rpid, 0, sizeof(m_rpid));
406-
}
407-
~HashTbl(void) {}
408-
409-
void operator delete(void* p, size_t size)
410-
{
411-
HeapFree(p, size);
412-
}
413-
414408
// Called to grow the number of buckets in the table to reduce the table density.
415409
void Grow();
416410

0 commit comments

Comments
 (0)