1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 07:55:08 +00:00
serenity/Userland/Libraries/LibWeb/RequestIdleCallback/IdleDeadline.cpp
Andreas Kling bfd354492e LibWeb: Put most LibWeb GC objects in type-specific heap blocks
With this change, we now have ~1200 CellAllocators across both LibJS and
LibWeb in a normal WebContent instance.

This gives us a minimum heap size of 4.7 MiB in the scenario where we
only have one cell allocated per type. Of course, in practice there will
be many more of each type, so the effective overhead is quite a bit
smaller than that in practice.

I left a few types unconverted to this mechanism because I got tired of
doing this. :^)
2023-11-19 22:00:48 +01:00

54 lines
1.7 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/RequestIdleCallback/IdleDeadline.h>
namespace Web::RequestIdleCallback {
JS_DEFINE_ALLOCATOR(IdleDeadline);
JS::NonnullGCPtr<IdleDeadline> IdleDeadline::create(JS::Realm& realm, bool did_timeout)
{
return realm.heap().allocate<IdleDeadline>(realm, realm, did_timeout);
}
IdleDeadline::IdleDeadline(JS::Realm& realm, bool did_timeout)
: PlatformObject(realm)
, m_did_timeout(did_timeout)
{
}
void IdleDeadline::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::IdleDeadlinePrototype>(realm, "IdleDeadline"));
}
IdleDeadline::~IdleDeadline() = default;
// https://w3c.github.io/requestidlecallback/#dom-idledeadline-timeremaining
double IdleDeadline::time_remaining() const
{
auto const& event_loop = HTML::main_thread_event_loop();
// 1. Let now be a DOMHighResTimeStamp representing current high resolution time in milliseconds.
auto now = HighResolutionTime::unsafe_shared_current_time();
// 2. Let deadline be the result of calling IdleDeadline's get deadline time algorithm.
auto deadline = event_loop.compute_deadline();
// 3. Let timeRemaining be deadline - now.
auto time_remaining = deadline - now;
// 4. If timeRemaining is negative, set it to 0.
if (time_remaining < 0)
time_remaining = 0;
// 5. Return timeRemaining.
// NOTE: coarsening to milliseconds
return ceil(time_remaining);
}
}