Skip to content

Commit 48b9311

Browse files
authored
Merge pull request #550 from rust-osdev/enhancement/bios-disk-speed
speed up BIOS bootloader
2 parents 535a1cc + 65f5385 commit 48b9311

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

bios/stage-2/src/disk.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ pub struct DiskAccess {
55
pub disk_number: u16,
66
pub base_offset: u64,
77
pub current_offset: u64,
8+
9+
/// The offset of the sector read in the last call to read_exact_at.
10+
pub last_read_exact_at_offset: Option<u64>,
811
}
912

1013
impl Read for DiskAccess {
@@ -19,7 +22,15 @@ impl Read for DiskAccess {
1922
let buf = unsafe { &mut TMP_BUF };
2023
assert!(current_sector_offset + len <= buf.buffer.len());
2124

22-
self.read_exact_into(buf.buffer.len(), buf);
25+
// Don't read the sector if we already read the same sector in the last
26+
// call.
27+
if self
28+
.last_read_exact_at_offset
29+
.is_none_or(|offset| offset != self.current_offset)
30+
{
31+
self.last_read_exact_at_offset = Some(self.current_offset);
32+
self.read_exact_into(buf.buffer.len(), buf);
33+
}
2334

2435
&buf.buffer[current_sector_offset..][..len]
2536
}

bios/stage-2/src/main.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn start(disk_number: u16, partition_table_start: *const u8) -> ! {
8383
disk_number,
8484
base_offset: u64::from(fat_partition.logical_block_address) * 512,
8585
current_offset: 0,
86+
last_read_exact_at_offset: None,
8687
};
8788

8889
let mut fs = fat::FileSystem::parse(disk.clone());
@@ -198,10 +199,17 @@ fn try_load_file(
198199
let file_size = file.file_size().into();
199200

200201
let mut total_offset = 0;
201-
for cluster in fs.file_clusters(&file) {
202-
let cluster = cluster.unwrap();
202+
let mut file_clusters = fs.file_clusters(&file).map(Result::unwrap).peekable();
203+
while let Some(cluster) = file_clusters.next() {
203204
let cluster_start = cluster.start_offset;
204-
let cluster_end = cluster_start + u64::from(cluster.len_bytes);
205+
let mut cluster_end = cluster_start + u64::from(cluster.len_bytes);
206+
207+
// Merge with the following clusters if they're contiguous.
208+
while let Some(next_cluster) =
209+
file_clusters.next_if(|next_cluster| next_cluster.start_offset == cluster_end)
210+
{
211+
cluster_end += u64::from(next_cluster.len_bytes);
212+
}
205213

206214
let mut offset = 0;
207215
loop {

0 commit comments

Comments
 (0)