Speed up by reusing tokio instance #6

closed
opened by danabra.mov targeting main

Claude claims this is faster

  Changes:
  1. Added rt: tokio::runtime::Runtime field to PdsFs<R> struct
  2. Initialize runtime once in new() method
  3. Replaced two instances of Runtime::new().unwrap():
    - In attr() method (line 169-171) → now uses self.rt
    - In read() method (line 388-391) → now uses self.rt

  Measured performance improvement:
  - Before: 10.981 seconds (cold cache)
  - After: 1.769 seconds (cold cache)
  - Speedup: 6.2x faster

  The optimization eliminates the massive overhead of creating ~22,000 Tokio runtimes (one per file) during directory
   listing operations. Builds cleanly with no warnings.
Changed files
+8 -4
src
+8 -4
src/fs.rs
··· 9 9 pub struct PdsFs<R> { 10 10 repos: IndexMap<String, Repository<R>>, 11 11 inodes: IndexSet<PdsFsEntry>, 12 + rt: tokio::runtime::Runtime, 12 13 } 13 14 14 15 #[derive(Debug, Clone, PartialEq, Eq, Hash)] ··· 91 92 PdsFs { 92 93 repos: Default::default(), 93 94 inodes: IndexSet::from([PdsFsEntry::Zero, PdsFsEntry::Root]), 95 + rt: tokio::runtime::Runtime::new().unwrap(), 94 96 } 95 97 } 96 98 ··· 164 166 let did = self.inodes[col.parent].unwrap_did(); 165 167 let repo = &mut self.repos[did]; 166 168 let key = format!("{}/{}", col.nsid, r.rkey); 167 - let rt = tokio::runtime::Runtime::new().unwrap(); 168 - let size = rt 169 + let size = self 170 + .rt 169 171 .block_on(repo.get_raw::<ipld_core::ipld::Ipld>(&key)) 170 172 .ok() 171 173 .flatten() ··· 377 379 _lock: Option<u64>, 378 380 reply: fuser::ReplyData, 379 381 ) { 380 - let rt = tokio::runtime::Runtime::new().unwrap(); 381 382 if let Some(PdsFsEntry::Record(r)) = self.inodes.get_index(ino as usize) { 382 383 let col = self.inodes[r.parent].unwrap_collection(); 383 384 let did = self.inodes[col.parent].unwrap_did(); 384 385 let repo = &mut self.repos[did]; 385 386 let key = format!("{}/{}", col.nsid, r.rkey); 386 387 387 - if let Ok(Some(val)) = rt.block_on(repo.get_raw::<ipld_core::ipld::Ipld>(&key)) { 388 + if let Ok(Some(val)) = self 389 + .rt 390 + .block_on(repo.get_raw::<ipld_core::ipld::Ipld>(&key)) 391 + { 388 392 reply.data(&serde_json::to_string(&val).unwrap().as_bytes()[offset as usize..]); 389 393 return; 390 394 }