Skip to content

Commit a1efb41

Browse files
camillobruniCommit Bot
authored andcommitted
[parser] Various Scope and Context speedups
- Dehandlify ScopeInfo::ContextSlotIndex - Dehandlify ScriptContextTable::Lookup - Introduce function-kind.h with range-based helper methods - Spread usage of Scope::is_script_scope and friends Change-Id: I8ed1d82cc5bb9ea3fce856e16e9eafe194fb57ba Reviewed-on: https://chromium-review.googlesource.com/c/1430100 Reviewed-by: Leszek Swirski <[email protected]> Reviewed-by: Jaroslav Sevcik <[email protected]> Reviewed-by: Ulan Degenbaev <[email protected]> Reviewed-by: Maya Lekova <[email protected]> Reviewed-by: Toon Verwaest <[email protected]> Commit-Queue: Camillo Bruni <[email protected]> Cr-Commit-Position: refs/heads/master@{#59120}
1 parent 1658dc8 commit a1efb41

32 files changed

+441
-230
lines changed

BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,7 @@ v8_source_set("v8_base") {
19621962
"src/frames-inl.h",
19631963
"src/frames.cc",
19641964
"src/frames.h",
1965+
"src/function-kind.h",
19651966
"src/futex-emulation.cc",
19661967
"src/futex-emulation.h",
19671968
"src/gdb-jit.cc",

src/ast/prettyprinter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "src/allocation.h"
99
#include "src/ast/ast.h"
1010
#include "src/base/compiler-specific.h"
11+
#include "src/function-kind.h"
1112

1213
namespace v8 {
1314
namespace internal {

src/ast/scopes.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,7 @@ bool DeclarationScope::Analyze(ParseInfo* info) {
547547
// 1) top-level code,
548548
// 2) a function/eval/module on the top-level
549549
// 3) a function/eval in a scope that was already resolved.
550-
DCHECK(scope->scope_type() == SCRIPT_SCOPE ||
551-
scope->outer_scope()->scope_type() == SCRIPT_SCOPE ||
550+
DCHECK(scope->is_script_scope() || scope->outer_scope()->is_script_scope() ||
552551
scope->outer_scope()->already_resolved_);
553552

554553
// The outer scope is never lazy.
@@ -786,8 +785,9 @@ void Scope::ReplaceOuterScope(Scope* outer) {
786785
Variable* Scope::LookupInScopeInfo(const AstRawString* name, Scope* cache) {
787786
DCHECK(!scope_info_.is_null());
788787
DCHECK_NULL(cache->variables_.Lookup(name));
788+
DisallowHeapAllocation no_gc;
789789

790-
Handle<String> name_handle = name->string();
790+
String name_handle = *name->string();
791791
// The Scope is backed up by ScopeInfo. This means it cannot operate in a
792792
// heap-independent mode, and all strings must be internalized immediately. So
793793
// it's ok to get the Handle<String> here.
@@ -801,20 +801,20 @@ Variable* Scope::LookupInScopeInfo(const AstRawString* name, Scope* cache) {
801801

802802
{
803803
location = VariableLocation::CONTEXT;
804-
index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode,
804+
index = ScopeInfo::ContextSlotIndex(*scope_info_, name_handle, &mode,
805805
&init_flag, &maybe_assigned_flag);
806806
found = index >= 0;
807807
}
808808

809-
if (!found && scope_type() == MODULE_SCOPE) {
809+
if (!found && is_module_scope()) {
810810
location = VariableLocation::MODULE;
811811
index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag,
812812
&maybe_assigned_flag);
813813
found = index != 0;
814814
}
815815

816816
if (!found) {
817-
index = scope_info_->FunctionContextSlotIndex(*name_handle);
817+
index = scope_info_->FunctionContextSlotIndex(name_handle);
818818
if (index < 0) return nullptr; // Nowhere found.
819819
Variable* var = AsDeclarationScope()->DeclareFunctionVar(name, cache);
820820
DCHECK_EQ(VariableMode::kConst, var->mode());

src/ast/scopes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "src/ast/ast.h"
99
#include "src/base/compiler-specific.h"
1010
#include "src/base/hashmap.h"
11+
#include "src/function-kind.h"
1112
#include "src/globals.h"
1213
#include "src/objects.h"
1314
#include "src/pointer-with-payload.h"

src/bootstrapper.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "src/extensions/ignition-statistics-extension.h"
1818
#include "src/extensions/statistics-extension.h"
1919
#include "src/extensions/trigger-failure-extension.h"
20+
#include "src/function-kind.h"
2021
#include "src/heap/heap.h"
2122
#include "src/isolate-inl.h"
2223
#include "src/math-random.h"

src/code-stub-assembler.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "src/counters.h"
99
#include "src/frames-inl.h"
1010
#include "src/frames.h"
11+
#include "src/function-kind.h"
1112
#include "src/objects/api-callbacks.h"
1213
#include "src/objects/cell.h"
1314
#include "src/objects/descriptor-array.h"

src/compiler/js-heap-broker.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,8 +1924,8 @@ ScriptContextTableRef::lookup(const NameRef& name) const {
19241924
if (!name.IsString()) return {};
19251925
ScriptContextTable::LookupResult lookup_result;
19261926
auto table = object();
1927-
if (!ScriptContextTable::Lookup(broker()->isolate(), table,
1928-
name.AsString().object(), &lookup_result)) {
1927+
if (!ScriptContextTable::Lookup(broker()->isolate(), *table,
1928+
*name.AsString().object(), &lookup_result)) {
19291929
return {};
19301930
}
19311931
Handle<Context> script_context = ScriptContextTable::GetContext(

src/compiler/js-heap-broker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "src/base/compiler-specific.h"
99
#include "src/base/optional.h"
1010
#include "src/compiler/refs-map.h"
11+
#include "src/function-kind.h"
1112
#include "src/globals.h"
1213
#include "src/handles.h"
1314
#include "src/objects.h"

src/contexts-inl.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ void ScriptContextTable::set_used(int used) {
3535
Handle<Context> ScriptContextTable::GetContext(Isolate* isolate,
3636
Handle<ScriptContextTable> table,
3737
int i) {
38-
DCHECK(i < table->used());
39-
return Handle<Context>::cast(
40-
FixedArray::get(*table, i + kFirstContextSlotIndex, isolate));
38+
return handle(table->get_context(i), isolate);
39+
}
40+
41+
Context ScriptContextTable::get_context(int i) const {
42+
DCHECK_LT(i, used());
43+
return Context::cast(this->get(i + kFirstContextSlotIndex));
4144
}
4245

4346
OBJECT_CONSTRUCTORS_IMPL(Context, HeapObject)
@@ -193,8 +196,7 @@ int Context::FunctionMapIndex(LanguageMode language_mode, FunctionKind kind,
193196

194197
base = ASYNC_FUNCTION_MAP_INDEX;
195198

196-
} else if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
197-
IsAccessorFunction(kind)) {
199+
} else if (IsStrictFunctionWithoutPrototype(kind)) {
198200
DCHECK_IMPLIES(IsArrowFunction(kind), !needs_home_object);
199201
CHECK_FOLLOWS4(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
200202
METHOD_WITH_NAME_MAP_INDEX,

src/contexts.cc

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ Handle<ScriptContextTable> ScriptContextTable::Extend(
3737
return result;
3838
}
3939

40-
bool ScriptContextTable::Lookup(Isolate* isolate,
41-
Handle<ScriptContextTable> table,
42-
Handle<String> name, LookupResult* result) {
40+
bool ScriptContextTable::Lookup(Isolate* isolate, ScriptContextTable table,
41+
String name, LookupResult* result) {
42+
DisallowHeapAllocation no_gc;
4343
for (int i = 0; i < table->used(); i++) {
44-
Handle<Context> context = GetContext(isolate, table, i);
44+
Context context = table->get_context(i);
4545
DCHECK(context->IsScriptContext());
46-
Handle<ScopeInfo> scope_info(context->scope_info(), context->GetIsolate());
4746
int slot_index = ScopeInfo::ContextSlotIndex(
48-
scope_info, name, &result->mode, &result->init_flag,
47+
context->scope_info(), name, &result->mode, &result->init_flag,
4948
&result->maybe_assigned_flag);
5049

5150
if (slot_index >= 0) {
@@ -57,7 +56,6 @@ bool ScriptContextTable::Lookup(Isolate* isolate,
5756
return false;
5857
}
5958

60-
6159
bool Context::is_declaration_context() {
6260
if (IsFunctionContext() || IsNativeContext() || IsScriptContext() ||
6361
IsModuleContext()) {
@@ -211,27 +209,25 @@ Handle<Object> Context::Lookup(Handle<Context> context, Handle<String> name,
211209
Handle<JSReceiver> object(context->extension_receiver(), isolate);
212210

213211
if (context->IsNativeContext()) {
212+
DisallowHeapAllocation no_gc;
214213
if (FLAG_trace_contexts) {
215214
PrintF(" - trying other script contexts\n");
216215
}
217216
// Try other script contexts.
218-
Handle<ScriptContextTable> script_contexts(
219-
context->global_object()->native_context()->script_context_table(),
220-
isolate);
217+
ScriptContextTable script_contexts =
218+
context->global_object()->native_context()->script_context_table();
221219
ScriptContextTable::LookupResult r;
222-
if (ScriptContextTable::Lookup(isolate, script_contexts, name, &r)) {
220+
if (ScriptContextTable::Lookup(isolate, script_contexts, *name, &r)) {
221+
Context context = script_contexts->get_context(r.context_index);
223222
if (FLAG_trace_contexts) {
224-
Handle<Context> c = ScriptContextTable::GetContext(
225-
isolate, script_contexts, r.context_index);
226223
PrintF("=> found property in script context %d: %p\n",
227-
r.context_index, reinterpret_cast<void*>(c->ptr()));
224+
r.context_index, reinterpret_cast<void*>(context->ptr()));
228225
}
229226
*index = r.slot_index;
230227
*variable_mode = r.mode;
231228
*init_flag = r.init_flag;
232229
*attributes = GetAttributesForMode(r.mode);
233-
return ScriptContextTable::GetContext(isolate, script_contexts,
234-
r.context_index);
230+
return handle(context, isolate);
235231
}
236232
}
237233

@@ -285,13 +281,14 @@ Handle<Object> Context::Lookup(Handle<Context> context, Handle<String> name,
285281
if (context->IsFunctionContext() || context->IsBlockContext() ||
286282
context->IsScriptContext() || context->IsEvalContext() ||
287283
context->IsModuleContext() || context->IsCatchContext()) {
284+
DisallowHeapAllocation no_gc;
288285
// Use serialized scope information of functions and blocks to search
289286
// for the context index.
290-
Handle<ScopeInfo> scope_info(context->scope_info(), isolate);
287+
ScopeInfo scope_info = context->scope_info();
291288
VariableMode mode;
292289
InitializationFlag flag;
293290
MaybeAssignedFlag maybe_assigned_flag;
294-
int slot_index = ScopeInfo::ContextSlotIndex(scope_info, name, &mode,
291+
int slot_index = ScopeInfo::ContextSlotIndex(scope_info, *name, &mode,
295292
&flag, &maybe_assigned_flag);
296293
DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
297294
if (slot_index >= 0) {
@@ -334,7 +331,7 @@ Handle<Object> Context::Lookup(Handle<Context> context, Handle<String> name,
334331
InitializationFlag flag;
335332
MaybeAssignedFlag maybe_assigned_flag;
336333
int cell_index =
337-
scope_info->ModuleIndex(name, &mode, &flag, &maybe_assigned_flag);
334+
scope_info->ModuleIndex(*name, &mode, &flag, &maybe_assigned_flag);
338335
if (cell_index != 0) {
339336
if (FLAG_trace_contexts) {
340337
PrintF("=> found in module imports or exports\n");

0 commit comments

Comments
 (0)