[webkit-changes] [WebKit/WebKit] 339bfd: [WTF] Add LazyRef & LazyUniqueRef

Yusuke Suzuki noreply at github.com
Sun Jan 21 23:11:17 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: 339bfd3e4a54c1e215bc300530670cfb2350b04d
      https://github.com/WebKit/WebKit/commit/339bfd3e4a54c1e215bc300530670cfb2350b04d
  Author: Yusuke Suzuki <ysuzuki at apple.com>
  Date:   2024-01-21 (Sun, 21 Jan 2024)

  Changed paths:
    M Source/JavaScriptCore/runtime/HasOwnPropertyCache.h
    M Source/JavaScriptCore/runtime/LazyPropertyInlines.h
    M Source/JavaScriptCore/runtime/VM.cpp
    M Source/JavaScriptCore/runtime/VM.h
    M Source/WTF/WTF.xcodeproj/project.pbxproj
    M Source/WTF/wtf/CMakeLists.txt
    A Source/WTF/wtf/LazyRef.h
    A Source/WTF/wtf/LazyUniqueRef.h
    M Tools/TestWebKitAPI/CMakeLists.txt
    M Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
    A Tools/TestWebKitAPI/Tests/WTF/LazyRef.cpp
    A Tools/TestWebKitAPI/Tests/WTF/LazyUniqueRef.cpp

  Log Message:
  -----------
  [WTF] Add LazyRef & LazyUniqueRef
https://bugs.webkit.org/show_bug.cgi?id=267830
rdar://121328458

Reviewed by Ryosuke Niwa.

This patch adds LazyRef and LazyUniqueRef. This is similar to LazyProperty in JSC.
We can set *stateless* lambda in the constructor side of the owner object so that it offers clean interface for lazy initialization,
which does not mess up the owner object's interface.
For example,

    LazyUniqueRef<VM, Property> m_property;
    Property& property() { return m_property.get(*this); }

First parameter is owner, which needs to be passed to `get`. And then, this `get` will automatically initialize if it is not initialized.
And the initialization code can be freely customized, but you do not need to define it as a VM's member! Instead, you can define it as a lambda inside VM.cpp.

    // This lambda needs to be stateless. So it must not capture anything (otherwise, it hits crash because of RELEASE_ASSERT anyway).
    m_property.initLater(
        [](VM& vm, auto& ref) {
            // You can do whatever. Even you can invoke the other property which can be further lazily initialized (so, it implicitly creates dependency graph of initialization).
            ref.set(Property::create());
            // And further, you can do anything after setting it. So these operations can see Property& via `vm.property()`.
        });

Or you can set lambda in constructor, so like, VM's constructor list,

    : m_property(
        [](VM& vm, auto& ref) {
            // You can do whatever. Even you can invoke the other property which can be further lazily initialized (so, it implicitly creates dependency graph of initialization).
            ref.set(Property::create());
            // And further, you can do anything after setting it. So these operations can see Property& via `vm.property()`.
        })
    , m_other()
    , ...

This patch applies this to some of JSC::VM's fields, making definitions much cleaner by moving all messy parts into VM.cpp side and clean up VM.h's interface.

* Source/JavaScriptCore/runtime/HasOwnPropertyCache.h:
(JSC::HasOwnPropertyCache::Entry::offsetOfStructureID): Deleted.
(JSC::HasOwnPropertyCache::Entry::offsetOfImpl): Deleted.
(JSC::HasOwnPropertyCache::Entry::offsetOfResult): Deleted.
(JSC::HasOwnPropertyCache::operator delete): Deleted.
(JSC::HasOwnPropertyCache::create): Deleted.
(JSC::HasOwnPropertyCache::hash): Deleted.
(JSC::HasOwnPropertyCache::get): Deleted.
(JSC::HasOwnPropertyCache::tryAdd): Deleted.
(JSC::HasOwnPropertyCache::clear): Deleted.
(JSC::HasOwnPropertyCache::clearBuffer): Deleted.
(JSC::VM::ensureHasOwnPropertyCache): Deleted.
* Source/JavaScriptCore/runtime/LazyPropertyInlines.h:
(JSC::ElementType>::initLater):
* Source/JavaScriptCore/runtime/VM.cpp:
(JSC::VM::VM):
(JSC::VM::~VM):
(JSC::VM::invalidateStructureChainIntegrity):
(JSC::VM::ensureWatchdog): Deleted.
(JSC::VM::ensureHeapProfiler): Deleted.
(JSC::VM::ensureShadowChicken): Deleted.
(JSC::VM::ensureMegamorphicCacheSlow): Deleted.
* Source/JavaScriptCore/runtime/VM.h:
(JSC::VM::watchdog):
(JSC::VM::ensureWatchdog):
(JSC::VM::heapProfiler):
(JSC::VM::ensureHeapProfiler):
(JSC::VM::hasOwnPropertyCache):
(JSC::VM::ensureHasOwnPropertyCache):
(JSC::VM::megamorphicCache):
(JSC::VM::ensureMegamorphicCache):
(JSC::VM::shadowChicken):
(JSC::VM::ensureShadowChicken):
(JSC::VM::heapProfiler const): Deleted.
* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/CMakeLists.txt:
* Source/WTF/wtf/LazyRef.h: Added.
(WTF::LazyRef::~LazyRef):
(WTF::LazyRef::isInitialized const):
(WTF::LazyRef::get const):
(WTF::LazyRef::get):
(WTF::LazyRef::getIfExists const):
(WTF::LazyRef::getIfExists):
(WTF::LazyRef::initLater):
(WTF::LazyRef::set):
(WTF::LazyRef::callFunc):
* Source/WTF/wtf/LazyUniqueRef.h: Added.
(WTF::LazyUniqueRef::~LazyUniqueRef):
(WTF::LazyUniqueRef::isInitialized const):
(WTF::LazyUniqueRef::get const):
(WTF::LazyUniqueRef::get):
(WTF::LazyUniqueRef::getIfExists const):
(WTF::LazyUniqueRef::getIfExists):
(WTF::LazyUniqueRef::initLater):
(WTF::LazyUniqueRef::set):
(WTF::LazyUniqueRef::callFunc):

Canonical link: https://commits.webkit.org/273287@main




More information about the webkit-changes mailing list