1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:37:35 +00:00

LibJS: Fast non-local variable access :^)

This patch introduces the "environment coordinate" concept, which
encodes the distance from a variable access to the binding it ends up
resolving to.

EnvironmentCoordinate has two fields:

    - hops:  The number of hops up the lexical environment chain we have
             to make before getting to the resolved binding.

    - index: The index of the resolved binding within its declarative
             environment record.

Whenever a variable lookup resolves somewhere inside a declarative
environment, we now cache the coordinates and reuse them in subsequent
lookups. This is achieved via a coordinate cache in JS::Identifier.

Note that non-strict direct eval() breaks this optimization and so it
will not be performed if the resolved environment has been permanently
screwed by eval().

This makes variable access *significantly* faster. :^)
This commit is contained in:
Andreas Kling 2021-10-07 01:06:21 +02:00
parent 421845b0cd
commit 41a072bded
8 changed files with 65 additions and 12 deletions

View file

@ -8,6 +8,7 @@
#include <AK/String.h>
#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/EnvironmentCoordinate.h>
#include <LibJS/Runtime/PropertyName.h>
#include <LibJS/Runtime/Value.h>
@ -44,12 +45,12 @@ public:
}
}
Reference(Environment& base, FlyString referenced_name, bool strict = false, Optional<size_t> index_in_declarative_environment = {})
Reference(Environment& base, FlyString referenced_name, bool strict = false, Optional<EnvironmentCoordinate> environment_coordinate = {})
: m_base_type(BaseType::Environment)
, m_base_environment(&base)
, m_name(move(referenced_name))
, m_strict(strict)
, m_index_in_declarative_environment(move(index_in_declarative_environment))
, m_environment_coordinate(move(environment_coordinate))
{
}
@ -119,6 +120,8 @@ public:
bool is_valid_reference() const { return m_name.is_valid(); }
Optional<EnvironmentCoordinate> environment_coordinate() const { return m_environment_coordinate; }
private:
void throw_reference_error(GlobalObject&) const;
@ -130,7 +133,7 @@ private:
PropertyName m_name;
Value m_this_value;
bool m_strict { false };
Optional<size_t> m_index_in_declarative_environment;
Optional<EnvironmentCoordinate> m_environment_coordinate;
};
}