Commit 3a9c7b55 authored by verwaest's avatar verwaest Committed by Commit bot

Store NonLocals in variables_

Now that ordered_variables_ is used to find non-dynamic variables, and NonLocals are always stored in the scope that introduces them, we can rely on variables_ to also cache non-locals. This has 2 advantages:
1) we don't need DynamicScopePart anymore, reducing all scopes by a pointer
2) upon second lookup of a non-local we don't need to walk the entire chain anymore. The cached value will immediately be found.

BUG=v8:5209

Review-Url: https://codereview.chromium.org/2276483003
Cr-Commit-Position: refs/heads/master@{#38855}
parent a5c29021
......@@ -217,7 +217,6 @@ void Scope::SetDefaults() {
inner_scope_ = nullptr;
sibling_ = nullptr;
unresolved_ = nullptr;
dynamics_ = nullptr;
start_position_ = kNoSourcePosition;
end_position_ = kNoSourcePosition;
......@@ -1077,14 +1076,16 @@ static void PrintVar(int indent, Variable* var) {
}
}
static void PrintMap(int indent, VariableMap* map) {
for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
static void PrintMap(int indent, VariableMap* map, bool locals) {
for (VariableMap::Entry* p = map->Start(); p != nullptr; p = map->Next(p)) {
Variable* var = reinterpret_cast<Variable*>(p->value);
if (var == NULL) {
Indent(indent, "<?>\n");
} else {
PrintVar(indent, var);
bool local = !IsDynamicVariableMode(var->mode());
if (locals ? local : !local) {
if (var == nullptr) {
Indent(indent, "<?>\n");
} else {
PrintVar(indent, var);
}
}
}
}
......@@ -1173,14 +1174,10 @@ void Scope::Print(int n) {
if (variables_.Start() != NULL) {
Indent(n1, "// local vars:\n");
PrintMap(n1, &variables_);
}
PrintMap(n1, &variables_, true);
if (dynamics_ != NULL) {
Indent(n1, "// dynamic vars:\n");
PrintMap(n1, dynamics_->GetMap(DYNAMIC));
PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL));
PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL));
PrintMap(n1, &variables_, false);
}
// Print inner scopes (disable by providing negative n).
......@@ -1215,17 +1212,12 @@ void Scope::CheckZones() {
#endif // DEBUG
Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) {
if (dynamics_ == NULL) dynamics_ = new (zone()) DynamicScopePart(zone());
VariableMap* map = dynamics_->GetMap(mode);
Variable* var = map->Lookup(name);
if (var == NULL) {
// Declare a new non-local.
DCHECK(!IsLexicalVariableMode(mode));
var = map->Declare(zone(), NULL, name, mode, Variable::NORMAL,
kCreatedInitialized);
// Allocate it by giving it a dynamic lookup.
var->AllocateTo(VariableLocation::LOOKUP, -1);
}
// Declare a new non-local.
DCHECK(IsDynamicVariableMode(mode));
Variable* var = variables_.Declare(zone(), NULL, name, mode, Variable::NORMAL,
kCreatedInitialized);
// Allocate it by giving it a dynamic lookup.
var->AllocateTo(VariableLocation::LOOKUP, -1);
return var;
}
......
......@@ -30,28 +30,6 @@ class VariableMap: public ZoneHashMap {
};
// The dynamic scope part holds hash maps for the variables that will
// be looked up dynamically from within eval and with scopes. The objects
// are allocated on-demand from Scope::NonLocal to avoid wasting memory
// and setup time for scopes that don't need them.
class DynamicScopePart : public ZoneObject {
public:
explicit DynamicScopePart(Zone* zone) {
for (int i = 0; i < 3; i++)
maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone);
}
VariableMap* GetMap(VariableMode mode) {
int index = mode - DYNAMIC;
DCHECK(index >= 0 && index < 3);
return maps_[index];
}
private:
VariableMap *maps_[3];
};
// Sloppy block-scoped function declarations to var-bind
class SloppyBlockFunctionMap : public ZoneHashMap {
public:
......@@ -474,8 +452,6 @@ class Scope: public ZoneObject {
// map above in order of addition.
// TODO(verwaest): Thread through Variable.
ZoneList<Variable*> ordered_variables_;
// Variables that must be looked up dynamically.
DynamicScopePart* dynamics_;
// Unresolved variables referred to from this scope. The proxies themselves
// form a linked list of all unresolved proxies.
VariableProxy* unresolved_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment