From 7b77dfc9aafb423e6204dd5433699cfd787147b3 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 11 Apr 2018 17:55:00 +0000 Subject: Reserve space to reduce memory operations while encoding barcode In the test case from the bug, the majority of the time is being spent resizing Widestring internal buffers, since += is being called in a tight loop. Since the size of the input being mutated and stored is known, this CL reserves the space before hand to lower thrashing. This substantially improves runtime of this test case locally. BUG=chromium:802242 Change-Id: I5176dabc94634b4d6bc3e9425fe6469a5bf35a41 Reviewed-on: https://pdfium-review.googlesource.com/30190 Reviewed-by: Lei Zhang Commit-Queue: Ryan Harrison --- fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp index ffc057a5f6..6dd7ee5191 100644 --- a/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp +++ b/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.cpp @@ -61,8 +61,9 @@ WideString CBC_PDF417HighLevelEncoder::encodeHighLevel(WideString wideMsg, int32_t& e) { ByteString bytes; CBC_UtilCodingConvert::UnicodeToUTF8(wideMsg, bytes); - WideString msg; int32_t len = bytes.GetLength(); + WideString msg; + msg.Reserve(len); for (int32_t i = 0; i < len; i++) { wchar_t ch = (wchar_t)(bytes[i] & 0xff); if (ch == '?' && bytes[i] != '?') { @@ -72,8 +73,9 @@ WideString CBC_PDF417HighLevelEncoder::encodeHighLevel(WideString wideMsg, msg += ch; } std::vector byteArr(bytes.begin(), bytes.end()); - WideString sb; len = msg.GetLength(); + WideString sb; + sb.Reserve(len); int32_t p = 0; int32_t textSubMode = SUBMODE_ALPHA; if (compaction == TEXT) { -- cgit v1.2.3