From 56a4af8d03a083b2b9e49d7455ebe869a8def00a Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 8 Jan 2024 20:22:22 -0500 Subject: [PATCH] LibPDF: Don't reallocate Vectors in ICCBasedColorSpace all the time Microoptimization; according to ministat a bit faster: ``` N Min Max Median Avg Stddev x 50 1.0179932 1.0561159 1.0315337 1.0333617 0.0094757426 + 50 1.000875 1.0427601 1.0208509 1.0201902 0.01066116 Difference at 95.0% confidence -0.0131715 +/- 0.00400208 -1.27463% +/- 0.387287% (Student's t, pooled s = 0.0100859) ``` --- Userland/Libraries/LibPDF/ColorSpace.cpp | 14 +++++++------- Userland/Libraries/LibPDF/ColorSpace.h | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibPDF/ColorSpace.cpp b/Userland/Libraries/LibPDF/ColorSpace.cpp index b4c1e44759..fb71d60c3e 100644 --- a/Userland/Libraries/LibPDF/ColorSpace.cpp +++ b/Userland/Libraries/LibPDF/ColorSpace.cpp @@ -500,7 +500,7 @@ ICCBasedColorSpace::ICCBasedColorSpace(NonnullRefPtr profile) PDFErrorOr ICCBasedColorSpace::style(ReadonlySpan arguments) const { - Vector components; + m_components.resize(arguments.size()); for (size_t i = 0; i < arguments.size(); ++i) { auto const& arg = arguments[i]; VERIFY(arg.has_number()); @@ -515,17 +515,17 @@ PDFErrorOr ICCBasedColorSpace::style(ReadonlySpan arguments number = (number + 128.0f) / 255.0f; } - components.append(number); + m_components[i] = number; } if (m_map.has_value()) - return m_map->map(FloatVector3 { components[0], components[1], components[2] }); + return m_map->map(FloatVector3 { m_components[0], m_components[1], m_components[2] }); - Vector bytes; - for (auto component : components) - bytes.append(static_cast(component * 255.0f)); + m_bytes.resize(arguments.size()); + for (size_t i = 0; i < arguments.size(); ++i) + m_bytes[i] = static_cast(m_components[i] * 255.0f); - auto pcs = TRY(m_profile->to_pcs(bytes)); + auto pcs = TRY(m_profile->to_pcs(m_bytes)); Array output; TRY(sRGB()->from_pcs(m_profile, pcs, output.span())); diff --git a/Userland/Libraries/LibPDF/ColorSpace.h b/Userland/Libraries/LibPDF/ColorSpace.h index dfbca3dee1..fb592558d1 100644 --- a/Userland/Libraries/LibPDF/ColorSpace.h +++ b/Userland/Libraries/LibPDF/ColorSpace.h @@ -195,6 +195,8 @@ private: static RefPtr s_srgb_profile; NonnullRefPtr m_profile; + mutable Vector m_components; + mutable Vector m_bytes; Optional m_map; };