[MLton-user] StreamIOExtra.anon.first.doit

Scott Cruzen sic@lerp.com
Wed, 23 Feb 2005 12:14:07 -0800


--HcAYCG3uE/tztfnV
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

If the attached program is invoked as tail and given a file that does
not grow, it constantly allocates memory. I think it's a general
problem that inputN at the end of a file allocates memory.

I ran it under the profiler and see that StreamIOExtra.anon.first.doit
is allocating the most.

I think this is around line 501 of stream-io.fun in inputN

I'm not sure if it's a bug exactly. If not, any suggestions for
fixing readline.sml?

Thanks,
Scott

--HcAYCG3uE/tztfnV
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="readline.sml"

fun tailf f stream =
   let
      val (s, stream') =
         case TextIO.StreamIO.inputLine stream of
            SOME s => s
          | NONE =>
               (OS.Process.sleep (Time.fromSeconds 1)
                ; TextIO.StreamIO.inputN (stream, 1))
   in
      (f s; tailf f stream')
   end

fun cat f stream =
   case TextIO.StreamIO.inputLine stream of
      SOME (s,stream') => (f s; cat f stream')
    | NONE => ()

val _ =
   let
      val x =
         if String.isSuffix "tail" (CommandLine.name()) then
            tailf
         else
            cat
      val args = CommandLine.arguments()
      val streams =
         if List.null args then
            [TextIO.getInstream TextIO.stdIn]
         else
            map (TextIO.getInstream o TextIO.openIn) args
   in
      map (x print) streams
   end

--HcAYCG3uE/tztfnV--