19 points · by Tulik · 9y ago · 10 comments
Quote (December 2016): "I have a plan to implement this, but it requires changes to the Java Memory Model and to the compilers. I didn't get it done in time for JDK 9 because of being distracted by a ton of other things, but I hope I'll get it done by JDK 10."
* Netty's: https://netty.io/4.0/api/io/netty/buffer/ByteBuf.html as used by Netty
* Agrona's, as used by Aeron and SBE: http://insightfullogic.com/2015/Apr/18/agronas-threadsafe-of...
byte[] buf=new byte[4096]; // this matters
while(true) {
int num=in.read(buf,0,buf.length);
...
}Also MemMapped files in Java have an int index so you can't really use them for large files without using another method as well.
Finally typically one has to parse the files to a string of a certain charset. This poses still more issues in terms of how you read files; esp multi and variable byte charsets.
Furthermore, in what situations is mmap() (or MappedByteBuffer, if the situation is Java-specific) worse than read() et al?
I ask because Java is on my "I should probably know at least a little bit" list and I'm interested to pick up all the tidbits and tricks that I can - and this article screams "just use MappedByteBuffer everywhere you need I/O!", so I want to know if there are any exceptions.
"Memory mapped files are new feature in Java nio package"
They were introduced in Java 1.4, 15 years ago.
Also, I'm genuinely curious about what you mean by "properly used read()". Could you give an example?
The most common signals you get in this situation are SIGSEGV and SIGBUS. AFAIK you can't catch it in Java.
You can get these errors in circumstances that don't involve physical IO device errors, you can also get them in cases where the OS decides you can't get to that file (eg another process truncates it, or you run out of space writing to a sparse file, etc).
By proper use of read() i meant being performance conscious. read() will usually have to copy the data into your buffer, whereas mmap() can just give you a virtual-memory assisted view of the kernel buffer cache. To eliminate this as a source of slowdown, you should size the read buffer so that it fits in CPU cache but still results enough work per read() call that the system call overhead doesn't get you. Around 128k is often close to the sweet spot.