-
Notifications
You must be signed in to change notification settings - Fork 4
perf: reduce memory usage #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
70fcbfc
perf: eliminate peer message payload copies
artrixdotdev 0b921d5
perf: avoid repeated tracker and piece work
artrixdotdev 8d93922
docs: simplify torrent actor link
artrixdotdev b02b946
perf: eliminate protocol allocation churn
artrixdotdev 4a46d59
perf: cache torrent info hashes
artrixdotdev 8ee5a79
perf: avoid redundant tracker work
artrixdotdev 4374497
fix: validate peer frame inputs
artrixdotdev 5a15b64
perf: reduce live transfer overhead
artrixdotdev 16ef5a5
test: avoid duplicate tracker assumption
artrixdotdev 52ef8d2
fix: handle completed scheduler cursor
artrixdotdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use std::{error::Error, time::Instant}; | ||
|
|
||
| use bytes::Bytes; | ||
| use libtortillas::protocol::{ | ||
| messages::PeerMessages, | ||
| stream::{PeerRecv, PeerSend, PeerStream}, | ||
| }; | ||
| use tokio::net::{TcpListener, TcpStream}; | ||
| use tracing::info; | ||
|
|
||
| const BLOCK_LENGTH: usize = 16 * 1024; | ||
| const BLOCK_COUNT: usize = 8 * 1024; | ||
|
|
||
| #[tokio::main(flavor = "current_thread")] | ||
| async fn main() -> Result<(), Box<dyn Error + Send + Sync>> { | ||
| tracing_subscriber::fmt().init(); | ||
|
|
||
| let listener = TcpListener::bind("127.0.0.1:0").await?; | ||
| let address = listener.local_addr()?; | ||
| let receiver = tokio::spawn(async move { | ||
| let (stream, _) = listener.accept().await?; | ||
| let mut stream = PeerStream::tcp(stream); | ||
| let mut bytes_received = 0usize; | ||
|
|
||
| for expected_index in 0..BLOCK_COUNT { | ||
| match stream.recv().await? { | ||
| PeerMessages::Piece(index, 0, block) | ||
| if index == expected_index as u32 | ||
| && block.len() == BLOCK_LENGTH | ||
| && block.iter().all(|byte| *byte == 0xa5) => | ||
| { | ||
| bytes_received += block.len(); | ||
| } | ||
| message => { | ||
| return Err(format!("received unexpected message: {message}").into()); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| Ok::<_, Box<dyn Error + Send + Sync>>(bytes_received) | ||
| }); | ||
|
|
||
| let block = Bytes::from(vec![0xa5; BLOCK_LENGTH]); | ||
| let mut sender = PeerStream::tcp(TcpStream::connect(address).await?); | ||
| let started_at = Instant::now(); | ||
|
|
||
| for index in 0..BLOCK_COUNT { | ||
| sender | ||
| .send(PeerMessages::Piece(index as u32, 0, block.clone())) | ||
| .await?; | ||
| } | ||
|
|
||
| let bytes_transferred = receiver.await??; | ||
| let elapsed = started_at.elapsed(); | ||
| let mebibytes = bytes_transferred as f64 / (1024.0 * 1024.0); | ||
| let throughput = mebibytes / elapsed.as_secs_f64(); | ||
| info!( | ||
| bytes_transferred, | ||
| ?elapsed, | ||
| throughput_mib_per_second = throughput, | ||
| "completed local peer transfer" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
Make completion logging edge-triggered.
completeremains true afterremaining_bytesreaches zero, so every laterMetricsChangedevent bypasses the interval. If completed torrents continue emitting metrics while seeding, logging becomes unthrottled. Track the false-to-true completion transition or make completion logging a one-shot.🤖 Prompt for AI Agents