![](https://habrastorage.org/web/e87/f57/89d/e87f5789da254fc7b72bf55ed8bc7fb7.jpg)
Java. Java C C++? Java / HFT.
: Java , HotSpot Java, .
1.
Java HFT. , , HFT (High Frequency Trading). , . , (Peter Lawrey), Java Performance User’s Group: «HFT — , (faster than a human can see)».
HFT . , .
, . , , ? ? :
- , . .. FPGA, . , FPGA plain assembler, .
- . , , . , — ( ), — 20 ( - !). , , , .
HFT (throughput), (latency). ?
throughput (///…). .. - (/), Garbage Collection Java (, Enterprise Java!) .
latency . . , throughput (, ), latency busy-spin, .
, HFT, . Java « »? Java , C, C++?
2. «»
, 3 :
- CPU- , ,
- ,
- .
.
2.1. CPU-performance
-, Java : , , «», . .
:
int doSmth(int i) {
if (i == 1) {
goo();
} else {
foo();
}
return …;
}
( compile-time) ( PGO), : i == 1 . - , , : #1, #2, #3. - . .
#1
cmpl $1, %edi
je .L7
call goo
NEXT:
...
ret
.L7:
call foo
jmp NEXT
#2
cmpl $1, %edi
jne .L9
call foo
NEXT:
...
ret
.L9:
call goo
jmp NEXT
#3
cmpl $1, %edi
je/jne .L3
call foo/goo
jmp NEXT:
.L3:
call goo/foo
NEXT:
…
ret
Java - , , .
-, Java . . , :
void doSmth(IMyInterface impl) {
impl.doSmth();
}
: doSmth. , /++ — . , .
Java, , :
- , ,
- , .
IMyInterface, (inline) , , , .
-, Java , .
. , , x86, . , runtime-dispatching (, ifunc' LINUX), ?
Java . , AVX, , , .
2.2.
: , () . , 3 .
2.2.1
data layout. ++ — , Java. C++):
class A {
int i;
};
class B {
long l;
};
class C {
A a;
B b;
};
C c;
C/C++, - , ( data-layout , ):
![](https://habrastorage.org/web/38d/cb0/a4d/38dcb0a4dc6d4bcfa7768b39ed428943.png)
.. ‘return c.a.i + c.b.l’ x86:
mov (%rdi), %rax ; << c.a.i
add ANY_OFFSET(%rdi), %rax ; << c.b.l c.a.i
ret
, . , c.a.i, - 64 , , , , c.b.l. , .
Java? , ( ), , , :
![](https://habrastorage.org/web/de2/510/8d9/de25108d978346b1b9a774ff75ae5d48.png)
‘c.a.i + c.b.l’ - x86:
mov (%rdi), %rax ; << a
mov 8(%rdi), %rdx ; << b
mov (%rax), %rax ; << i a
add (%rdx), %rax ; << l b
-, -. .
2.2.2. ()
Java ( , ).
Java TLAB' (Thread local allocation buffer), , . , .
, TLAB' 04000. , , 16 04010. 04000:04010. , TLAB' ( thread-local buffer, ), !
, operator new/malloc/realloc/calloc. , , , Java. (heap) , latency.
2.2.3.
Java , (Garbage collector). , .
. , : , , .
try-finally try-with-resources.
:
Java:
{
try (Connection c = createConnection()) {
...
}
}
:
{
Connection c = createConnection();
try {
...
} finally {
c.close();
}
}
, ++
{
Connection c = createConnection();
} // scope'
. , Java, Stop-The-World. ( , throughput, latency) — Garbage Collection.
«» . ( C/C++ delete), . new, . , , . , :
:
{
Object obj = new Object();
.....
// ,
}
:
{
Object obj = Storage.malloc(); //
...
Storage.free(obj); //
}
, , , , .
2.3.
Java C C++. , ( 4 OSI ), , . , C/C++, Java.
3.
Java . , -. , Garbage Collection , . , C++ : (: “” — ). Java , stack trace ( !). Java HFT , .
4.
HFT , : , . — ( ), . , . , , - (Quantitative Researcher’), , , . , Java — . :
- ;
- ;
- ( , C++ , IDEA);
- ;
- .
, : Java HFT . Java, ++, . Java C++ , Java , /++ .