8244936: Reduce JNI overhead of accessing FileDescriptor

Reviewed-by: rriggs, alanb
This commit is contained in:
Claes Redestad 2020-05-13 22:25:14 +02:00
parent ad2afe0bf4
commit 168cdcf65d
8 changed files with 66 additions and 45 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -92,6 +92,14 @@ handleOpen(const char *path, int oflag, int mode) {
return fd;
}
FD getFD(JNIEnv *env, jobject obj, jfieldID fid) {
jobject fdo = (*env)->GetObjectField(env, obj, fid);
if (fdo == NULL) {
return -1;
}
return (*env)->GetIntField(env, fdo, IO_fd_fdID);
}
void
fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
{
@ -108,10 +116,10 @@ fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
if (fd != -1) {
jobject fdobj;
jboolean append;
SET_FD(this, fd, fid);
fdobj = (*env)->GetObjectField(env, this, fid);
if (fdobj != NULL) {
// Set FD
(*env)->SetIntField(env, fdobj, IO_fd_fdID, fd);
append = (flags & O_APPEND) == 0 ? JNI_FALSE : JNI_TRUE;
(*env)->SetBooleanField(env, fdobj, IO_append_fdID, append);
}

View file

@ -43,20 +43,12 @@ jlong handleGetLength(FD fd);
FD handleOpen(const char *path, int oflag, int mode);
/*
* Macros to set/get fd from the java.io.FileDescriptor. These
* macros rely on having an appropriately defined 'this' object
* within the scope in which they're used.
* If GetObjectField returns null, SET_FD will stop and GET_FD
* will simply return -1 to avoid crashing VM.
* Functions to get fd from the java.io.FileDescriptor field
* of an object. These functions rely on having an appropriately
* defined object with a FileDescriptor object at the fid offset.
* If the FD object is null, return -1 to avoid crashing VM.
*/
#define SET_FD(this, fd, fid) \
if ((*env)->GetObjectField(env, (this), (fid)) != NULL) \
(*env)->SetIntField(env, (*env)->GetObjectField(env, (this), (fid)),IO_fd_fdID, (fd))
#define GET_FD(this, fid) \
(*env)->GetObjectField(env, (this), (fid)) == NULL ? \
-1 : (*env)->GetIntField(env, (*env)->GetObjectField(env, (this), (fid)), IO_fd_fdID)
FD getFD(JNIEnv *env, jobject cur, jfieldID fid);
/*
* Macros to set/get fd when inside java.io.FileDescriptor