Skip to content

Conversation

@rklaehn
Copy link
Contributor

@rklaehn rklaehn commented Dec 17, 2025

Description

Prevent WeakConnectionHandle from keeping the allocation for a ConnectionInner alive.

ConnectionInner is quite large, 6048 bytes when I measured it. Arc is just an 8 byte pointer. So before the weak handle would keep a large allocation alive. Now it just keeps a tiny allocation alive, consisting of a pointer and 2 refcounts, so 24 bytes on 64 bit machines.

The downside of this approach is that it does a tiny allocation in fn weak_handle. The alternative would be to do the double arc inside Connection, which would make every connection access a double dereference, but prevent this allocation.

I would assume that tiny allocations are pretty fast though. But just in case we want to do the other variant, here it is: #277

Breaking Changes

None

Notes & open questions

…ion for a ConnectionInner alive

ConnectionInner is quite large, so before the weak handle would keep a large
allocation alive. Now it just keeps a tiny allocation alive, consisting of
a pointer and 2 refcounts.

The downside of this approach is that it does a tiny allocation in fn weak_handle.
The alternative would be to do the double arc inside Connection, which would
make every connection access a double dereference, but prevent this
allocation.
/// Returns a weak reference to the inner connection struct.
pub fn weak_handle(&self) -> WeakConnectionHandle {
WeakConnectionHandle(Arc::downgrade(&self.0.0))
WeakConnectionHandle(Arc::downgrade(&Arc::new(self.0.0.clone())))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the allocation we have to do.

Copy link
Member

Choose a reason for hiding this comment

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

I'm confused, is Arc::downgrade(&Arc::new(..)) not leading to a strong count of 0, the inner value being dropped right away and thus all upgrades would fail?

Copy link
Member

@ramfox ramfox Dec 19, 2025

Choose a reason for hiding this comment

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

Ooof yes, this is happening, just verified it in my example.

Copy link
Member

Choose a reason for hiding this comment

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

... which also shows we're very much missing tests for this both in quinn and in iroh

@github-actions
Copy link

Documentation for this PR has been generated and is available at: https://n0-computer.github.io/quinn/pr/276/docs/iroh_quinn/

Last updated: 2025-12-17T12:22:43Z

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.67%. Comparing base (4dcb3a7) to head (cd9c23c).

Files with missing lines Patch % Lines
quinn/src/connection.rs 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #276      +/-   ##
==========================================
- Coverage   76.69%   76.67%   -0.02%     
==========================================
  Files          83       83              
  Lines       23241    23241              
==========================================
- Hits        17824    17821       -3     
- Misses       5417     5420       +3     

☔ View full report in Codecov by Sentry.
📢 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.

@rklaehn rklaehn requested a review from ramfox December 17, 2025 12:29
@n0bot n0bot bot added this to iroh Dec 17, 2025
@github-project-automation github-project-automation bot moved this to 🏗 In progress in iroh Dec 17, 2025
Copy link
Member

@ramfox ramfox left a comment

Choose a reason for hiding this comment

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

From testing on iroh in n0-computer/iroh#3785 , this indeed has the expected impact!

Copy link
Collaborator

@flub flub left a comment

Choose a reason for hiding this comment

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

Just making sure this doesn't show up as ready because it seems buggy.

My understanding is that this is some kind of release blocker, adding to appropriate milestone.

@flub flub added this to the iroh 1.0 RC milestone Dec 22, 2025
@rklaehn
Copy link
Contributor Author

rklaehn commented Jan 5, 2026

Not sure what I was thinking, this doesn't work. So closing this in favour of the version that always does a double deref: #277

@rklaehn rklaehn closed this Jan 5, 2026
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in iroh Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

6 participants