code: private static void runCopy(final long start, final long len, final int keySize, final int batchSize, final long otmp[][], final byte xtmp[][], final long o[][], final byte x[][]) {
      // now copy _otmp and _xtmp back over _o and _x from the start position, allowing for boundaries
      // _o, _x, _otmp and _xtmp all have the same _batchsize
      // Would be really nice if Java had 64bit indexing to save programmer time.
      long numRowsToCopy = len;
      int sourceBatch = 0, sourceOffset = 0;
      int targetBatch = (int)(start / batchSize), targetOffset = (int)(start % batchSize);
      int targetBatchRemaining = batchSize - targetOffset;  // 'remaining' means of the the full batch, not of the numRowsToCopy
      int sourceBatchRemaining = batchSize - sourceOffset;  // at most batchSize remaining.  No need to actually put the number of rows left in here
      while (numRowsToCopy > 0) {   // TO DO: put this into class as well, to ArrayCopy into batched
        final int thisCopy = (int)Math.min(numRowsToCopy, Math.min(sourceBatchRemaining, targetBatchRemaining));
        System.arraycopy(otmp[sourceBatch], sourceOffset,         o[targetBatch], targetOffset,         thisCopy);
        System.arraycopy(xtmp[sourceBatch], sourceOffset*keySize, x[targetBatch], targetOffset*keySize, thisCopy*keySize);
        numRowsToCopy -= thisCopy;
        sourceOffset += thisCopy; sourceBatchRemaining -= thisCopy;
        targetOffset += thisCopy; targetBatchRemaining -= thisCopy;
        if (sourceBatchRemaining == 0) { sourceBatch++; sourceOffset = 0; sourceBatchRemaining = batchSize; }
        if (targetBatchRemaining == 0) { targetBatch++; targetOffset = 0; targetBatchRemaining = batchSize; }
        // 'source' and 'target' deliberately the same length variable names and long lines deliberately used so we
        // can easy match them up vertically to ensure they are the same
      }
    }
docs: Hot loop, pulled out from the main run code