Skip to content

Commit

Permalink
re-use cells for non-empty list construction
Browse files Browse the repository at this point in the history
  • Loading branch information
khibino committed Sep 3, 2024
1 parent 795cebb commit 630cc16
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions Network/Socket/Info.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

1 comment on commit 630cc16

@kazu-yamamoto
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your idea is great.
But for this go, the original code, which does not have acc, should be used, I think.

Please sign in to comment.