From b2c3eb18235c6d7faa3401a429caefe4ce6ffe93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sun, 15 Oct 2017 16:17:41 +0200 Subject: [PATCH] Don't recalculate cluster in seek if not needed. --- src/file.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/file.rs b/src/file.rs index 5f38e7d..5e69490 100644 --- a/src/file.rs +++ b/src/file.rs @@ -270,19 +270,21 @@ impl<'a, 'b> Seek for File<'a, 'b> { return Ok(self.offset as u64); } let cluster_size = self.fs.get_cluster_size(); - let new_cluster = if new_pos == 0 { + // get number of clusters to seek (favoring previous cluster in corner case) + let cluster_count = ((new_pos + cluster_size as i64 - 1) / cluster_size as i64 - 1) as isize; + let old_cluster_count = ((self.offset as i64 + cluster_size as i64 - 1) / cluster_size as i64 - 1) as isize; + let new_cluster = if new_pos == 0 { None + } else if cluster_count == old_cluster_count { + self.current_cluster } else { - // get number of clusters to seek (favoring previous cluster in corner case) - let cluster_count = ((new_pos - 1) / cluster_size as i64) as isize; match self.first_cluster { Some(n) => { let mut cluster = n; let mut iter = self.fs.cluster_iter(n); for i in 0..cluster_count { cluster = match iter.next() { - Some(Err(err)) => return Err(err), - Some(Ok(n)) => n, + Some(r) => r?, None => { // chain ends before new position - seek to end of last cluster new_pos = (i + 1) as i64 * cluster_size as i64;