Skip to content

perf: avoid per-byte allocations in random access stream - #5389

Merged
arturcic merged 2 commits into
GitTools:mainfrom
arturcic:codex/5210-managed-allocations
Oct 20, 2026
Merged

arturcic merged 2 commits into
GitTools:mainfrom
arturcic:codex/5210-managed-allocations

Conversation

@arturcic

@arturcic arturcic commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

RandomAccessStream inherits Stream.ReadByte, which allocates a temporary one-byte array on each call. GitPackReader uses this path for variable-length packed-object offsets. Override ReadByte using a stack span and the existing span read so file bounds, positions and EOF behavior remain consistent on net10 and net11.

Adds regression coverage for unsigned byte values, mixed reads, bounded/physical EOF, seek and independent stream positions, shared-handle ownership, and per-call allocations. The allocation test fails without the override (32,768 bytes for 1,024 reads) and passes with it.

Validation: all 182 managed-backend tests passed on each framework on macOS ARM64; formatting/whitespace checks passed; CodeRabbit reviewed both files with zero findings. An isolated probe saved 32 bytes per call on both runtimes. Reading 1,000 real commits saved 6,496 allocated bytes (~0.16%); this does not establish an application speedup or general memory reduction. Cross-platform CI remains to validate the PR.

Follow-up to #5210. No .NET 11-specific API or benchmark infrastructure is added.

Summary by CodeRabbit

  • New Features

    • Added support for reading individual bytes from managed random-access streams.
    • Returns an end-of-stream indicator when no more data is available.
  • Tests

    • Added coverage for byte reads, stream positions, end-of-stream behavior, shared handles, disposal, and repeated reads without allocations.
  • Documentation

    • Updated version-calculation documentation review metadata while preserving existing behavior and accuracy.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3406f1fe-d349-44d2-9821-dd7800d3d72c
📥 Commits

Reviewing files that changed from the base of the PR and between a66e893 and 3532426.

📒 Files selected for processing: 3
  • docs/version-calculation-map.json
  • src/GitVersion.Git.Managed.Tests/RandomAccessStreamTests.cs
  • src/GitVersion.Git.Managed/RandomAccessStream.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

RandomAccessStream adds ReadByte, which returns a byte value or -1 at end of stream. Tests cover read positions, shared-handle behavior, and zero allocations across repeated calls. Version-calculation documentation metadata is updated.

Changes: RandomAccessStream byte reads

Layer / File(s) Summary
Add and verify single-byte reads
src/GitVersion.Git.Managed/RandomAccessStream.cs, src/GitVersion.Git.Managed.Tests/RandomAccessStreamTests.cs, docs/version-calculation-map.json
ReadByte uses the existing Read method. Tests cover unsigned values, position advancement, end-of-stream behavior, independent positions over a shared handle, and zero allocations. The documentation metadata records the reviewed change.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 35324

This change avoids a small per-call allocation in single-byte reads while preserving existing stream behavior. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies the main change: preventing per-byte allocations in RandomAccessStream.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@arturcic
arturcic force-pushed the codex/5210-managed-allocations branch from c7c9bf0 to 3222e5b Compare October 20, 2026 13:16
@arturcic

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
✅ Action performed

Full review finished.

@sonarqubecloud

Copy link
Copy Markdown

@arturcic
arturcic marked this pull request as ready for review October 20, 2026 13:30
Copilot AI balanced review requested due to automatic review settings October 20, 2026 13:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@arturcic
arturcic merged commit 2a21bac into GitTools:main Oct 20, 2026
51 checks passed
@arturcic
arturcic deleted the codex/5210-managed-allocations branch October 20, 2026 13:52
@mergify

mergify Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Thank you @arturcic for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants