فهرست منبع

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)
```
Nico Weber 1 سال پیش
والد
کامیت
56a4af8d03
2فایلهای تغییر یافته به همراه9 افزوده شده و 7 حذف شده
  1. 7 7
      Userland/Libraries/LibPDF/ColorSpace.cpp
  2. 2 0
      Userland/Libraries/LibPDF/ColorSpace.h

+ 7 - 7
Userland/Libraries/LibPDF/ColorSpace.cpp

@@ -500,7 +500,7 @@ ICCBasedColorSpace::ICCBasedColorSpace(NonnullRefPtr<Gfx::ICC::Profile> profile)
 
 PDFErrorOr<ColorOrStyle> ICCBasedColorSpace::style(ReadonlySpan<Value> arguments) const
 {
-    Vector<float, 4> 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<ColorOrStyle> ICCBasedColorSpace::style(ReadonlySpan<Value> 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<u8, 4> bytes;
-    for (auto component : components)
-        bytes.append(static_cast<u8>(component * 255.0f));
+    m_bytes.resize(arguments.size());
+    for (size_t i = 0; i < arguments.size(); ++i)
+        m_bytes[i] = static_cast<u8>(m_components[i] * 255.0f);
 
-    auto pcs = TRY(m_profile->to_pcs(bytes));
+    auto pcs = TRY(m_profile->to_pcs(m_bytes));
     Array<u8, 3> output;
     TRY(sRGB()->from_pcs(m_profile, pcs, output.span()));
 

+ 2 - 0
Userland/Libraries/LibPDF/ColorSpace.h

@@ -195,6 +195,8 @@ private:
 
     static RefPtr<Gfx::ICC::Profile> s_srgb_profile;
     NonnullRefPtr<Gfx::ICC::Profile> m_profile;
+    mutable Vector<float, 4> m_components;
+    mutable Vector<u8, 4> m_bytes;
     Optional<Gfx::ICC::MatrixMatrixConversion> m_map;
 };