Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Span<T> out of the box you can use with any type, not just Byte, Char, Double, Float, Int, Long, Short

It can be put over an array (as per ByteBuffer); but also over stack allocated memory; a native pointer T* etc

The underlying allocation is separate from the type



ByteBuffer provides universal access to on heap/off heap data.

JNI or Unsafe allow to create ByteBuffer from native pointers, created in C/C++ or in Java using malloc, memcopy them, etc. This is used (and abused) by most web servers, DataStax or LMAX-Exchange have even created their whole business on that.

You also have have CharBuffer, DoubleBuffer, FloatBuffer, etc

There is no stack allocated ByteBuffer in Java (Java provides no stack access, this is religious) but in Java can do relaxed data access on Buffer element, volatile access, opaque access, CAS, etc.

This is the same API.


Does it work over non-primitive and user defined types?

Object, InetAddress, etc

Though only `struct` value types should be used in stack or native memory due to risk of GC holes


No, not yet. I believe there is a prototype of that kind of buffers that comes with the valhalla project (value types) but i've not tested it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: