LibJS+WebContent: Cache the resolved system time zone identifier

Even though the underlying time zone is already cached by LibUnicode, JS
performs additional expensive lookups with that time zone. There's no
need to do those lookups again until the system time zone has changed.
This commit is contained in:
Timothy Flynn 2024-09-03 10:18:43 -04:00 committed by Andreas Kling
parent 25e5ccd376
commit 5ee92af1d9
Notes: github-actions[bot] 2024-09-03 17:27:07 +00:00
4 changed files with 18 additions and 1 deletions

View file

@ -8,6 +8,7 @@
#include <AK/Enumerate.h>
#include <LibCore/Environment.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibTest/JavaScriptTestRunner.h>
#include <LibUnicode/TimeZone.h>
@ -109,6 +110,7 @@ TESTJS_GLOBAL_FUNCTION(set_time_zone, setTimeZone)
return vm.throw_completion<JS::InternalError>(MUST(String::formatted("Could not set time zone: {}", result.error())));
}
JS::clear_system_time_zone_cache();
Unicode::clear_system_time_zone_cache();
tzset();

View file

@ -411,9 +411,15 @@ Unicode::TimeZoneOffset get_named_time_zone_offset_nanoseconds(StringView time_z
return offset.release_value();
}
static Optional<String> cached_system_time_zone_identifier;
// 21.4.1.24 SystemTimeZoneIdentifier ( ), https://tc39.es/ecma262/#sec-systemtimezoneidentifier
String system_time_zone_identifier()
{
// OPTIMIZATION: We cache the system time zone to avoid the expensive lookups below.
if (cached_system_time_zone_identifier.has_value())
return *cached_system_time_zone_identifier;
// 1. If the implementation only supports the UTC time zone, return "UTC".
// 2. Let systemTimeZoneString be the String representing the host environment's current time zone, either a primary
@ -429,7 +435,13 @@ String system_time_zone_identifier()
}
// 3. Return systemTimeZoneString.
return system_time_zone_string;
cached_system_time_zone_identifier = move(system_time_zone_string);
return *cached_system_time_zone_identifier;
}
void clear_system_time_zone_cache()
{
cached_system_time_zone_identifier.clear();
}
// 21.4.1.25 LocalTime ( t ), https://tc39.es/ecma262/#sec-localtime

View file

@ -77,6 +77,7 @@ Crypto::SignedBigInteger get_utc_epoch_nanoseconds(i32 year, u8 month, u8 day, u
Vector<Crypto::SignedBigInteger> get_named_time_zone_epoch_nanoseconds(StringView time_zone_identifier, i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond);
Unicode::TimeZoneOffset get_named_time_zone_offset_nanoseconds(StringView time_zone_identifier, Crypto::SignedBigInteger const& epoch_nanoseconds);
String system_time_zone_identifier();
void clear_system_time_zone_cache();
double local_time(double time);
double utc_time(double time);
double make_time(double hour, double min, double sec, double ms);

View file

@ -16,6 +16,7 @@
#include <LibGfx/SystemTheme.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/Date.h>
#include <LibUnicode/TimeZone.h>
#include <LibWeb/ARIA/RoleType.h>
#include <LibWeb/Bindings/MainThreadVM.h>
@ -1194,6 +1195,7 @@ void ConnectionFromClient::enable_inspector_prototype(u64)
void ConnectionFromClient::system_time_zone_changed()
{
JS::clear_system_time_zone_cache();
Unicode::clear_system_time_zone_cache();
}