Returning from JPoint 2017, where with great pleasure I talked with a large number of mega-cool programmers, I decided to put on my hat and delve into various kinds of optimizations.
JPoint 2017 opened with a mega plenary report by Alexey Shipilev, which he announced again in October 2016 on Joker - then I watched it online and imbued with the Curve of them W :
And if with the green and red zones it is somehow more or less clear-understandable, then with the green-yellow border everything is not so obvious - what is it? can you give an example?
Let's start with the epic sracha questions - which is faster - a handheld jedi sword
for (int i = 0; i < src.length; i++) target[i] = src[i];
or racially faithful System.arraycopy
.
At the moment, vectorization and intrinsic make both options equivalent - a matter of taste preferences.
There is a collection, for example, of lines, and we want to receive an array.
Best (enemy of good) solution:
String[] array = collection.toArray(new String[collection.size]);
Arrays of Wisdom of the Ancients teaches that new is well forgotten old
String[] array = collection.toArray(new String[0]);
Previously (when the trees were high) when creating an ArrayList / HashMap / etc instance, an array with an initial size was created inside (10 for ArrayList, 0x10 for HashMap).
Another thing is when you knew in advance how many items you were going to put in the collection - experienced Jedi create something like
List<String> strings = new ArrayList<>( values.length );
immediately push your values
and happiness - no parasitic allocations from 10 elements to your goal.
But what if there are no values themselves? I don’t want to mess with null , but we know that there will be no more, for example, three ?
List<String> strings = new ArrayList<>( 3 );
And they drove - a profit of 7 saved cells for each such list (~ 7 * 16 bytes / list), which will be filled once.
Optimize empty HashMap and ArrayList commits in openjdk - now all ArrayList / HashMap created from the designer by default keep an empty static stub - and the very first addition will create an array of 10 elements (for ArrayList, 16 for HashMap), while new ArrayList( int )
creates an array vigorously and immediately.
What do Jedi know about regular expressions?
Compiling regexp every time is expensive, experienced Padawans do something like instead of stringValue.split(regexp)
private static final Pattern PATTERN = Pattern.compile(regex); .... String[] strings = PATTERN.split(stringValue);
The profit is that the template is not compiled every time.
Lightweight implementation of String.split for simple use case
If you have a single-character separator, it is a separator of the type ( space ) or ,
( comma ), and not special. character regular expression type .
( dot ) - then no regular expression is even created close.
I will not even talk about code that clearly smells like
return new StringBuilder().append("value:").append(value).toString();
there is no benefit to it - but no readability.
You can very carefully select the heap sizes and other XX-flags - but over time, when another 1000 and 1 single business features are embedded in the project code, all these flags can more than not easily bring the desired result of speed. Consider they are rotten.
If your project is alive and thriving, and once in a hundred or two hundred years you are asked to arrange a performance (although dancing naked on drunken tables is also a performance, but I’m a little about something else), then all sorts of optimization appear, and it is possible that from that very yellow zones that, after the expiration date, start not only to smell, but also to do the inverse of the original task - to slow down the application.
There is no hope that PMD analyzers, or your favorite IDE, have actual recommendations for the version of java on which you run in PROD ==>
Perform an audit magic tricks knowledge.
Source: https://habr.com/ru/post/326242/
All Articles