Skip to content

Commit 835431b

Browse files
allisonkarlitskayacgwalters
authored andcommitted
image: change inline data from Vec<u8> to Box<[u8]>
This makes more sense, and we don't risk accidentally carrying around spare capacity. Do the same for splitstream as well. This requires a couple of adjustments around the tree, but nothing major. Take the chance to make better use of the new Buffer API in rustix to avoid a copy (from a slice subset of a vector back into a vector) when reading the file data from the filesystem. Signed-off-by: Allison Karlitskaya <[email protected]>
1 parent 5f9d7e2 commit 835431b

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

src/fs.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::{
1313

1414
use anyhow::{ensure, Result};
1515
use rustix::{
16+
buffer::spare_capacity,
1617
fd::{AsFd, OwnedFd},
1718
fs::{
1819
fstat, getxattr, linkat, listxattr, mkdirat, mknodat, openat, readlinkat, symlinkat,
@@ -184,18 +185,19 @@ impl FilesystemReader<'_> {
184185
let content = match FileType::from_raw_mode(buf.st_mode) {
185186
FileType::Directory | FileType::Unknown => unreachable!(),
186187
FileType::RegularFile => {
187-
let mut buffer = vec![MaybeUninit::uninit(); buf.st_size as usize];
188-
let (data, _) = read(fd, &mut buffer)?;
188+
let mut buffer = Vec::with_capacity(buf.st_size as usize);
189+
read(fd, spare_capacity(&mut buffer))?;
190+
let buffer = Box::from(buffer);
189191

190192
if buf.st_size > INLINE_CONTENT_MAX as i64 {
191193
let id = if let Some(repo) = self.repo {
192-
repo.ensure_object(data)?
194+
repo.ensure_object(&buffer)?
193195
} else {
194-
FsVerityHasher::hash(data)
196+
FsVerityHasher::hash(&buffer)
195197
};
196198
LeafContent::Regular(RegularFile::External(id, buf.st_size as u64))
197199
} else {
198-
LeafContent::Regular(RegularFile::Inline(Vec::from(data)))
200+
LeafContent::Regular(RegularFile::Inline(buffer))
199201
}
200202
}
201203
FileType::Symlink => {

src/image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Stat {
2222

2323
#[derive(Debug)]
2424
pub enum RegularFile {
25-
Inline(Vec<u8>),
25+
Inline(Box<[u8]>),
2626
External(Sha256HashValue, u64),
2727
}
2828

src/oci/image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn file_entry(path: &str) -> oci::tar::TarEntry {
122122
st_mtim_sec: 0,
123123
xattrs: RefCell::new(BTreeMap::new()),
124124
},
125-
item: oci::tar::TarItem::Leaf(LeafContent::Regular(RegularFile::Inline(vec![]))),
125+
item: oci::tar::TarItem::Leaf(LeafContent::Regular(RegularFile::Inline([].into()))),
126126
}
127127
}
128128

src/selabel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn openat<'a>(
116116

117117
match &dir.entries[idx].inode {
118118
Inode::Leaf(leaf) => match &leaf.content {
119-
LeafContent::Regular(RegularFile::Inline(data)) => Ok(Some(Box::new(data.as_slice()))),
119+
LeafContent::Regular(RegularFile::Inline(data)) => Ok(Some(Box::new(&**data))),
120120
LeafContent::Regular(RegularFile::External(id, ..)) => {
121121
Ok(Some(Box::new(File::from(repo.open_object(id)?))))
122122
}

src/splitstream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl SplitStreamWriter<'_> {
168168

169169
#[derive(Debug)]
170170
pub enum SplitStreamData {
171-
Inline(Vec<u8>),
171+
Inline(Box<[u8]>),
172172
External(Sha256HashValue),
173173
}
174174

@@ -315,7 +315,7 @@ impl<R: Read> SplitStreamReader<R> {
315315
read_into_vec(&mut self.decoder, &mut content, stored_size)?;
316316
content.truncate(actual_size);
317317
self.inline_bytes -= stored_size;
318-
Ok(SplitStreamData::Inline(content))
318+
Ok(SplitStreamData::Inline(content.into()))
319319
}
320320
}
321321

tests/mkfs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn simple(fs: &mut FileSystem) {
5454
add_leaf(
5555
&mut fs.root,
5656
"regular-inline",
57-
LeafContent::Regular(RegularFile::Inline(b"hihi".to_vec())),
57+
LeafContent::Regular(RegularFile::Inline((*b"hihi").into())),
5858
);
5959
add_leaf(
6060
&mut fs.root,

0 commit comments

Comments
 (0)