Minor performance enhancement on FileInputStream read()

Alan Bateman Alan.Bateman at oracle.com
Tue Mar 29 12:32:54 UTC 2011

Jing LV wrote:
> Hello,
> I am reading the code in FileInputStream.read(), I find the current API was:
> Java_java_io_RandomAccessFile_read(JNIEnv *env, jobject this)
> readSingle(JNIEnv *env, jobject this, jfieldID fid)
> It brings a object down into native and as we know reflection costs a
> lot. I am wondering if we can improve this.
> I see the real pointer is stored in class FileDescriptor, a quick
> solution may be:
> 1. add a method read() in FileDescriptor, and it delgate to a native
> method read(fd), fd is the real pointer stored
> 2. modify readSingle(JNIEnv *env, jobject this, jfieldID fid) to
> readSingle(JNIEnv *env, jobject this, jlong fd)
> 3. add Java_java_io_FileDescriptor_read(JNIEnv *env, jobject this, jlong
> fd) in FileDescriptor_md.c, which calls readSingle
> As FileDescriptor is used in Socket as well, we may add similar
> approach, i.e, readSocket() etc. And this approach may be available for
> write and other operations.
> Any comments?
I think it would be okay to pass the java.io.FileDescriptor to the 
native methods and this would avoid the GetObjectField. However, I would 
have concerns about passing down the fd directly, mostly because it 
widens the window for issues caused by asynchronous close (a problematic 
area for java.io).


More information about the core-libs-dev mailing list