From 1e95aeadd33fc4a13f5579501f81b6d5edc7cc05 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Mon, 23 Apr 2018 21:09:33 +0000 Subject: Pre-allocate more string buffers in barcode code Another couple of examples where the slow down in the barcode code can be fixed by reserving and thus pre-allocating the buffer that backs the Widestring. Doing += in a tight loop caused reallocation thrashing. BUG=chromium:834630 Change-Id: I48a802225351bcaf992c324732fddf81639b4898 Reviewed-on: https://pdfium-review.googlesource.com/31230 Commit-Queue: Ryan Harrison Reviewed-by: Henrique Nakashima --- fxbarcode/datamatrix/BC_Base256Encoder.cpp | 1 + fxbarcode/datamatrix/BC_EncoderContext.cpp | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/fxbarcode/datamatrix/BC_Base256Encoder.cpp b/fxbarcode/datamatrix/BC_Base256Encoder.cpp index b7dd69c0f7..1010cfe32c 100644 --- a/fxbarcode/datamatrix/BC_Base256Encoder.cpp +++ b/fxbarcode/datamatrix/BC_Base256Encoder.cpp @@ -35,6 +35,7 @@ int32_t CBC_Base256Encoder::getEncodingMode() { } void CBC_Base256Encoder::Encode(CBC_EncoderContext& context, int32_t& e) { WideString buffer; + buffer.Reserve(context.getRemainingCharacters() + 1); buffer += L'\0'; while (context.hasMoreCharacters()) { wchar_t c = context.getCurrentChar(); diff --git a/fxbarcode/datamatrix/BC_EncoderContext.cpp b/fxbarcode/datamatrix/BC_EncoderContext.cpp index fe0a497d58..d9823bffe2 100644 --- a/fxbarcode/datamatrix/BC_EncoderContext.cpp +++ b/fxbarcode/datamatrix/BC_EncoderContext.cpp @@ -33,8 +33,9 @@ CBC_EncoderContext::CBC_EncoderContext(const WideString& msg, int32_t& e) { ByteString dststr; CBC_UtilCodingConvert::UnicodeToUTF8(msg, dststr); - WideString sb; size_t c = dststr.GetLength(); + WideString sb; + sb.Reserve(c); for (size_t i = 0; i < c; i++) { wchar_t ch = static_cast(dststr[i] & 0xff); if (ch == '?' && dststr[i] != '?') { -- cgit v1.2.3