Proposal for Direct-X-Buffer change to improve allocation of zero capacity buffers

Jon V. sybersnake at gmail.com
Mon Aug 1 04:24:45 UTC 2016


The Direct Buffer does not optimize the allocation of zero capacity
buffers.  I am proposing a simple if( cap != 0 )  condition that will
bypass most of the setup process.

Here is the current constructor:

    Direct$Type$Buffer$RW$(int cap) {                   // package-private
#if[rw]
        super(-1, 0, cap, cap);
        boolean pa = VM.isDirectMemoryPageAligned();
        int ps = Bits.pageSize();
        long size = Math.max(1L, (long)cap + (pa ? ps : 0));
        Bits.reserveMemory(size, cap);

        long base = 0;
        try {
            base = unsafe.allocateMemory(size);
        } catch (OutOfMemoryError x) {
            Bits.unreserveMemory(size, cap);
            throw x;
        }
        unsafe.setMemory(base, size, (byte) 0);
        if (pa && (base % ps != 0)) {
            // Round up to page boundary
            address = base + ps - (base & (ps - 1));
        } else {
            address = base;
        }
        cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
        att = null;
#else[rw]
        super(cap);
#end[rw]
    }


More information about the jdk8u-dev mailing list