diff options
author | Andreas Kling <kling@serenityos.org> | 2022-11-24 14:16:56 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-11-24 16:06:20 +0000 |
commit | f0482b61bf3efbce61c52e81c792c072b46b548d (patch) | |
tree | d2ecd5933b784da31ae80d05285a1661bfe4c8f9 /Userland/Libraries/LibJS/SourceCode.h | |
parent | e7ba03ddd109dda3f0d1cdac4a8053a4753bfbf0 (diff) | |
download | serenity-f0482b61bf3efbce61c52e81c792c072b46b548d.zip |
LibJS: Make Error stack trace generation faster with a line break cache
Generating Error objects got a lot slower after the introduction of
SourceCode in b0b022507b2f73be2f4cef17deb8ece91dae6822.
This was noticeable with `test-js` which generates a lot of errors,
so walking the source code over and over to compute (line, column)
was eating a ton of time.
This patch makes repeated lookups a lot faster by building a cache
of line break offsets in the source code. The cache is built on first
offset lookup, so we only pay for this in code that actually throws.
On my machine, this takes `test-js` runtime from 6.7 sec to 4.3 sec.
Diffstat (limited to 'Userland/Libraries/LibJS/SourceCode.h')
-rw-r--r-- | Userland/Libraries/LibJS/SourceCode.h | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/SourceCode.h b/Userland/Libraries/LibJS/SourceCode.h index 3980efed69..1550c50c89 100644 --- a/Userland/Libraries/LibJS/SourceCode.h +++ b/Userland/Libraries/LibJS/SourceCode.h @@ -7,6 +7,7 @@ #pragma once #include <AK/String.h> +#include <AK/Vector.h> #include <LibJS/Forward.h> namespace JS { @@ -23,8 +24,12 @@ public: private: SourceCode(String filename, String code); + void compute_line_break_offsets() const; + String m_filename; String m_code; + + Optional<Vector<size_t>> mutable m_line_break_offsets; }; } |