java - Unsafe, reflection access vs toCharArray (performance) -


jdk9 team puts effort helping removing non-public dependencies (using jdeps). using unsafe class faster access strings 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.

  1. there chance there no backing char[] array @ in java 9 version of string, see jep 254. is, tochararray() option.
  2. 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?
  3. if absolutely needed, use methodhandles instead of both reflection , unsafe. methodhandles fast direct field access, unlike unsafe public, supported , safe api.

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -