Hello Guest

[CLOSED] make BufferUtils.create***Buffer() return page aligned buffers

  • 2 Replies
  • 9510 Views
*

Offline Riven

  • *
  • 15
Recently java.nio.ByteBuffer.allocateDirect( ) changed from returning page aligned ByteBuffers to returning ByteBuffer that lacked any alignment. There is no way in Java (without using sun.misc.Unsafe or JNI) to request a page aligned buffer, so the new behaviour of java.nio.ByteBuffer.allocateDirect( ) makes it impossible to end up with said page aligned buffer.

LWJGL's BufferUtil class has been around to fix all the annoyances of ByteBuffer.allocateDirect - mainly setting the native ByteOrder, and filling a need created by the lack of methods in the Java API such as: FloatBuffer.allocateDirect. Long story short, it's there for convenience. IMHO page aligned ByteBuffers are equally a convenience that could/should be provided by LWJGL, as certain OpenGL functions simply require page aligned pointers (AMD_pinned_memory, for one). Therefore I request that LWJGL 'restores' the behaviour of ByteBuffer.allocateDirect that we enjoyed since Java 1.4, without telling everybody that the only way their application won't crash is to pass -XX:+PageAlignDirectMemory on the commandline.

Either that, or plan B:
   BufferUtils.createByteBuffer(...)
   BufferUtils.createFloatBuffer(...)
   BufferUtils.createPageAlignedByteBuffer(...)
   BufferUtils.createPageAlignedFloatBuffer(...)

*

Offline spasi

  • *****
  • 2261
    • WebHotelier
Re: [RFE] make BufferUtils.create***Buffer() return page aligned buffers
« Reply #1 on: October 31, 2013, 17:41:19 »
This has been implemented in LWJGL 3. The default alignment (page, cache-line, default/unaligned) for buffers created via BufferUtils is configurable using a system property. There is also new API to explicitly allocate aligned/unaligned ByteBuffers. This is an advanced feature and since there haven't been more requests for this for LWJGL 2, we'll skip it for now.

Keep in mind that changing the default alignment to the page size may negatively impact memory usage for applications that allocate many small buffers. Also, the default alignment provided by ByteBuffer.allocateDirect is 8 bytes, which means natural alignment for all primitive values and is good enough for most use-cases.

*

Offline spasi

  • *****
  • 2261
    • WebHotelier
I was just reading this interesting post. It shows how using page-alignment for all your buffers can have a serious impact on cache utilization. Even if you don't care about the memory overhead, alignment should only be applied when absolutely required (e.g. for AMD_pinned_memory).