mirror of
https://github.com/dart-lang/sdk
synced 2024-09-19 20:51:50 +00:00
5cd0770c9c
As part of making the compiler and other subsystems independent of `Isolate` we have to move various state from `Isolate` to `IsolateGroup` (or to another place). The access of `ObjectStore::closure_functions()` was spread out over our codebase, including from `Isolate`. This CL moves it to a centralized place - ClosureFunctionsCache. Issue https://github.com/dart-lang/sdk/issues/36097 TEST=Pure refactoring - relying on existing test coverage. Change-Id: I3b282623f07614cdc2c2a863bd5b9d6c7748d36f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177130 Commit-Queue: Martin Kustermann <kustermann@google.com> Reviewed-by: Alexander Aprelev <aam@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
73 lines
2.7 KiB
C++
73 lines
2.7 KiB
C++
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#ifndef RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_
|
|
#define RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_
|
|
|
|
#include <functional>
|
|
|
|
#include "vm/allocation.h"
|
|
#include "vm/token_position.h"
|
|
|
|
namespace dart {
|
|
|
|
class Class;
|
|
class Function;
|
|
class FunctionPtr;
|
|
|
|
// Implementation of cache for inner closure functions.
|
|
//
|
|
// This cache is populated lazily by the compiler: When compiling a function,
|
|
// the flow graph builder will recursively traverse the kernel AST for the
|
|
// function and any inner functions. This will cause the lazy-creation of inner
|
|
// closure functions.
|
|
//
|
|
// The cache is currently implemented as O(n) lookup in a growable list.
|
|
//
|
|
// Parts of the VM have certain requirements that are maintained:
|
|
//
|
|
// * parent functions need to come before inner functions
|
|
// * closure functions list can grow while iterating
|
|
// * the index of closure function must be stable
|
|
//
|
|
// If the linear lookup turns out to be too expensive, the list of closures
|
|
// could be maintained in a hash map, with the key being the token position of
|
|
// the closure. There are almost no collisions with this simple hash value.
|
|
// However, iterating over all closure functions becomes more difficult,
|
|
// especially when the list/map changes while iterating over it (see
|
|
// requirements above).
|
|
class ClosureFunctionsCache : public AllStatic {
|
|
public:
|
|
static FunctionPtr LookupClosureFunction(const Class& owner,
|
|
TokenPosition pos);
|
|
static FunctionPtr LookupClosureFunctionLocked(const Class& owner,
|
|
TokenPosition pos);
|
|
|
|
static FunctionPtr LookupClosureFunction(const Function& parent,
|
|
TokenPosition token_pos);
|
|
static FunctionPtr LookupClosureFunctionLocked(const Function& parent,
|
|
TokenPosition token_pos);
|
|
|
|
static void AddClosureFunctionLocked(const Function& function);
|
|
|
|
static intptr_t FindClosureIndex(const Function& needle);
|
|
static FunctionPtr ClosureFunctionFromIndex(intptr_t idx);
|
|
|
|
static FunctionPtr GetUniqueInnerClosure(const Function& outer);
|
|
|
|
// Visits all closure functions registered in the object store.
|
|
//
|
|
// Iterates in-order, thereby allowing new closures being added during the
|
|
// iteration.
|
|
//
|
|
// The iteration continues until either [callback] returns `false` or all
|
|
// closure functions have been visited.
|
|
static void ForAllClosureFunctions(
|
|
std::function<bool(const Function&)> callback);
|
|
};
|
|
|
|
} // namespace dart
|
|
|
|
#endif // RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_
|