From e696c78b09bb545882b63e0daab62441b2897ac6 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Sun, 3 Dec 2023 12:27:06 +0100 Subject: [PATCH] Avoid reallocating Bytes when appending from view When copying a full view into a Bytes instance, avoid potential reallocations and memcpy() by pre-allocating enough capacity in the underlying string. --- hilti/runtime/src/types/bytes.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/hilti/runtime/src/types/bytes.cc b/hilti/runtime/src/types/bytes.cc index 75ea33a8df..10ad320566 100644 --- a/hilti/runtime/src/types/bytes.cc +++ b/hilti/runtime/src/types/bytes.cc @@ -215,6 +215,7 @@ Result Bytes::match(const RegExp& re, unsigned int group) const { } void Bytes::append(const stream::View& view) { + reserve(size() + view.size()); for ( auto block = view.firstBlock(); block; block = view.nextBlock(block) ) Base::append(reinterpret_cast(block->start), block->size); }