Contenu connexe Similaire à Java micro optimizations 2017 (20) Plus de Dmitriy Dumanskiy (6) Java micro optimizations 20176. ● Typical code
● Plain java
● System is under “highload”
● All “issues” are from open-source
Today talk
11. ● Primitive wrappers
● Boxing / Unboxing
● Regex / Pattern matching
● String concatenation in loops
Typical issues
12. ● Primitive wrappers
● Boxing / Unboxing
● Regex / Pattern matching
● String concatenation in loops
● Reflection
Typical issues
20. Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
String name = iter.next();
}
Iterator allocation
22. for (int i = 0; i < list.size(); i++) {
Data data = list.get(i);
}
Solution 1
40. If (email == null || email.equals(“”)) {
...
}
What is wrong here?
42. If (email == null || email.isEmpty()) {
...
}
Slow equals
55. String[] split2(char separator, String body) {
final int i1 = body.indexOf(separator, 1);
if (i1 == -1) {
return new String[] {body};
}
return new String[] {
body.substring(0, i1), body.substring(i1 + 1, body.length())
};
}
Solution
60. String trimmed1 = body1.trim();
String trimmed2 = body2.trim();
return trimmed1 + SEPARATOR +
trimmed2;
Solution 1
61. String trimmed1 = body1.trim();
String trimmed2 = body2.trim();
return new StringBuilder()
.append(trimmed1)
.append(SEPARATOR)
.append(trimmed2)
.toString();
Solution 2
63. StringBuilder sb = new StringBuilder();
sb.append(body1)
sb.append(SEPARATOR)
sb.append(body2)
return sb.toString();
What is wrong here?
64. StringBuilder sb = new StringBuilder();
sb.append(body1)
sb.append(SEPARATOR)
sb.append(body2)
return sb.toString();
No chaining
69. if (charset == UTF_8) {
return decodeUTF8(data);
} else if (charset == US_ASCII) {
return decodeASCII(data);
} else {
return data.getBytes(charset);
}
Solution
71. if (data == null) {
throw new NoDataException();
}
What is wrong here?
72. if (data == null) {
throw new NoDataException();
}
Exception is expensive
78. if (data == null) {
return; //or return code
}
Solution 3
79. try {
double d = Double.parseDouble(s);
} catch (NumberFormatException e) {
}
What is wrong here?
80. try {
double d = Double.parseDouble(s);
} catch (NumberFormatException e) {
}
Allocations
81. ● Does trim()
● Spams ASCIIToBinaryConverter
● Spams exceptions
● Slow
Allocations
82. try {
double d = Utils.parseDouble(s);
} catch (NumberFormatException e) {
}
Solution
84. double d = 11.2321D;
return Math.round(val);
What is wrong here?
85. double d = 11.2321D;
return Math.round(val);
Slow round
86. double d = 11.2321D;
return (int) (val + 0.5);
Solution
88. public static int bitCount(int i) {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
What is wrong here?
93. int[] ar = new int[] {1, -32, 1, 1, 5, 6, 7};
return IntStream.of(ar).anyMatch(i -> i == val)
What is wrong here?
94. int[] ar = new int[] {1, -32, 1, 1, 5, 6, 7};
return IntStream.of(ar).anyMatch(i -> i == val)
Lambda/Stream cost
96. boolean contains(int[] ar, int value) {
for (int arVal : ar) {
if (arVal == value) {
return true;
}
}
return false;
}
Solution
97. E get(int index) {
if (index >= size) {
throw new IndexOutOfBoundsException();
}
return (E) elementData[index];
}
What is wrong here?
98. E get(int index) {
if (index >= size) {
throw new IndexOutOfBoundsException();
}
return (E) elementData[index];
}
Bounds-checking
101. ● Know your data (jmap, logs, db)
● Know your flows (metrics, jstack)
● Don’t rely on JIT
● Don’t trust java :)
● Always stress test your code
● 1% vs 99%
Summary