Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recommend using a <code>BufferedOutputStream</code> wrapping a <code>FileOutputStream</code>. I do not believe you will see <em>any</em> performance improvement by mucking with <code>ByteBuffer</code> and <code>FileChannel</code>, and that you'll be left with a lot of hard-to-maintain code if you go that route.</p> <p>The reasoning is quite simple: regardless of the approach you take, the steps involved are the same:</p> <ol> <li>Generate bytes. You don't say how you plan to do this, and it could introduce an additional level of temporary buffering into the equation. But regardless, the Java data has to be turned into bytes.</li> <li>Accumulate bytes into a buffer. You want to buffer your data before writing it, so that you're not making lots of small writes. That's a given. But where that buffer lives is immaterial.</li> <li>Move bytes from Java heap to C heap, across JNI barrier. Writing a file is a native operation, and it doesn't read directly from the Java heap. So whether you buffer on the Java heap and then move the buffered bytes, or buffer in a direct <code>ByteBuffer</code> (and yes, you want a <em>direct</em> buffer), you're still moving the bytes. You will make more JNI calls with the <code>ByteBuffer</code>, but that's a marginal cost.</li> <li>Invoke <code>fwrite</code>, a kernel call that copies bytes from the C heap into a kernel-maintained disk buffer.</li> <li>Write the kernel buffer to disk. This will outweigh all the other steps combined, because <em>disks are slow</em>.</li> </ol> <p>There may be a few microseconds gained or lost depending on exactly how you implement these steps, but you can't change the basic steps.</p> <p>The <code>FileChannel</code> does give you the option to call <code>force()</code>, to ensure that step #5 actually happens. This is likely to actually <em>decrease</em> your overall performance, as the underlying <code>fsync</code> call will not return until the bytes are written. And if you really want to do it, you can always get the channel from the underlying stream.</p> <p>Bottom line: I'm willing to bet that you're actually IO-bound, and there's no cure for that save better hardware.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload