Skip to content

Commit afddde2

Browse files
Make some micro optimizations in checkDirStatus
1 parent df792f2 commit afddde2

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

core/src/Streamly/Internal/FileSystem/Posix/ReadDir.hsc

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,24 @@ isMetaDir :: Ptr CChar -> IO Bool
127127
isMetaDir dname = do
128128
-- XXX Assuming an encoding that maps "." to ".", this is true for
129129
-- UTF8.
130+
-- Load as soon as possible to optimize memory accesses
130131
c1 <- peek dname
132+
c2 :: Word8 <- peekByteOff dname 1
131133
if (c1 /= fromIntegral (ord '.'))
132134
then return False
133135
else do
134-
c2 :: Word8 <- peekByteOff dname 1
135136
if (c2 == 0)
136137
then return True
137-
else if (c2 /= fromIntegral (ord '.'))
138-
then return False
139138
else do
140-
c3 :: Word8 <- peekByteOff dname 2
141-
if (c3 == 0)
142-
then return True
143-
else return False
139+
if (c2 /= fromIntegral (ord '.'))
140+
then return False
141+
else do
142+
c3 :: Word8 <- peekByteOff dname 2
143+
if (c3 == 0)
144+
then return True
145+
else return False
144146

147+
{-# NOINLINE lstatDname #-}
145148
lstatDname :: PosixPath -> Ptr CChar -> IO (Bool, Bool)
146149
lstatDname parent dname = do
147150
isMeta <- liftIO $ isMetaDir dname
@@ -167,13 +170,13 @@ checkDirStatus
167170
checkDirStatus parent dname _ = lstatDname parent dname
168171
#else
169172
checkDirStatus parent dname dtype =
170-
if dtype == #const DT_UNKNOWN
171-
then lstatDname parent dname
172-
else if dtype == (#const DT_DIR)
173+
if dtype == (#const DT_DIR)
173174
then do
174175
isMeta <- liftIO $ isMetaDir dname
175176
pure (True, isMeta)
176-
else pure (False, False)
177+
else if dtype /= #const DT_UNKNOWN
178+
then pure (False, False)
179+
else lstatDname parent dname
177180
#endif
178181

179182
-- XXX We can use getdents64 directly so that we can use array slices from the

0 commit comments

Comments
 (0)