Introduce IterableDir.iterateAssumeFirstIteration

This allows for avoiding an unnecessary lseek (or equivalent) call in places where it can be known that the fd has not had its cursor modified yet.
This commit is contained in:
Ryan Liptak
2022-10-04 23:24:19 -07:00
parent 6ac0d2d9d6
commit 5059384b57

View File

@@ -811,6 +811,17 @@ pub const IterableDir = struct {
};
pub fn iterate(self: IterableDir) Iterator {
return self.iterateImpl(true);
}
/// Like `iterate`, but will not reset the directory cursor before the first
/// iteration. This should only be used in cases where it is known that the
/// `IterableDir` has not had its cursor modified yet (e.g. it was just opened).
pub fn iterateAssumeFirstIteration(self: IterableDir) Iterator {
return self.iterateImpl(false);
}
fn iterateImpl(self: IterableDir, first_iter_start_value: bool) Iterator {
switch (builtin.os.tag) {
.macos,
.ios,
@@ -825,20 +836,20 @@ pub const IterableDir = struct {
.index = 0,
.end_index = 0,
.buf = undefined,
.first_iter = true,
.first_iter = first_iter_start_value,
},
.linux, .haiku => return Iterator{
.dir = self.dir,
.index = 0,
.end_index = 0,
.buf = undefined,
.first_iter = true,
.first_iter = first_iter_start_value,
},
.windows => return Iterator{
.dir = self.dir,
.index = 0,
.end_index = 0,
.first_iter = true,
.first_iter = first_iter_start_value,
.buf = undefined,
.name_data = undefined,
},