Por que o diferencial da curva de desempenho ímpar entre ByteBuffer.allocate () e ByteBuffer.allocateDirect ()

Estou trabalhando em algunsSocketChannel-para-SocketChannel código que funcionará melhor com um buffer de byte direto - com vida útil longa e grande (dezenas a centenas de megabytes por conexão).FileChannels, executei alguns micro-benchmarks emByteBuffer.allocate() vs.ByteBuffer.allocateDirect() desempenho.

Houve uma surpresa nos resultados que eu realmente não consigo explicar. No gráfico abaixo, há um penhasco muito pronunciado nos 256KB e 512KB para oByteBuffer.allocate() implementação de transferência - o desempenho cai em ~ 50%! Também parece haver um penhasco de desempenho menor para oByteBuffer.allocateDirect(). (A série% de ganho ajuda a visualizar essas alterações.)

Tamanho do buffer (bytes) versus tempo (MS)

Por que o diferencial da curva de desempenho ímpar entreByteBuffer.allocate() eByteBuffer.allocateDirect()? O que exatamente está acontecendo atrás da cortina?

Talvez seja muito dependente de hardware e sistema operacional, então, aqui estão esses detalhes:

MacBook Pro com CPU Core 2 de núcleo duploUnidade SSD Intel X25MOSX 10.6.4

Código fonte, mediante solicitação:

package ch.dietpizza.bench;

import static java.lang.String.format;
import static java.lang.System.out;
import static java.nio.ByteBuffer.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class SocketChannelByteBufferExample {
    private static WritableByteChannel target;
    private static ReadableByteChannel source;
    private static ByteBuffer          buffer;

    public static void main(String[] args) throws IOException, InterruptedException {
        long timeDirect;
        long normal;
        out.println("start");

        for (int i = 512; i <= 1024 * 1024 * 64; i *= 2) {
            buffer = allocateDirect(i);
            timeDirect = copyShortest();

            buffer = allocate(i);
            normal = copyShortest();

            out.println(format("%d, %d, %d", i, normal, timeDirect));
        }

        out.println("stop");
    }

    private static long copyShortest() throws IOException, InterruptedException {
        int result = 0;
        for (int i = 0; i < 100; i++) {
            int single = copyOnce();
            result = (i == 0) ? single : Math.min(result, single);
        }
        return result;
    }


    private static int copyOnce() throws IOException, InterruptedException {
        initialize();

        long start = System.currentTimeMillis();

        while (source.read(buffer)!= -1) {    
            buffer.flip();  
            target.write(buffer);
            buffer.clear();  //pos = 0, limit = capacity
        }

        long time = System.currentTimeMillis() - start;

        rest();

        return (int)time;
    }   


    private static void initialize() throws UnknownHostException, IOException {
        InputStream  is = new FileInputStream(new File("/Users/stu/temp/robyn.in"));//315 MB file
        OutputStream os = new FileOutputStream(new File("/dev/null"));

        target = Channels.newChannel(os);
        source = Channels.newChannel(is);
    }

    private static void rest() throws InterruptedException {
        System.gc();
        Thread.sleep(200);      
    }
}

questionAnswers(4)

yourAnswerToTheQuestion