From 3365a92ead9c29d3258aba20559c08462fd8ccb2 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 8 Jan 2024 18:29:47 -0500 Subject: [PATCH] LibGfx/ICC: Cache inverted matrix on profile Reduces time spent rendering page 3 of 0000849.pdf from 1.85s to 1.45s on my machine. As determined by running time Build/lagom/bin/pdf --page 3 --render out.png \ ~/Downloads/0000/0000849.pdf a few times and eyeballing the min time. Also reduces the time to run Meta/test_pdf.py on 0000.zip (without 0000849.pdf) from 1m7s to 58s. --- Userland/Libraries/LibGfx/ICC/Profile.cpp | 13 ++++++++----- Userland/Libraries/LibGfx/ICC/Profile.h | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibGfx/ICC/Profile.cpp b/Userland/Libraries/LibGfx/ICC/Profile.cpp index 9afd25c689..2e3bb4fd3f 100644 --- a/Userland/Libraries/LibGfx/ICC/Profile.cpp +++ b/Userland/Libraries/LibGfx/ICC/Profile.cpp @@ -1505,7 +1505,7 @@ ErrorOr Profile::from_pcs(Profile const& source_profile, FloatVector3 pcs, // inverted." // Convert from XYZ to linear rgb. - // FIXME: Inverting matrix and curve on every call to this function is very inefficient. + // FIXME: Inverting curves on every call to this function is very inefficient. FloatVector3 linear_rgb = TRY(xyz_to_rgb_matrix()) * pcs; auto evaluate_curve_inverse = [this](TagSignature curve_tag, float f) { @@ -1619,10 +1619,13 @@ Optional Profile::tag_string_data(TagSignature signature) const ErrorOr Profile::xyz_to_rgb_matrix() const { - FloatMatrix3x3 forward_matrix = rgb_to_xyz_matrix(); - if (!forward_matrix.is_invertible()) - return Error::from_string_literal("ICC::Profile::from_pcs: matrix not invertible"); - return forward_matrix.inverse(); + if (!m_cached_xyz_to_rgb_matrix.has_value()) { + FloatMatrix3x3 forward_matrix = rgb_to_xyz_matrix(); + if (!forward_matrix.is_invertible()) + return Error::from_string_literal("ICC::Profile::from_pcs: matrix not invertible"); + m_cached_xyz_to_rgb_matrix = forward_matrix.inverse(); + } + return m_cached_xyz_to_rgb_matrix.value(); } FloatMatrix3x3 Profile::rgb_to_xyz_matrix() const diff --git a/Userland/Libraries/LibGfx/ICC/Profile.h b/Userland/Libraries/LibGfx/ICC/Profile.h index 0c3dc0396d..718fd37d4b 100644 --- a/Userland/Libraries/LibGfx/ICC/Profile.h +++ b/Userland/Libraries/LibGfx/ICC/Profile.h @@ -252,6 +252,8 @@ private: // Only valid for RGB matrix-based profiles. ErrorOr xyz_to_rgb_matrix() const; FloatMatrix3x3 rgb_to_xyz_matrix() const; + + mutable Optional m_cached_xyz_to_rgb_matrix; }; }