diff options
Diffstat (limited to 'src/zencore/compositebuffer.cpp')
| -rw-r--r-- | src/zencore/compositebuffer.cpp | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/src/zencore/compositebuffer.cpp b/src/zencore/compositebuffer.cpp index ed2b16384..1dee8477f 100644 --- a/src/zencore/compositebuffer.cpp +++ b/src/zencore/compositebuffer.cpp @@ -179,12 +179,11 @@ CompositeBuffer::GetIterator(uint64_t Offset) const MemoryView CompositeBuffer::ViewOrCopyRange(Iterator& It, uint64_t Size, UniqueBuffer& CopyBuffer) const { - // We use a sub range IoBuffer when we want to copy data from a segment. - // This means we will only materialize that range of the segment when doing - // GetView() rather than the full segment. - // A hot path for this code is when we call CompressedBuffer::FromCompressed which - // is only interested in reading the header (first 64 bytes or so) and then throws - // away the materialized data. + // A hot path for this code is CompressedBuffer::FromCompressed, which only reads the header + // (first 64 bytes or so). For plain memory segments we take a direct view (no allocation); + // for extended (file-backed) segments we materialize only the requested slice via a + // sub-range IoBuffer whose lifetime must extend across the CopyFrom below — otherwise its + // view would dangle into freed memory the moment the IoBuffer goes out of scope. if (CopyBuffer.GetSize() < Size) { CopyBuffer = UniqueBuffer::Alloc(Size); @@ -198,9 +197,20 @@ CompositeBuffer::ViewOrCopyRange(Iterator& It, uint64_t Size, UniqueBuffer& Copy const SharedBuffer& Segment = m_Segments[It.SegmentIndex]; size_t SegmentSize = Segment.GetSize(); size_t CopySize = zen::Min(SegmentSize - It.OffsetInSegment, SizeLeft); - IoBuffer SubSegment(Segment.AsIoBuffer(), It.OffsetInSegment, CopySize); - MemoryView ReadView = SubSegment.GetView(); - WriteView = WriteView.CopyFrom(ReadView); + + IoBuffer SubSegment; // lifetime holder for the extended-segment view + MemoryView ReadView; + if (Segment.IsExtended()) + { + SubSegment = IoBuffer(Segment.AsIoBuffer(), It.OffsetInSegment, CopySize); + ReadView = SubSegment.GetView(); + } + else + { + ReadView = Segment.GetView().Mid(It.OffsetInSegment, CopySize); + } + WriteView = WriteView.CopyFrom(ReadView); + It.OffsetInSegment += CopySize; ZEN_ASSERT_SLOW(It.OffsetInSegment <= SegmentSize); if (It.OffsetInSegment == SegmentSize) @@ -216,12 +226,7 @@ CompositeBuffer::ViewOrCopyRange(Iterator& It, uint64_t Size, UniqueBuffer& Copy void CompositeBuffer::CopyTo(MutableMemoryView WriteView, Iterator& It) const { - // We use a sub range IoBuffer when we want to copy data from a segment. - // This means we will only materialize that range of the segment when doing - // GetView() rather than the full segment. - // A hot path for this code is when we call CompressedBuffer::FromCompressed which - // is only interested in reading the header (first 64 bytes or so) and then throws - // away the materialized data. + // See ViewOrCopyRange above for rationale on the extended vs. plain segment split. size_t SizeLeft = WriteView.GetSize(); size_t SegmentCount = m_Segments.size(); @@ -231,9 +236,20 @@ CompositeBuffer::CopyTo(MutableMemoryView WriteView, Iterator& It) const const SharedBuffer& Segment = m_Segments[It.SegmentIndex]; size_t SegmentSize = Segment.GetSize(); size_t CopySize = zen::Min(SegmentSize - It.OffsetInSegment, SizeLeft); - IoBuffer SubSegment(Segment.AsIoBuffer(), It.OffsetInSegment, CopySize); - MemoryView ReadView = SubSegment.GetView(); - WriteView = WriteView.CopyFrom(ReadView); + + IoBuffer SubSegment; // lifetime holder for the extended-segment view + MemoryView ReadView; + if (Segment.IsExtended()) + { + SubSegment = IoBuffer(Segment.AsIoBuffer(), It.OffsetInSegment, CopySize); + ReadView = SubSegment.GetView(); + } + else + { + ReadView = Segment.GetView().Mid(It.OffsetInSegment, CopySize); + } + WriteView = WriteView.CopyFrom(ReadView); + It.OffsetInSegment += CopySize; ZEN_ASSERT_SLOW(It.OffsetInSegment <= SegmentSize); if (It.OffsetInSegment == SegmentSize) |