Skip to content

Commit

Permalink
Avoid reallocating Bytes when appending from view
Browse files Browse the repository at this point in the history
When copying a full view into a Bytes instance, avoid potential
reallocations and memcpy() by pre-allocating enough capacity in
the underlying string.
  • Loading branch information
awelzel authored and bbannier committed Dec 4, 2023
1 parent 5315a54 commit e696c78
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions hilti/runtime/src/types/bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ Result<Bytes> 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<const char*>(block->start), block->size);
}
Expand Down

0 comments on commit e696c78

Please sign in to comment.