mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:32:44 +00:00 
			
		
		
		
	 4699c81fc1
			
		
	
	
		4699c81fc1
		
	
	
	
	
		
			
			This patch makes IteratorRecord an Object. Although it's not exposed to
author code, this does allow us to store it in a VM register.
Now that we can store it in a VM register, we don't need to convert it
back and forth between IteratorRecord and Object when accessing it from
bytecode.
The big win here is avoiding 3 [[Get]] accesses on every iteration step
of for..of loops. There are also a bunch of smaller efficiencies gained.
20% speed-up on this microbenchmark:
    function go(a) {
        for (const p of a) {
        }
    }
    const a = [];
    a.length = 1_000_000;
    go(a);
		
	
			
		
			
				
	
	
		
			67 lines
		
	
	
	
		
			2.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
	
		
			2.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibJS/Runtime/GlobalObject.h>
 | |
| #include <LibJS/Runtime/Iterator.h>
 | |
| #include <LibJS/Runtime/IteratorHelper.h>
 | |
| #include <LibJS/Runtime/Realm.h>
 | |
| 
 | |
| namespace JS {
 | |
| 
 | |
| JS_DEFINE_ALLOCATOR(IteratorHelper);
 | |
| 
 | |
| ThrowCompletionOr<NonnullGCPtr<IteratorHelper>> IteratorHelper::create(Realm& realm, NonnullGCPtr<IteratorRecord> underlying_iterator, Closure closure, Optional<AbruptClosure> abrupt_closure)
 | |
| {
 | |
|     return realm.heap().allocate<IteratorHelper>(realm, realm, realm.intrinsics().iterator_helper_prototype(), move(underlying_iterator), move(closure), move(abrupt_closure));
 | |
| }
 | |
| 
 | |
| IteratorHelper::IteratorHelper(Realm& realm, Object& prototype, NonnullGCPtr<IteratorRecord> underlying_iterator, Closure closure, Optional<AbruptClosure> abrupt_closure)
 | |
|     : GeneratorObject(realm, prototype, realm.vm().running_execution_context().copy(), "Iterator Helper"sv)
 | |
|     , m_underlying_iterator(move(underlying_iterator))
 | |
|     , m_closure(move(closure))
 | |
|     , m_abrupt_closure(move(abrupt_closure))
 | |
| {
 | |
| }
 | |
| 
 | |
| void IteratorHelper::visit_edges(Visitor& visitor)
 | |
| {
 | |
|     Base::visit_edges(visitor);
 | |
|     visitor.visit(m_underlying_iterator);
 | |
| }
 | |
| 
 | |
| Value IteratorHelper::result(Value value)
 | |
| {
 | |
|     set_generator_state(value.is_undefined() ? GeneratorState::Completed : GeneratorState::SuspendedYield);
 | |
|     return value;
 | |
| }
 | |
| 
 | |
| ThrowCompletionOr<Value> IteratorHelper::close_result(VM& vm, Completion completion)
 | |
| {
 | |
|     set_generator_state(GeneratorState::Completed);
 | |
|     return *TRY(iterator_close(vm, underlying_iterator(), move(completion)));
 | |
| }
 | |
| 
 | |
| ThrowCompletionOr<Value> IteratorHelper::execute(VM& vm, JS::Completion const& completion)
 | |
| {
 | |
|     ScopeGuard guard { [&] { vm.pop_execution_context(); } };
 | |
| 
 | |
|     if (completion.is_abrupt()) {
 | |
|         if (m_abrupt_closure.has_value())
 | |
|             return (*m_abrupt_closure)(vm, *this, completion);
 | |
|         return close_result(vm, completion);
 | |
|     }
 | |
| 
 | |
|     auto result_value = m_closure(vm, *this);
 | |
| 
 | |
|     if (result_value.is_throw_completion()) {
 | |
|         set_generator_state(GeneratorState::Completed);
 | |
|         return result_value;
 | |
|     }
 | |
| 
 | |
|     return create_iterator_result_object(vm, result(result_value.release_value()), generator_state() == GeneratorState::Completed);
 | |
| }
 | |
| 
 | |
| }
 |