java - Unsafe, reflection access vs toCharArray (performance) -
jdk9 team puts effort helping removing non-public dependencies (using jdeps
). using unsafe
class faster access string
s inner char
array - without creating new char array. if want drop dependency on unsafe
class, need load dynamically , call unsafe.getobject
, other methods using reflection.
now wonder performances: when use reflection unsafe
, how matches string.tochararray
performances? make sense keep using unsafe
?
i assume jdk >= 7.
edit
yes, totally know can write these tests using eg jmh; takes lot of time measure different inputs , different vm versions (7,8). wonder if did this; many libraries using unsafe
.
- there chance there no backing
char[]
array @ in java 9 version ofstring
, see jep 254. is,tochararray()
option. - generally should never use
unsafe
apis unless absolutely sure neccessary. since asking question, guess not. on laptop,tochararray()
completes in 25 nanoseconds 100-chars string, i.e. call 40 million times second! have such kind of workloads? - if absolutely needed, use methodhandles instead of both reflection , unsafe. methodhandles fast direct field access, unlike
unsafe
public, supported , safe api.
Comments
Post a Comment