
This gives FunctionNode a "might need arguments object" boolean flag and sets it based on the simplest possible heuristic for this: if we encounter an identifier called "arguments" or "eval" up to the next (nested) function declaration or expression, we won't need an arguments object. Otherwise, we *might* need one - the final decision is made in the FunctionDeclarationInstantiation AO. Now, this is obviously not perfect. Even if you avoid eval, something like `foo.arguments` will still trigger a false positive - but it's a start and already massively cuts down on needlessly allocated objects, especially in real-world code that is often minified, and so a full "arguments" identifier will be an actual arguments object more often than not. To illustrate the actual impact of this change, here's the number of allocated arguments objects during a full test-js run: Before: - Unmapped arguments objects: 78765 - Mapped arguments objects: 2455 After: - Unmapped arguments objects: 18 - Mapped arguments objects: 37 This results in a ~5% speedup of test-js on my Linux host machine, and about 3.5% on i686 Serenity in QEMU (warm runs, average of 5). The following microbenchmark (calling an empty function 1M times) runs 25% faster on Linux and 45% on Serenity: function foo() {} for (var i = 0; i < 1_000_000; ++i) foo(); test262 reports no changes in either direction, apart from a speedup :^)
68 lines
2.5 KiB
C++
68 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/Debug.h>
|
|
#include <AK/Optional.h>
|
|
#include <LibJS/Bytecode/Interpreter.h>
|
|
#include <LibJS/Lexer.h>
|
|
#include <LibJS/Parser.h>
|
|
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
|
|
#include <LibJS/Runtime/FunctionConstructor.h>
|
|
#include <LibJS/Runtime/GeneratorFunctionConstructor.h>
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
namespace JS {
|
|
|
|
GeneratorFunctionConstructor::GeneratorFunctionConstructor(GlobalObject& global_object)
|
|
: NativeFunction(*static_cast<Object*>(global_object.function_constructor()))
|
|
{
|
|
}
|
|
|
|
void GeneratorFunctionConstructor::initialize(GlobalObject& global_object)
|
|
{
|
|
auto& vm = this->vm();
|
|
NativeFunction::initialize(global_object);
|
|
|
|
// 27.3.2.1 GeneratorFunction.length, https://tc39.es/ecma262/#sec-generatorfunction.length
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
|
// 27.3.2.2 GeneratorFunction.prototype, https://tc39.es/ecma262/#sec-generatorfunction.length
|
|
define_direct_property(vm.names.prototype, global_object.generator_function_prototype(), 0);
|
|
}
|
|
|
|
GeneratorFunctionConstructor::~GeneratorFunctionConstructor()
|
|
{
|
|
}
|
|
|
|
// 27.3.1.1 GeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-generatorfunction
|
|
Value GeneratorFunctionConstructor::call()
|
|
{
|
|
return construct(*this);
|
|
}
|
|
|
|
// 27.3.1.1 GeneratorFunction ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-generatorfunction
|
|
Value GeneratorFunctionConstructor::construct(FunctionObject& new_target)
|
|
{
|
|
auto function = FunctionConstructor::create_dynamic_function_node(global_object(), new_target, FunctionKind::Generator);
|
|
if (!function)
|
|
return {};
|
|
|
|
auto* bytecode_interpreter = Bytecode::Interpreter::current();
|
|
VERIFY(bytecode_interpreter);
|
|
|
|
auto executable = Bytecode::Generator::generate(function->body(), true);
|
|
auto& passes = JS::Bytecode::Interpreter::optimization_pipeline();
|
|
passes.perform(executable);
|
|
if constexpr (JS_BYTECODE_DEBUG) {
|
|
dbgln("Optimisation passes took {}us", passes.elapsed());
|
|
dbgln("Compiled Bytecode::Block for function '{}':", function->name());
|
|
for (auto& block : executable.basic_blocks)
|
|
block.dump(executable);
|
|
}
|
|
|
|
return ECMAScriptFunctionObject::create(global_object(), function->name(), function->body(), function->parameters(), function->function_length(), vm().lexical_environment(), FunctionKind::Generator, function->is_strict_mode(), function->might_need_arguments_object());
|
|
}
|
|
|
|
}
|