Menu

#76 CVSWriter produces lots of garbage in runtime

closed
gc (1)
5
2016-06-07
2016-03-31
No

CSVWriter produces a new StringBuilder on each writeNext.

Moreover StringBuilder is preallocated in a wrong manner: let's say we have nextLine array of 2 items each of them of 10 chars,

StringBuilder sb = new StringBuilder(nextLine.length * 2);

preallocates just 4 bytes instead of 16 by default - so extra garbage load on builder expansion.

processLine creates new StringBuilder and after that converts to String - it is unnecessary - it could appends to the same sb.

One of the solutions how to drastically reduce the garbage pressure it to have private StringBuilder and reuse it, the other solution is to use BufferedWriter and appends immediately to it (instead of StringBuilder).

For the performance reasons I made my own BufferedWriter w/o extra sync because thread safely is not required for us.

I can provide patch.

Related

Feature Requests: #76

Discussion

  • Scott Conway

    Scott Conway - 2016-04-01

    Please send patch and I will run it through the unit test and performance
    test it.

    The next release is already full up but I will try and test it out for the
    release after.

    Scott :)

    On Thu, Mar 31, 2016 at 3:55 AM, Dolzhenko Vladimir Alexandrovic bob_marlin@users.sf.net wrote:


    Status: open
    Group: Next Release (example)
    Labels: gc
    Created: Thu Mar 31, 2016 08:55 AM UTC by Dolzhenko Vladimir
    Alexandrovic
    Last Updated: Thu Mar 31, 2016 08:55 AM UTC
    Owner: nobody

    CSVWriter produces a new StringBuilder on each writeNext.

    Moreover StringBuilder is preallocated in a wrong manner: let's say we
    have nextLine array of 2 items each of them of 10 chars,

    StringBuilder sb = new StringBuilder(nextLine.length * 2);

    preallocates just 4 bytes instead of 16 by default - so extra garbage load
    on builder expansion.

    processLine creates new StringBuilder and after that converts to String -
    it is unnecessary - it could appends to the same sb.

    One of the solutions how to drastically reduce the garbage pressure it to
    have private StringBuilder and reuse it, the other solution is to use
    BufferedWriter and appends immediately to it (instead of StringBuilder).

    For the performance reasons I made my own BufferedWriter w/o extra sync
    because thread safely is not required for us.

    I can provide patch.

    Sent from sourceforge.net because you indicated interest in
    https://sourceforge.net/p/opencsv/feature-requests/76/

    To unsubscribe from further messages, please visit
    https://sourceforge.net/auth/subscriptions/

    --
    Scott Conway
    scott.conway@gmail.com
    http://www.conwayfamily.name

     

    Related

    Feature Requests: #76

  • Dolzhenko Vladimir Alexandrovic

    So - that's a jmh based benchmark

     
  • Dolzhenko Vladimir Alexandrovic

    That's a patch implies idea of reusable StringBuilder

     
  • Dolzhenko Vladimir Alexandrovic

    That's a patch implies idea of not thread safety BufferedWriter (avoid garbage pressure as well + avoid unnecessary copying from StringBuilder-String to Writer)

     
  • Andrew Rucker Jones

    • assigned_to: Scott Conway
     
  • Scott Conway

    Scott Conway - 2016-06-07

    The Buffered writer is no longer needed because of the changes made in 3.8 where we are using the writer the user passed in instead of wrapping it in a PrintWriter.

    But your reusedStringBuilder patch is what gave me pause. I personally have never tested openCSV in a multi-threaded environment though I have seen several questions early on asking if it supported threading. This patch would definitely force us to be single threaded because everything is sharing the same StringBuffer.

    Vladimir I am going to have to think on this one.

     
  • Scott Conway

    Scott Conway - 2016-06-07

    Actually take that back - your change in Patch #57 took out the need for the StringBuilder by allowing the user to pass in their own Appendable and put the threadsafety issue into their hands <BG>.

     
  • Scott Conway

    Scott Conway - 2016-06-07
    • status: open --> accepted
     
  • Scott Conway

    Scott Conway - 2016-06-07
    • status: accepted --> closed
     

Log in to post a comment.