Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:12:29+00:00 2026-06-12T05:12:29+00:00

How can I read multiple files as a single ByteString lazily with constant memory?

  • 0

How can I read multiple files as a single ByteString lazily with constant memory?

readFiles :: [FilePath] -> IO ByteString

I currently have the following implementation but from what I have seen from profiling as well as my understanding I will end with n-1 of the files in memory.

readFiles = foldl1 joinIOStrings . map ByteString.readFile
    where joinIOStrings ml mr = do
                                l <- ml
                                r <- mr
                                return $ l `ByteString.append` r

I understand that the flaw here is that I am applying the IO actions then rewrapping them so what I think I need is a way to replace the foldl1 joinIOStrings without applying them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:12:30+00:00Added an answer on June 12, 2026 at 5:12 am

    How can I read multiple files as a single ByteString lazily with constant memory?

    If you want constant memory usage, you need Data.ByteString.Lazy. A strict ByteString cannot be read lazily, and would require O(sum of filesizes) memory.

    For a not too large number of files, simply reading them all (D.B.L.readFile reads lazily) and concatenating the results is good,

    import qualified Data.ByteString.Lazy as L
    
    readFiles :: [FilePath] -> IO L.ByteString
    readFiles = fmap L.concat . mapM L.readFile
    

    The mapM L.readFile will open the files, but only read the contents of each file when it is demanded.

    If the number of files is large, so that the limit of open file handles allowed by the OS for a single process could be exhausted, you need something more complicated. You can cook up your own lazy version of mapM,

    import System.IO.Unsafe (unsafeInterleaveIO)
    
    mapM_lazy :: [IO a] -> IO [a]
    mapM_lazy [] = return []
    mapM_lazy (x:xs) = do
                  r <- x
                  rs <- unsafeInterleaveIO (mapM_lazy xs)
                  return (r:rs)
    

    so that each file will only be opened when its contents are needed, when previously read files can already be closed. There’s a slight possibility that that still runs into resource limits, since the time of closing the handles is not guaranteed.

    Or you can use your favourite iteratee, enumerator, conduit or whatever package that solves the problem in a systematic way. Each of them has its own advantages and disadvantages with respect to the others and, if coded correctly, eliminates the possibility of accidentally hitting the resource limit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read multiple files (can be of any format i.e. pdf,
I need to read multiple files into a single function A. I need to
I have a method that extracts multiple zip files but can't delete them until
How can I read multiple lines at a time from a file in Ruby?
In C#, can multiple threads read and write to a Dictionary provided each thread
I've read in several places that a C struct can safely be defined multiple
I can read unmanaged memory in C# using UnmanagedMemoryStream, but how can I do
I need to read a single file using multiple threads under Linux. There are
I have read that combining all of your css files into one big one,
I am building an android application that reads text files. Now,i have multiple text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.