authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-10-04 23:24:19-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-10-05 02:20:13-07:00
log5059384b570a4cb554215c8091ff00d77a0ebadf
tree5a8c255057529535530a9921c882f028cf3dd420
parent6ac0d2d9d6a28dc79f78ed9a4e66f5145d6d7765

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.

1 files changed, 14 insertions(+), 3 deletions(-)

lib/std/fs.zig+14-3
...@@ -811,6 +811,17 @@ pub const IterableDir = struct {...@@ -811,6 +811,17 @@ pub const IterableDir = struct {
811 };811 };
812812
813 pub fn iterate(self: IterableDir) Iterator {813 pub fn iterate(self: IterableDir) Iterator {
814 return self.iterateImpl(true);
815 }
816
817 /// Like `iterate`, but will not reset the directory cursor before the first
818 /// iteration. This should only be used in cases where it is known that the
819 /// `IterableDir` has not had its cursor modified yet (e.g. it was just opened).
820 pub fn iterateAssumeFirstIteration(self: IterableDir) Iterator {
821 return self.iterateImpl(false);
822 }
823
824 fn iterateImpl(self: IterableDir, first_iter_start_value: bool) Iterator {
814 switch (builtin.os.tag) {825 switch (builtin.os.tag) {
815 .macos,826 .macos,
816 .ios,827 .ios,
...@@ -825,20 +836,20 @@ pub const IterableDir = struct {...@@ -825,20 +836,20 @@ pub const IterableDir = struct {
825 .index = 0,836 .index = 0,
826 .end_index = 0,837 .end_index = 0,
827 .buf = undefined,838 .buf = undefined,
828 .first_iter = true,839 .first_iter = first_iter_start_value,
829 },840 },
830 .linux, .haiku => return Iterator{841 .linux, .haiku => return Iterator{
831 .dir = self.dir,842 .dir = self.dir,
832 .index = 0,843 .index = 0,
833 .end_index = 0,844 .end_index = 0,
834 .buf = undefined,845 .buf = undefined,
835 .first_iter = true,846 .first_iter = first_iter_start_value,
836 },847 },
837 .windows => return Iterator{848 .windows => return Iterator{
838 .dir = self.dir,849 .dir = self.dir,
839 .index = 0,850 .index = 0,
840 .end_index = 0,851 .end_index = 0,
841 .first_iter = true,852 .first_iter = first_iter_start_value,
842 .buf = undefined,853 .buf = undefined,
843 .name_data = undefined,854 .name_data = undefined,
844 },855 },