2020-12-28 17:15:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
2020-12-28 17:15:22 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-28 17:15:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-02-25 17:42:45 +00:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2021-09-16 19:15:16 +00:00
|
|
|
#include <AK/StringView.h>
|
2020-12-28 17:15:22 +00:00
|
|
|
#include <AK/Types.h>
|
2023-09-12 11:03:56 +00:00
|
|
|
#include <LibJS/Position.h>
|
LibJS: Reduce AST memory usage by shrink-wrapping source range info
Before this change, each AST node had a 64-byte SourceRange member.
This SourceRange had the following layout:
filename: StringView (16 bytes)
start: Position (24 bytes)
end: Position (24 bytes)
The Position structs have { line, column, offset }, all members size_t.
To reduce memory consumption, AST nodes now only store the following:
source_code: NonnullRefPtr<SourceCode> (8 bytes)
start_offset: u32 (4 bytes)
end_offset: u32 (4 bytes)
SourceCode is a new ref-counted data structure that keeps the filename
and original parsed source code in a single location, and all AST nodes
have a pointer to it.
The start_offset and end_offset can be turned into (line, column) when
necessary by calling SourceCode::range_from_offsets(). This will walk
the source code string and compute line/column numbers on the fly, so
it's not necessarily fast, but it should be rare since this information
is primarily used for diagnostics and exception stack traces.
With this, ASTNode shrinks from 80 bytes to 32 bytes. This gives us a
~23% reduction in memory usage when loading twitter.com/awesomekling
(330 MiB before, 253 MiB after!) :^)
2022-11-21 16:37:38 +00:00
|
|
|
#include <LibJS/SourceCode.h>
|
2020-12-28 17:15:22 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
struct SourceRange {
|
2021-07-10 20:46:17 +00:00
|
|
|
[[nodiscard]] bool contains(Position const& position) const { return position.offset <= end.offset && position.offset >= start.offset; }
|
|
|
|
|
2023-02-19 21:07:52 +00:00
|
|
|
NonnullRefPtr<SourceCode const> code;
|
2020-12-28 17:15:22 +00:00
|
|
|
Position start;
|
|
|
|
Position end;
|
LibJS: Reduce AST memory usage by shrink-wrapping source range info
Before this change, each AST node had a 64-byte SourceRange member.
This SourceRange had the following layout:
filename: StringView (16 bytes)
start: Position (24 bytes)
end: Position (24 bytes)
The Position structs have { line, column, offset }, all members size_t.
To reduce memory consumption, AST nodes now only store the following:
source_code: NonnullRefPtr<SourceCode> (8 bytes)
start_offset: u32 (4 bytes)
end_offset: u32 (4 bytes)
SourceCode is a new ref-counted data structure that keeps the filename
and original parsed source code in a single location, and all AST nodes
have a pointer to it.
The start_offset and end_offset can be turned into (line, column) when
necessary by calling SourceCode::range_from_offsets(). This will walk
the source code string and compute line/column numbers on the fly, so
it's not necessarily fast, but it should be rare since this information
is primarily used for diagnostics and exception stack traces.
With this, ASTNode shrinks from 80 bytes to 32 bytes. This gives us a
~23% reduction in memory usage when loading twitter.com/awesomekling
(330 MiB before, 253 MiB after!) :^)
2022-11-21 16:37:38 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString filename() const;
|
2020-12-28 17:15:22 +00:00
|
|
|
};
|
|
|
|
|
2023-07-27 12:40:01 +00:00
|
|
|
struct UnrealizedSourceRange {
|
|
|
|
[[nodiscard]] SourceRange realize() const
|
|
|
|
{
|
|
|
|
VERIFY(source_code);
|
|
|
|
return source_code->range_from_offsets(start_offset, end_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceCode const> source_code;
|
|
|
|
u32 start_offset { 0 };
|
|
|
|
u32 end_offset { 0 };
|
|
|
|
};
|
|
|
|
|
2020-12-28 17:15:22 +00:00
|
|
|
}
|