Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-468

Avoid allocating memory for method internal buffers, use threadlocal memory instead

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • 2.4
    • None
    • Utilities
    • all environments

    Description

      In a lot of places, we allocate new buffers dynamically via new byte[]. This is a performance drawback since many of these allocations could be avoided if we would use threadlocal buffers that can be reused. For example, consider the following code from IOUtils.java, ln 2177:

      return copyLarge(input, output, inputOffset, length, new byte[DEFAULT_BUFFER_SIZE]);

      This code allocates new memory for every copy-process, that is not used outside of the method and could easily and safely reused, as long as is is thread-local. So instead of allocating new memory, a new utility-class could provide a thread-local bytearray like this:

      byte[] buffer = ThreadLocalByteArray.ofSize(DEFAULT_BUFFER_SIZE);

      return copyLarge(input, output, inputOffset, length, buffer);

      I have not measured the performance-benefits yet, but I would expect them to be significant, especially when the streams itself are not the performance bottleneck.

      Git PR is at https://github.com/apache/commons-io/pull/6/files

      Attachments

        1. micro-benchmark.zip
          4 kB
          Thomas Neidhart
        2. monitoring_with_threadlocals.png
          41 kB
          Bernd Hopp
        3. monitoring_without_threadlocals.png
          55 kB
          Bernd Hopp
        4. performancetest_weakreference.ods
          63 kB
          Bernd Hopp
        5. performancetest.ods
          63 kB
          Bernd Hopp
        6. PerfTest.java
          3 kB
          Bernd Hopp

        Issue Links

          Activity

            People

              Unassigned Unassigned
              bernd_hopp Bernd Hopp
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 12h
                  12h
                  Remaining:
                  Remaining Estimate - 12h
                  12h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified