Skip to content

Commit

Permalink
Reduce cost of resetting row attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Jun 2, 2023
1 parent 1bec08e commit 4d6a2ba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/buffer/out/Row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ void ROW::Reset(const TextAttribute& attr)
{
_charsHeap.reset();
_chars = { _charsBuffer, _columnCount };
_attr = { _columnCount, attr };
// Constructing and then moving objects into place isn't free.
// Modifying the existing object is _much_ faster.
*_attr.runs().unsafe_shrink_to_size(1) = til::rle_pair{ attr, _columnCount };
_lineRendition = LineRendition::SingleWidth;
_wrapForced = false;
_doubleBytePadded = false;
Expand Down
5 changes: 5 additions & 0 deletions src/inc/til/rle.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
return _runs;
}

container& runs() noexcept
{
return _runs;
}

// Get the value at the position
const_reference at(size_type position) const
{
Expand Down
18 changes: 18 additions & 0 deletions src/inc/til/small_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,24 @@ namespace til
_capacity = capacity;
}

// This is a very unsafe shortcut to free the buffer and get a direct
// hold to the _buffer. The caller can then fill it with `size` items.
[[nodiscard]] T* unsafe_shrink_to_size(size_t size) noexcept
{
assert(size <= N);

if (_capacity != N)
{
_deallocate(_data);
}

_data = &_buffer[0];
_capacity = N;
_size = size;

return &_buffer[0];
}

void push_back(const T& value)
{
emplace_back(value);
Expand Down

0 comments on commit 4d6a2ba

Please sign in to comment.