Skip to content

Commit

Permalink
Remove trimming inside indivdual chunks.
Browse files Browse the repository at this point in the history
Trimming `Chunk`s (always from the left) causes a lot of internal work
with only limited benefit since we manage visbility with `stream::View`s
on top of `Chunk`s anyway.

This patch removes trimming inside `Chunk`s so now any trimming only
removes `Chunk`s from `Chain`s, but does not internally change
individual `Chunk`s anymore. This might lead to slighly increased memory
use, but callers usually have that data in memory anyway.

Closes #1571.
  • Loading branch information
bbannier committed Oct 12, 2023
1 parent 4970368 commit 409b52c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 22 deletions.
2 changes: 0 additions & 2 deletions hilti/runtime/include/types/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ class Chunk {
// only from chain so that it can track any changes.
friend class Chain;

void trim(const Offset& o);

// Update offset for current chunk and all others linked from it.
void setOffset(Offset o) {
auto c = this;
Expand Down
21 changes: 1 addition & 20 deletions hilti/runtime/src/types/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,6 @@ Chunk::Chunk(const Offset& offset, const std::string& s) : _offset(offset) {
}
}

void Chunk::trim(const Offset& o) {
assert(o >= _offset && o < _offset + size());
if ( auto a = std::get_if<Array>(&_data) ) {
auto begin = a->second.data() + (o - _offset).Ref();
auto end = a->second.data() + a->first.Ref();
a->first = (end - begin);
memmove(a->second.data(), begin, a->first.Ref());
}
else if ( std::holds_alternative<Vector>(_data) ) {
auto& v = std::get<Vector>(_data);
v.erase(v.begin(), v.begin() + static_cast<Vector::difference_type>((o - _offset).Ref()));
}
// Nothing to do for gap chunks.

_offset = o;
}

void Chain::append(std::unique_ptr<Chunk> chunk) {
_ensureValid();
_ensureMutable();
Expand Down Expand Up @@ -117,8 +100,7 @@ void Chain::trim(const Offset& offset) {
}

else if ( _head->inRange(offset) ) {
_head->trim(offset);
assert(_head->offset() == offset);
// Perform not trimming inside individual chunks.
break;
}

Expand All @@ -128,7 +110,6 @@ void Chain::trim(const Offset& offset) {
}

_head_offset = offset;
assert(! _head || _head->offset() == offset);
}

ChainPtr Chain::deepCopy() const {
Expand Down

0 comments on commit 409b52c

Please sign in to comment.