Alternatives for Unsafe field access
nathanmittler at google.com
Thu Dec 8 18:03:22 UTC 2016
Apologies in advance if this isn't the correct forum for this question. My
team is working on an experimental runtime for Google's protocol buffers
which is currently relying on sun.misc.Unsafe to perform various tasks
efficiently. I'm aware that the plan is to eventually remove Unsafe
altogether, and I want to make sure that we still have a way to move
forward with future versions of Java.
The code is up on a github branch (
For an example of the sorts of things our runtime needs to do, you can look
at the serialization code (
The idea is that the runtime dynamically determines information about
message fields (e.g. field types, offsets) and stores it into a single
buffer. Our main need is fast read/write access to the private fields of
our generated message classes. Java reflection would be too slow and would
require data representation as a list of objects, rather than a continuous
buffer (much less compact and cache-friendly). Security restrictions would
be a concern as well. At first glance at Java 9, I don't see any facilities
that would help here.
This would seem to be a fairly common use case a low-level serialization
framework, such as protobuf. Are there any thoughts on how such a use case
could be supported post-Unsafe?
More information about the core-libs-dev