From f0482b61bf3efbce61c52e81c792c072b46b548d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 24 Nov 2022 14:16:56 +0100 Subject: 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. --- Userland/Libraries/LibJS/SourceCode.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'Userland/Libraries/LibJS/SourceCode.h') 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 +#include #include 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> mutable m_line_break_offsets; }; } -- cgit v1.2.3