<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>Hi Iavor,</p>
    <p>Thank you very much for this. It's nice to know that we have the
      ability in Haskell to be as frugal (or profligate) with memory as
      R when working with data frames. I should say this number of
      fields is quite low in the data science world. Data sets with 500
      columns are not uncommon and I did have one with 10,000 columns!<br>
    </p>
    <p> I know other folks have worked on producing something like data
      frames e.g. <a class="moz-txt-link-freetext" href="https://github.com/acowley/Frames">https://github.com/acowley/Frames</a> and
      <a class="moz-txt-link-freetext" href="http://stla.github.io/stlapblog/posts/HaskellFrames.html">http://stla.github.io/stlapblog/posts/HaskellFrames.html</a> for
      example but I wanted to remain in the world of relatively simple
      types and I haven't looked at its performance in terms of memory.
      On the plus side it did manage to read in the 10,000 column data
      set although ghc took about 5 minutes to do the typechecking (I
      should say within ghci).</p>
    <p>Just to mention that R is not the only language that has nice
      facilities for data exploration; python has a package called
      pandas: <a class="moz-txt-link-freetext" href="http://pandas.pydata.org">http://pandas.pydata.org</a>.</p>
    <p>I feel we still have a way to go to make Haskell provide as easy
      an environment for data exploration as R or Python but I shall
      continue on my crusade.</p>
    <p>Many thanks once again, Dominic.<br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 30/08/2016 22:05, Iavor Diatchki
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAGK9nurjok4XNWMTr+s07Q-phxdqP1nYhr+zYKT1_+nZyvNNMw@mail.gmail.com"
      type="cite">
      <div dir="ltr">Hello,
        <div><br>
        </div>
        <div>when you parse the CSV fully, you end up creating a lot of
          small bytestring objects, and each of these adds some
          overhead.   The vectors themselves add up some additional
          overhead.  All of this adds up when you have as many fields as
          you do.   An alternative would be to use a different
          representation for the data, which recomputes things when
          needed.   While this might be a bit slower in some cases, it
          could have significant saving in terms of memory use.   I
          wrote up a small example to illustrate what I have in mind,
          which should be attached to this e-mail.</div>
        <div><br>
        </div>
        <div>Basically, instead of parsing the CSV file fully, I just
          indexed where the lines are (ref. the "rows" field of "CSV"). 
          This allows me to access each row quickly, and the when I need
          to get a specific field, I simply parse the bytes of the row.</div>
        <div>One could play all kinds of games like that, and I imagine
          R does something similar, although I have never looked at how
          it works.   To test the approach I generated ~200Mb of sample
          data (generator is also in the attached file), and I was able
          to filter it using ~240Mb, which is comparable to what you
          reported about R.  One could probably package all this up in
          library that supports "R like" operations. </div>
        <div><br>
        </div>
        <div>These are the stats I get from -s:</div>
        <div><br>
        </div>
        <div>
          <div>   4,137,632,432 bytes allocated in the heap</div>
          <div>         925,200 bytes copied during GC</div>
          <div>     200,104,224 bytes maximum residency (2 sample(s))</div>
          <div>       6,217,864 bytes maximum slop</div>
          <div>             246 MB total memory in use (1 MB lost due to
            fragmentation)</div>
          <div><br>
          </div>
          <div>                                     Tot time (elapsed)
             Avg pause  Max pause</div>
          <div>  Gen  0      7564 colls,     0 par    0.024s   0.011s  
              0.0000s    0.0001s</div>
          <div>  Gen  1         2 colls,     0 par    0.000s   0.001s  
              0.0003s    0.0006s</div>
          <div><br>
          </div>
          <div>  INIT    time    0.000s  (  0.000s elapsed)</div>
          <div>  MUT     time    0.364s  (  0.451s elapsed)</div>
          <div>  GC      time    0.024s  (  0.011s elapsed)</div>
          <div>  EXIT    time    0.000s  (  0.001s elapsed)</div>
          <div>  Total   time    0.388s  (  0.463s elapsed)</div>
          <div><br>
          </div>
          <div>  %GC     time       6.2%  (2.5% elapsed)</div>
          <div><br>
          </div>
          <div>  Alloc rate    11,367,122,065 bytes per MUT second</div>
          <div><br>
          </div>
          <div>  Productivity  93.8% of total user, 78.6% of total
            elapsed</div>
        </div>
        <div><br>
        </div>
        <div>-Iavor</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Thu, Aug 25, 2016 at 3:31 AM, Simon
          Peyton Jones via Glasgow-haskell-users <span dir="ltr"><<a
              moz-do-not-send="true"
              href="mailto:glasgow-haskell-users@haskell.org"
              target="_blank">glasgow-haskell-users@haskell.org</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">Sounds
            bad.  But it'll need someone with bytestring expertise to
            debug.  Maybe there's a GHC problem underlying; or maybe
            it's shortcoming of bytestring.<br>
            <br>
            Simon<br>
            <div>
              <div class="h5"><br>
                |  -----Original Message-----<br>
                |  From: Glasgow-haskell-users [mailto:<a
                  moz-do-not-send="true"
                  href="mailto:glasgow-haskell-users-">glasgow-haskell-users-</a><br>
                |  <a moz-do-not-send="true"
                  href="mailto:bounces@haskell.org">bounces@haskell.org</a>]
                On Behalf Of Dominic Steinitz<br>
                |  Sent: 25 August 2016 10:11<br>
                |  To: GHC users <<a moz-do-not-send="true"
                  href="mailto:glasgow-haskell-users@haskell.org">glasgow-haskell-users@<wbr>haskell.org</a>><br>
                |  Subject: GHC Performance / Replacement for R?<br>
                |<br>
                |  I am trying to use Haskell as a replacement for R but
                running into two<br>
                |  problems which I describe below. Are there any plans
                to address the<br>
                |  performance issues I have encountered?<br>
                |<br>
                |   1. I seem to have to jump through a lot of hoops
                just to be able to<br>
                |      select the data I am interested in.<br>
                |<br>
                |  {-# LANGUAGE ScopedTypeVariables #-}<br>
                |<br>
                |  {-# OPTIONS_GHC -Wall #-}<br>
                |<br>
                |  import Data.Csv hiding ( decodeByName )<br>
                |  import qualified Data.Vector as V<br>
                |<br>
                |  import Data.ByteString ( ByteString )<br>
                |  import qualified Data.ByteString.Char8 as B<br>
                |<br>
                |  import qualified Pipes.Prelude as P<br>
                |  import qualified Pipes.ByteString as Bytes import
                Pipes import<br>
                |  qualified Pipes.Csv as Csv import System.IO<br>
                |<br>
                |  import qualified Control.Foldl as L<br>
                |<br>
                |  main :: IO ()<br>
                |  main = withFile "examples/787338586_T_ONTIME.<wbr>csv"
                ReadMode $ \h -> do<br>
                |    let csvs :: Producer (V.Vector ByteString) IO ()<br>
                |        csvs = Csv.decode HasHeader (Bytes.fromHandle
                h) >-> P.concat<br>
                |        uvectors :: Producer (V.Vector ByteString) IO
                ()<br>
                |        uvectors = csvs  >-> P.map (V.foldr
                V.cons V.empty)<br>
                |    vec_vec <- L.impurely P.foldM  L.vector uvectors<br>
                |    print $ (vec_vec :: V.Vector (V.Vector ByteString))
                V.! 17<br>
                |    print $ V.length vec_vec<br>
                |    let rockspring = V.filter (\x -> x V.! 8 ==
                B.pack "RKS") vec_vec<br>
                |    print $ V.length rockspring<br>
                |<br>
                |  Here's the equivalent R:<br>
                |<br>
                |  df <- read.csv("787338586_T_ONTIME.<wbr>csv")<br>
                |  rockspring <- df[df$ORIGIN == "RKS",]<br>
                |<br>
                |   2. Now I think I could improve the above to make an
                environment that<br>
                |      is more similar to the one my colleagues are used
                to in R but more<br>
                |      problematical is the memory usage.<br>
                |<br>
                |   * 112.5M file<br>
                |   * Just loading the source into ghci takes 142.7M<br>
                |   * > foo <- readFile
                "examples/787338586_T_ONTIME.<wbr>csv" > length foo<br>
                |     takes me up to 4.75G. But we probably don't want
                to do this!<br>
                |   * Let's try again.<br>
                |   * > :set -XScopedTypeVariables<br>
                |   * > h <- openFile
                "examples/787338586_T_ONTIME.<wbr>csv" ReadMode<br>
                |   * > let csvs :: Producer (V.Vector ByteString) IO
                () = Csv.decode<br>
                |  HasHeader (Bytes.fromHandle h) >-> P.concat<br>
                |   * > let uvectors :: Producer (V.Vector
                ByteString) IO () = csvs  >-><br>
                |  P.map (V.map id) >-> P.map (V.foldr V.cons
                V.empty)<br>
                |   * > vec_vec :: V.Vector (V.Vector ByteString)
                <- L.impurely P.foldM<br>
                |  L.vector uvectors<br>
                |   * Now I am up at 3.17G. In R I am under 221.3M.<br>
                |   * > V.length rockspring takes a long time to
                return 155 and now I am<br>
                |     at 3.5G!!! In R > rockspring <- df[df$ORIGIN
                == "RKS",] seems<br>
                |     instantaneous and now uses only 379.5M.<br>
                |   * > length(rockspring) 37 > length(df$ORIGIN)
                471949 i.e. there are<br>
                |     37 columns and 471,949 rows.<br>
                |<br>
                |  Running this as an executable gives<br>
                |<br>
                |  ~/Dropbox/Private/labels $ ./examples/BugReport +RTS
                -s ["2014-01-<br>
                |  01","EV","20366","N904EV","<wbr>2512","10747","1074702","<wbr>30747",<br>
                |   "BRO","Brownsville, TX","Texas","11298","1129803",<wbr>"30194",<br>
                |    "DFW","Dallas/Fort Worth,
                TX","Texas","0720","0718",<br>
                |    "-2.00","8.00","0726","0837","<wbr>7.00","0855","0844","-11.00","<wbr>0.00",<br>
                |    "","0.00","482.00","","","",""<wbr>,"",""]<br>
                |  471949<br>
                |  155<br>
                |    14,179,764,240 bytes allocated in the heap<br>
                |     3,378,342,072 bytes copied during GC<br>
                |       786,333,512 bytes maximum residency (13
                sample(s))<br>
                |        36,933,976 bytes maximum slop<br>
                |              1434 MB total memory in use (0 MB lost
                due to<br>
                |  fragmentation)<br>
                |<br>
                |                                       Tot time
                (elapsed)  Avg pause<br>
                |  Max pause<br>
                |    Gen  0     26989 colls,     0 par    1.423s 
                 1.483s     0.0001s<br>
                |  0.0039s<br>
                |    Gen  1        13 colls,     0 par    1.005s 
                 1.499s     0.1153s<br>
                |  0.6730s<br>
                |<br>
                |    INIT    time    0.000s  (  0.003s elapsed)<br>
                |    MUT     time    3.195s  (  3.193s elapsed)<br>
                |    GC      time    2.428s  (  2.982s elapsed)<br>
                |    EXIT    time    0.016s  (  0.138s elapsed)<br>
                |    Total   time    5.642s  (  6.315s elapsed)<br>
                |<br>
                |    %GC     time      43.0%  (47.2% elapsed)<br>
                |<br>
                |    Alloc rate    4,437,740,019 bytes per MUT second<br>
                |<br>
                |    Productivity  57.0% of total user, 50.9% of total
                elapsed<br>
                |<br>
                |  ______________________________<wbr>_________________<br>
                |  Glasgow-haskell-users mailing list<br>
                |  <a moz-do-not-send="true"
                  href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.<wbr>org</a><br>
              </div>
            </div>
            |  <a moz-do-not-send="true"
href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fmail.h"
              rel="noreferrer" target="_blank">https://na01.safelinks.<wbr>protection.outlook.com/?url=<wbr>http%3a%2f%2fmail.h</a><br>
            |  <a moz-do-not-send="true" href="http://askell.org"
              rel="noreferrer" target="_blank">askell.org</a>%2fcgi-bin%<wbr>2fmailman%2flistinfo%<wbr>2fglasgow-haskell-<br>
            |  users&data=01%7c01%7csimonpj%<a
              moz-do-not-send="true" href="http://40microsoft.com"
              rel="noreferrer" target="_blank">4<wbr>0microsoft.com</a>%<wbr>7c5017a5fe26cb4df9c41d08d<br>
            |  3ccc7b5bd%<wbr>7c72f988bf86f141af91ab2d7cd011<wbr>db47%7c1&sdata=2Ku1Fr5QttHRoj5<br>
            |  NSOJREZrt2Fsqhi63iJOpxmku68E%<wbr>3d<br>
            <div class="HOEnZb">
              <div class="h5">______________________________<wbr>_________________<br>
                Glasgow-haskell-users mailing list<br>
                <a moz-do-not-send="true"
                  href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.<wbr>org</a><br>
                <a moz-do-not-send="true"
href="http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users"
                  rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/glasgow-<wbr>haskell-users</a><br>
              </div>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>