Copy a content span from a change without cloning the full `Change`. Graph output calls this for every vertex it materializes. Using `load_change()` here is expensive for imported changes because cache hits clone the entire change, including potentially large unhashed Git metadata. This path copies only the requested bytes.
(
&self,
hash: &Hash,
start: usize,
end: usize,
buf: &mut [u8],
)
| 517 | /// hits clone the entire change, including potentially large unhashed Git |
| 518 | /// metadata. This path copies only the requested bytes. |
| 519 | pub(crate) fn copy_content_span( |
| 520 | &self, |
| 521 | hash: &Hash, |
| 522 | start: usize, |
| 523 | end: usize, |
| 524 | buf: &mut [u8], |
| 525 | ) -> ChangeStoreResult<usize> { |
| 526 | if let Ok(spans) = self.spans.read() { |
| 527 | if let Some(n) = spans.copy(hash, start, end, buf) { |
| 528 | return Ok(n); |
| 529 | } |
| 530 | } |
| 531 | // Fast path: shared read lock — multiple threads can read concurrently. |
| 532 | // peek() doesn't update LRU order, which is an acceptable trade-off |
| 533 | // to avoid serializing all readers on a write lock. |
| 534 | if let Ok(cache) = self.cache.read() { |
| 535 | if let Some(change) = cache.peek(hash) { |
| 536 | return copy_content_from_change(hash, change, start, end, buf); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | let path = self.change_path(hash); |
| 541 | log::debug!( |
| 542 | "Loading change content {} from {}", |
| 543 | hash.to_base32(), |
| 544 | path.display() |
| 545 | ); |
| 546 | |
| 547 | if !path.exists() { |
| 548 | return Err(ChangeStoreError::NotFound { |
| 549 | hash: hash.to_base32(), |
| 550 | }); |
| 551 | } |
| 552 | |
| 553 | let file = File::open(&path)?; |
| 554 | let mut reader = BufReader::new(file); |
| 555 | let (change, computed_hash) = Change::deserialize(&mut reader)?; |
| 556 | |
| 557 | if computed_hash != *hash { |
| 558 | return Err(ChangeStoreError::HashMismatch { |
| 559 | expected: hash.to_base32(), |
| 560 | computed: computed_hash.to_base32(), |
| 561 | }); |
| 562 | } |
| 563 | |
| 564 | let copied = copy_content_from_change(hash, &change, start, end, buf)?; |
| 565 | |
| 566 | if let Ok(mut cache) = self.cache.write() { |
| 567 | cache.insert(*hash, change); |
| 568 | } |
| 569 | |
| 570 | Ok(copied) |
| 571 | } |
| 572 | |
| 573 | /// Delete a change from disk and the cache. |
| 574 | /// |