From 630cc16eb66bbff955e7bb2cc91a2822a088ba05 Mon Sep 17 00:00:00 2001 From: Kei Hibino Date: Tue, 3 Sep 2024 16:43:00 +0900 Subject: [PATCH] re-use cells for non-empty list construction --- Network/Socket/Info.hsc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Network/Socket/Info.hsc b/Network/Socket/Info.hsc index 1f9b7ac9..b8124d76 100644 --- a/Network/Socket/Info.hsc +++ b/Network/Socket/Info.hsc @@ -317,16 +317,15 @@ followAddrInfo ptr_ai | otherwise = do a <- peek ptr_ai ptr <- (# peek struct addrinfo, ai_next) ptr_ai - go ptr a + (a :|) <$> go id ptr where - go :: Ptr AddrInfo -> AddrInfo -> IO (NonEmpty AddrInfo) - go ptr a - | ptr == nullPtr = return $ NE.singleton a + go :: ([AddrInfo] -> [AddrInfo]) -> Ptr AddrInfo -> IO [AddrInfo] + go acc ptr + | ptr == nullPtr = return $ acc [] | otherwise = do a' <- peek ptr ptr' <- (# peek struct addrinfo, ai_next) ptr - as <- go ptr' a' - return $ NE.cons a as + go (acc . (a':)) ptr' foreign import ccall safe "hsnet_getaddrinfo" c_getaddrinfo :: CString -> CString -> Ptr AddrInfo -> Ptr (Ptr AddrInfo)