YuyuZha0 opened a new pull request #443: Optimize string split methods: 1. Use ThreadLocal to make reuse of th… URL: https://github.com/apache/commons-lang/pull/443 ####Optimize string split methods: - In current impl of split methods, a string array list is created every time the method called, this should increase gc burden, also, if a string contains too many segments, it should contains several array resizing during the method call. Use ThreadLocal to make reuse of ArrayList and avoid resizing, this trick can also be found on JDK: see java.math.BigDecimal#toString(); ``` // Private class to build a string representation for BigDecimal object. // "StringBuilderHelper" is constructed as a thread local variable so it is // thread safe. The StringBuilder field acts as a buffer to hold the temporary // representation of BigDecimal. The cmpCharArray holds all the characters for // the compact representation of BigDecimal (except for '-' sign' if it is // negative) if its intCompact field is not INFLATED. It is shared by all // calls to toString() and its variants in that particular thread. static class StringBuilderHelper { final StringBuilder sb; // Placeholder for BigDecimal string final char[] cmpCharArray; // character array to place the intCompact StringBuilderHelper() { sb = new StringBuilder(); // All non negative longs can be made to fit into 19 character array. cmpCharArray = new char[19]; } // Accessors. StringBuilder getStringBuilder() { sb.setLength(0); return sb; } ... ``` - From JetBrains Intellij Idea inspection: > There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). > > In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation. > > This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs). See more:[https://shipilev.net/blog/2016/arrays-wisdom-ancients/](https://shipilev.net/blog/2016/arrays-wisdom-ancients/)
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
