Skip to content

Buffer capture file reads - #1016

Open
godlygeek wants to merge 1 commit into
bloomberg:mainfrom
godlygeek:source_buffering
Open

godlygeek wants to merge 1 commit into
bloomberg:mainfrom
godlygeek:source_buffering

Conversation

@godlygeek

@godlygeek godlygeek commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

#1011 accidentally targeted the faster-capture-padding-scan branch, rather than main.

While fixing that, I got nerd sniped into playing around, since I wasn't totally happy with how #1011 was factored, and came up with something that I think is organized better and which is actually faster as well.

This PR removes the buffering that was happening inside the SocketSource in favor of a new BufferedSource class, which wraps an existing Source and adds buffering. The RecordReader only ever reads via a BufferedSource, and the BufferedSource isn't a subclass of the Source abstract base class, so the calls made by RecordReader in its hot loop don't need to go through virtual dispatch. We only need to hop through a vtable when we need to refresh the buffer, which is once per 64KiB read for the FileSource, though more often for the SocketSource since we don't expect the socket to have 64KiB sitting in it waiting for us every time we request more data.

@godlygeek godlygeek self-assigned this Sep 2, 2026
@codecov-commenter

codecov-commenter commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.43750% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.48%. Comparing base (b4524ee) to head (7fb1ca7).

Files with missing lines Patch % Lines
src/memray/_memray/source.cpp 98.38% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1016      +/-   ##
==========================================
+ Coverage   92.42%   92.48%   +0.05%     
==========================================
  Files         102      102              
  Lines       13303    13312       +9     
  Branches      482      482              
==========================================
+ Hits        12295    12311      +16     
+ Misses       1008     1001       -7     
Flag Coverage Δ
cpp 92.48% <98.43%> (+0.05%) ⬆️
python_and_cython 92.48% <98.43%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Read data from the capture file in large chunks, and then reference that
buffered data during record parsing, refilling it as necessary. This
significantly reduces the number of virtual method calls that are needed
and significantly increases branch predictability and performance.

Signed-off-by: Matt Wozniski <godlygeek@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants