Remove conditionals in inner loops in CJPX_Decoder::Decode().
Check `adjust` once and choose among 3 for-loops. This results in
another 3% speedup, as measured by running:
testing/tools/safetynet_measure.py --profiler perfstat bug_1698.pdf,
where bug_1698.pdf is the PDF attached to https://crbug.com/pdfium/1698.
Change-Id: I92e46909f346486eb3d941f48ffb8ed679f4ff18
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/82773
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcodec/jpx/cjpx_decoder.cpp b/core/fxcodec/jpx/cjpx_decoder.cpp
index 270dabd..c66985a 100644
--- a/core/fxcodec/jpx/cjpx_decoder.cpp
+++ b/core/fxcodec/jpx/cjpx_decoder.cpp
@@ -539,6 +539,8 @@
if (!comps.data)
continue;
+ // Perfomance-sensitive code below. Combining these 3 for-loops below will
+ // cause a slowdown.
const uint32_t src_offset = comps.sgnd ? 1 << (comps.prec - 1) : 0;
if (adjust < 0) {
for (uint32_t row = 0; row < height; ++row) {
@@ -549,19 +551,24 @@
*pPixel = static_cast<uint8_t>(src << -adjust);
}
}
+ } else if (adjust == 0) {
+ for (uint32_t row = 0; row < height; ++row) {
+ uint8_t* pScanline = pChannel + row * pitch;
+ for (uint32_t col = 0; col < width; ++col) {
+ uint8_t* pPixel = pScanline + col * m_Image->numcomps;
+ int src = comps.data[row * width + col] + src_offset;
+ *pPixel = static_cast<uint8_t>(src);
+ }
+ }
} else {
for (uint32_t row = 0; row < height; ++row) {
uint8_t* pScanline = pChannel + row * pitch;
for (uint32_t col = 0; col < width; ++col) {
uint8_t* pPixel = pScanline + col * m_Image->numcomps;
int src = comps.data[row * width + col] + src_offset;
- if (adjust == 0) {
- *pPixel = static_cast<uint8_t>(src);
- } else {
- int pixel = (src >> adjust) + ((src >> (adjust - 1)) % 2);
- pixel = pdfium::clamp(pixel, 0, 255);
- *pPixel = static_cast<uint8_t>(pixel);
- }
+ int pixel = (src >> adjust) + ((src >> (adjust - 1)) % 2);
+ pixel = pdfium::clamp(pixel, 0, 255);
+ *pPixel = static_cast<uint8_t>(pixel);
}
}
}