Commit graph

3988 commits

Author SHA1 Message Date
Jelle Raaijmakers
2c78fd5b89 AK: Remove unused Checked<T> code
We could never hit the #else branches for some of these methods, because
we already relied on having __builtin_*_overflow() readily available in
earlier methods.

multiplication_would_overflow() with three arguments was only used in a
test, so let's get rid of that as well.
2025-10-22 00:26:23 +02:00
Ali Mohammad Pur
553a7a9278 AK: Remove unused and unintuitive Trait for char*
We don't use char* in containers, and the expectation would be a pointer
compare instead of a string compare; so remove this specialisation
entirely.
2025-10-21 18:20:48 +02:00
Luke Wilde
afd170d16c AK: Add the ability to reinterpret a Span to a given type
This allows you to reinterpret a Span to any given type, maintaining
the original data and working out the new size for you.

The target type must evenly fit into the Span's original type, ensuring
bytes are not dropped.
2025-10-20 16:26:12 +02:00
Andreas Kling
25a5ed94d6 LibGC: Add GC::Weak<T> as an alternative to AK::WeakPtr<T>
This is a weak pointer that integrates with the garbage collector.
It has a number of differences compared to AK::WeakPtr, including:

- The "control block" is allocated from a well-packed WeakBlock owned by
  the GC heap, not just a generic malloc allocation.

- Pointers to dead cells are nulled out by the garbage collector
  immediately before running destructors.

- It works on any GC::Cell derived type, meaning you don't have to
  inherit from AK::Weakable for the ability to be weakly referenced.

- The Weak always points to a control block, even when "null" (it then
  points to a null WeakImpl), which means one less null check when
  chasing pointers.
2025-10-17 17:22:16 +02:00
R-Goc
ba5ef052e4 AK: Fix libbacktrace fallback
Introducing cpptrace as the primary backtrace library broke the
backtrace fallback during the code move. This commit properly links AK
to libbacktrace.

It also fixes the function signatures for the fallback backtrace
handlers.
2025-10-10 12:46:20 +02:00
R-Goc
62e7d1b5c8 AK: Remove FreeLibrary call in assertion handler
The call to FreeLibrary was incorrect as GetModuleHandle does not
increment the reference count of the DLL.
2025-10-08 07:07:57 +02:00
R-Goc
3c7bad32cd AK: Clean up backtraces
This commit replaces the default backtrace logic with cpptrace, for
nicer, colored backtraces. Cpptrace runs on all of our supported
platforms excpet android. As such backtrace.h is left in place.

All the backtrace functions are made noinline to have a consistent
number of frames. A maximum depth parameter is added to dump_backtrace
with a default of 100. This should be enough, and can be easily
changed, and allows for limiting the maximum depth.

Setting the LADYBIRD_BACKTRACE_SNIPPETS environment variable enables
surrouding code snippets in the backtrace. Specifically 2 lines above
and below. This number can be changed by calling snippet_context on the
formatter. For the whole list of options of what can be done with
formatting see the cpptrace repository.

On Windows we skipped frames when verification fails and when
dump_backtrace was added the logic was wrong and would have skipped
frames we care about.

This commit also implements skipping frames on Linux.
The only time where this does not skip all frames is when the call to
backtrace gets intercepted. Then we will end up skipping one frame less
than needed.

To keep delayload on Windows a patch and overlay port is used. When
upstream accepts these changes and vcpkg bumps the version the patch
could be removed to have just the cmake define.
2025-10-08 07:07:57 +02:00
Andreas Kling
ce16bdd07d Revert "AK: Use HashMap::ensure() for FlyString and Utf16FlyString lookups"
This reverts commit 9425ee6d2b.

Appears to have introduced flakiness on WPT.
2025-10-06 10:07:40 +02:00
Andreas Kling
9425ee6d2b AK: Use HashMap::ensure() for FlyString and Utf16FlyString lookups
This consolidates sometimes-two hash lookups into always-one, which is
less work in the case where a new fly-string is introduced.
2025-10-05 21:44:06 +02:00
Andreas Kling
ca772caee6 AK: Add HashTable::ensure(hash, predicate, init_callback)
...and use it to make HashMap::ensure() do a single hash lookup instead
of three.

We achieve this by factoring out everything but the bucket construction
logic from HashTable::write_value() into a lookup_for_writing() helper
so we can use it from more places.
2025-10-05 21:44:06 +02:00
Andreas Kling
24934ba479 LibJS: Make single-character ASCII string cache strings be Utf16String
Just another little step towards all strings being Utf16String.
2025-10-05 16:39:14 +02:00
Andreas Kling
72ea158f16 AK: Initialize StringBuilder buffer in place
Before this change, we'd construct a ByteBuffer for the internal buffer,
and then move-construct StringBuilder::m_buffer from it.

Due to ByteBuffer's inline capacity, this meant we had to memmove the
inline buffer an extra time for every StringBuilder created.
2025-10-05 11:24:46 +02:00
Andreas Kling
1ec72de0a6 AK: Remove unnecessary StringBuilder::create() factory 2025-10-05 11:24:46 +02:00
Andreas Kling
bb93107a7d AK: Avoid redundant ASCII validation in StringBuilder::append(Utf16View)
If the UTF-16 data comes from ASCII-only storage, we can skip the ASCII
validation and save ourselves the effort.
2025-10-05 11:24:46 +02:00
Andreas Kling
b50ff02da4 AK: Skip ASCII validation in {Utf16String,String}::number() 2025-10-05 11:24:46 +02:00
Andreas Kling
1c04b6da3b AK: Make Utf16String::number(Integral) fast like String::number()
Move the fast String::number() implementation to a shareable place
so both string types can make use of it.
2025-10-05 11:24:46 +02:00
Ali Mohammad Pur
15bb9fee71 AK: Use OOB pointer for "empty" last-fast-access Vector last ptr
This is a _significant_ perf improvement as we no longer have to think
about tracking state transitions from empty <-> nonempty.
(corresponds to a ~20% perf improvement in LibWasm)
2025-10-01 23:47:29 +02:00
Ali Mohammad Pur
80e5356853 AK: Add Vector::remove_all(container)/remove_all(it, end)
Instead of repeatedly removing elements off the vector, this allows for
specifying all the removed indices at once, and does not perform any
extra reallocations or unnecessary moves.
2025-10-01 23:47:29 +02:00
Tomasz Strejczek
5cde267979 AK: Implement Formatter<UnixDateTime>
Implement StringBuilder's Formatter<UnixDateTime>. Add necessary tests.
2025-09-30 12:39:01 +02:00
Tomasz Strejczek
ea32e39d68 AK: Add UnixDateTime::parse() method
Copy parse() method from LibCore::DateTime::parse(). Augment the method
to handle parsing from GMT time. Fix incorrect handling of year in '%D'
format specifier. Remove all format specifiers related to time zones.
Copy relevant tests and add additional ones.
2025-09-30 12:39:01 +02:00
me-it-is
b76f1fb011 AK: Fix is_within_range when converting from float
Within range now uses the max capacity of a type rather than its size.
This fixes some subtests in
https://wpt.fyi/results/wasm/core/conversions.wast.js.html?product=ladybird
2025-09-24 10:40:24 +01:00
Zaggy1024
744568c912 AK: Destroy non-trivial types by ref in HashTable::clear_with_capacity
Buckets being iterated by pointer instead of reference was causing a
compilation error when calling clear_with_capacity() on a HashTable
containing a non-trivially-destructible type.
2025-09-22 17:28:00 -05:00
Andreas Kling
bc368110ab AK: Make is<T const> use fast_is<T>
We were missing many opportunities to use the fast_is optimization due
to requiring T be non-const.
2025-09-22 15:00:50 +02:00
Timothy Flynn
2df4835025 LibWeb: Place HTTP cache logging behind a debug flag
It's quite verbose to have logging on by default here.
2025-09-19 13:52:07 +02:00
Andreas Kling
59a28febc9 AK: Store hash with HashTable entry to avoid expensive equality checks
When T in HashTable<T> has a potentially slow equality check, it can be
very profitable to check for a matching hash before full equality.

This patch adds may_have_slow_equality_check() to AK::Traits and
defaults it to true. For trivial types (pointers, integers, etc) we
default it to false. This means we skip the hash check when the equality
check would be a single-CPU-word compare anyway.

This synergizes really well with things like HashMap<String, V> where
collisions previously meant we may have to churn through multiple O(n)
equality checks.
2025-09-18 22:37:18 +02:00
Zaggy1024
5207de9a2d AK: Add a function to Duration to convert it to double seconds 2025-09-17 16:45:45 -05:00
Jelle Raaijmakers
0fe9255991 AK: Add Optional<T>::ensure()
We often use Optional<T> in a cache pattern such as:

  if (!m_cache.has_value())
      m_cache = slow_thing();
  return m_cache.value();

The new ::ensure() makes it a bit simpler:

  return m_cache.ensure([&] { return slow_thing(); });
2025-09-17 12:01:18 -04:00
Ali Mohammad Pur
4462348916 Everywhere: Slap some [[clang::lifetimebound]] where appropriate
This first pass only applies to the following two cases:
- Public functions returning a view type into an object they own
- Public ctors storing a view type

This catches a grand total of one (1) issue, which is fixed in
the previous commit.
2025-09-01 11:11:38 +02:00
Timothy Flynn
086877a280 AK: Use simdutf to validate UTF-16 strings as ASCII 2025-08-27 13:25:03 +02:00
Viktor Szépe
1c01e183b7 Everywhere: Fix even more typos 2025-08-27 08:48:01 +02:00
Ali Mohammad Pur
3e62cae2ad AK: Add Variant::unsafe_get()
This performs no validation and no verifications on release.
only useful when you've already verified the type by external means.
2025-08-26 15:20:33 +02:00
Jelle Raaijmakers
305dfe3f40 AK: Don't remove CV qualifiers from types in Optional<T>
CV qualifiers are already ignored by __is_base_of(), which is what
IsBaseOf<T,U> uses.
2025-08-25 11:02:42 +02:00
ayeteadoe
97e8a922ad ImageDecoder: Enable in Windows CI 2025-08-23 16:04:36 -06:00
ayeteadoe
ee3c033de2 LibWebView: Enable in Windows CI 2025-08-23 16:04:36 -06:00
Tim Ledbetter
d31aec25e8 AK: Ignore possible const reference when comparing type equality in is 2025-08-22 20:26:09 +02:00
Tim Ledbetter
4e57a2aedf LibWeb: Ensure static cast is used when possible in as_if 2025-08-22 20:26:09 +02:00
Timothy Flynn
b1fe816336 AK: Provide a length_in_code_units API for UTF-8 strings
This is just to afford API symmetry with UTF-16 strings to help in
templated methods.
2025-08-22 14:06:46 +02:00
Timothy Flynn
1869399fd1 AK: Specialize Optional for Utf16String and Utf16FlyString
We added this for String some time ago, so let's give Utf16String the
same optimization. Note that Utf16String was already handling its data
member potentially being null as of 5af99f4dd0.
2025-08-19 06:24:09 -04:00
Glenn Skrzypczak
d25d62e74c AK/Time+LibWeb/HTML: Fix ISO8601 week conversions
This reimplements conversions between unix date times and ISO8601
weeks. The new algorithms also do not use loops, so they should be
faster.
2025-08-14 11:05:28 -04:00
Timothy Flynn
8472e469f4 AK+LibJS+LibWeb: Recognize that our UTF-16 string is actually WTF-16
For the web, we allow a wobbly UTF-16 encoding (i.e. lonely surrogates
are permitted). Only in a few exceptional cases do we strictly require
valid UTF-16. As such, our `validate(AllowLonelySurrogates::Yes)` calls
will always succeed. It's a wasted effort to ever make such a check.

This patch eliminates such invocations. The validation methods will now
only check for strict UTF-16, and are only invoked when needed.
2025-08-13 09:56:13 -04:00
Timothy Flynn
36c7302178 AK: Optimize the UTF-16 StringBuilder for ASCII storage
When we build a UTF-16 string, we currently always switch to the UTF-16
storage mode inside StringBuilder. Then when it comes time to create the
string, we switch the storage to ASCII if possible (by shifting the
underlying bytes up).

Instead, let's start out with ASCII storage and then switch to UTF-16
storage once we see a non-ASCII code point. For most strings, this will
avoid allocating 2x the memory, and avoids many ASCII validation calls.
2025-08-13 09:56:13 -04:00
Timothy Flynn
99d7e08dff AK: Templatize GenericLexer for UTF-16 strings
We now define GenericLexer as a template to allow using it with UTF-16
strings. To keep existing users happy, the template is defined in the
Detail namespace. Then AK::GenericLexer is an alias for a char-based
view, and AK::Utf16GenericLexer is an alias for a char16-based view.
2025-08-13 09:56:13 -04:00
Timothy Flynn
28d9d3a2c7 AK+Libraries: Reduce API surface of GenericLexer a bit
* Remove completely unused methods.
* Deduplicate methods that were overloaded with both StringView and
  char const* parameters.

A future commit will templatize GenericLexer by char type. This patch
serves to make that a tiny bit easier.
2025-08-13 09:56:13 -04:00
Callum Law
861bcbd9ad AK: Format floats with precision in scientific notation where applicable 2025-08-11 17:10:04 +01:00
Callum Law
1474da31c7 AK: Reduce duplication between put_f32_or_f64 and put_f64_with_precision
We were handling the special cases of NaN and Infinity in basically the
same way across both functions - we can reduce code duplication by
moving this to before we branch.

This is also required as we will be moving the logic to encode in
scientific notation above the branch in a later commit and the
`convert_floating_point_to_decimal_exponential_form` method doesn't work
with non-finite values.
2025-08-11 17:10:04 +01:00
Timothy Flynn
f03c432b52 AK: Use simdutf for searching strings for a single code unit
In the following synthetic benchmark, the simdutf version is 4x faster:

    BENCHMARK_CASE(find)
    {
        auto string = u"😀Foo😀Bar"sv;

        for (size_t i = 0; i < 100'000'000; ++i)
            (void)string.find_code_unit_offset('a');
    }
2025-08-11 16:55:37 +02:00
Idan Horowitz
93692242b9 AK: Implement take_all_matching(predicate) API in HashMap 2025-08-08 13:09:58 -04:00
Idan Horowitz
5097e72174 AK: Implement take_all_matching(predicate) API in HashTable 2025-08-08 13:09:58 -04:00
Ali Mohammad Pur
e47fceed38 AK: Optionally keep track of the last slot in Vector
last() and take_last() are extremely common ops when the vector is used
like a stack.
2025-08-08 12:54:06 +02:00
Ali Mohammad Pur
2cd4b4e28d AK: Skip vcalls to Stream::read_value and read_until_filled in LEB128
...for the first byte.
This function only really needs to read a single byte at that point, so
read_until_filled() is useless and read_value<u8> is functionally
equivalent to just a read.

This showed up hot in a wasm parse benchmark.
2025-08-08 12:54:06 +02:00