From e0067df491dbd95697c85f602beaa89e81fcda61 Mon Sep 17 00:00:00 2001 From: crudelios Date: Thu, 28 May 2026 15:12:20 +0100 Subject: [PATCH] Slightly improve performance by changing JNI method calls to field retrievals --- .../main/java/org/libsdl/app/SDLActivity.java | 35 +++++++------------ src/core/android/SDL_android.c | 26 +++++++------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java index df5d3bed02..be7506c22d 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java @@ -2118,8 +2118,9 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh private boolean dirty; private final String id; private final String mimeType; - private long lastModified; - private long size; + public final boolean isDirectory; + public long lastModified; + public long size; private final Uri uri; private final Uri tree; private HashMap children; @@ -2129,6 +2130,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh this.mimeType = cursor.getString(2); this.lastModified = cursor.getLong(3); this.size = cursor.getLong(4); + this.isDirectory = this.mimeType.equals(DocumentsContract.Document.MIME_TYPE_DIR); this.tree = uri; this.uri = uri; this.dirty = false; @@ -2141,12 +2143,14 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh this.size = cursor.getLong(4); this.tree = parent.tree; this.uri = DocumentsContract.buildDocumentUriUsingTree(this.tree, this.id); + this.isDirectory = this.mimeType.equals(DocumentsContract.Document.MIME_TYPE_DIR); this.dirty = false; } private SAFDocument(Uri tree) { this.id = DocumentsContract.getTreeDocumentId(tree); this.mimeType = DocumentsContract.Document.MIME_TYPE_DIR; + this.isDirectory = true; this.tree = tree; this.uri = DocumentsContract.buildDocumentUriUsingTree(this.tree, this.id); this.dirty = true; @@ -2156,6 +2160,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh { this.id = DocumentsContract.getDocumentId(uri); this.mimeType = mimeType; + this.isDirectory = this.mimeType.equals(DocumentsContract.Document.MIME_TYPE_DIR); this.lastModified = System.currentTimeMillis(); this.tree = tree; this.uri = uri; @@ -2221,6 +2226,10 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh } } + if (document.dirty) { + document.update(); + } + return document; } @@ -2264,8 +2273,8 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh return newFileInfo; } - public HashMap getChildren() throws FileNotFoundException { - if (!this.isDirectory()) { + private HashMap getChildren() throws FileNotFoundException { + if (!this.isDirectory) { throw new FileNotFoundException(this.id + " is not a directory for uri: " + this.uri); } if (this.children != null) { @@ -2313,24 +2322,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh this.dirty = false; } - public boolean isDirectory() { - return this.mimeType.equals(DocumentsContract.Document.MIME_TYPE_DIR); - } - - public long getLastModified() { - if (this.dirty) { - this.update(); - } - return this.lastModified; - } - - public long getSize() { - if (this.dirty) { - this.update(); - } - return this.size; - } - public String getUri() { return this.uri.toString(); } diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c index 89b6673633..177229d76f 100644 --- a/src/core/android/SDL_android.c +++ b/src/core/android/SDL_android.c @@ -438,9 +438,9 @@ static jobject javaAssetManagerRef = 0; // Android content file handling static jclass mSAFDocumentClass = NULL; -static jmethodID midIsDirectory = NULL; -static jmethodID midGetLastModified = NULL; -static jmethodID midGetSize = NULL; +static jfieldID fidIsDirectory = NULL; +static jfieldID fidLastModified = NULL; +static jfieldID fidSize = NULL; static jmethodID midGetUri = NULL; static SDL_Mutex *Android_ActivityMutex = NULL; @@ -2591,7 +2591,7 @@ static char *GetURIWithNormalizedPath(const char *uri, bool should_append_slash) static bool CreateSAFDocumentClass(JNIEnv *env, jobject jSAFDocument) { - if (mSAFDocumentClass && midIsDirectory && midGetLastModified && midGetSize && midGetUri) { + if (mSAFDocumentClass && fidIsDirectory && fidLastModified && fidSize && midGetUri) { return true; } @@ -2613,22 +2613,22 @@ static bool CreateSAFDocumentClass(JNIEnv *env, jobject jSAFDocument) } } - if (!midIsDirectory) { - midIsDirectory = (*env)->GetMethodID(env, mSAFDocumentClass, "isDirectory", "()Z"); + if (!fidIsDirectory) { + fidIsDirectory = (*env)->GetFieldID(env, mSAFDocumentClass, "isDirectory", "Z"); if (Android_JNI_ExceptionOccurred(false)) { return false; } } - if (!midGetLastModified) { - midGetLastModified = (*env)->GetMethodID(env, mSAFDocumentClass, "getLastModified", "()J"); + if (!fidLastModified) { + fidLastModified = (*env)->GetFieldID(env, mSAFDocumentClass, "lastModified", "J"); if (Android_JNI_ExceptionOccurred(false)) { return false; } } - if (!midGetSize) { - midGetSize = (*env)->GetMethodID(env, mSAFDocumentClass, "getSize", "()J"); + if (!fidSize) { + fidSize = (*env)->GetFieldID(env, mSAFDocumentClass, "size", "J"); if (Android_JNI_ExceptionOccurred(false)) { return false; } @@ -2817,19 +2817,19 @@ bool Android_JNI_GetContentInfo(const char *uri, SDL_PathInfo *info) } // Get the fields from the SAFDocument object - bool is_directory = (*env)->CallBooleanMethod(env, jSAFDocument, midIsDirectory); + bool is_directory = (*env)->GetBooleanField(env, jSAFDocument, fidIsDirectory); if (Android_JNI_ExceptionOccurred(false)) { LocalReferenceHolder_Cleanup(&refs); return false; } - long last_modified = (*env)->CallLongMethod(env, jSAFDocument, midGetLastModified); + jlong last_modified = (*env)->GetLongField(env, jSAFDocument, fidLastModified); if (Android_JNI_ExceptionOccurred(false)) { LocalReferenceHolder_Cleanup(&refs); return false; } - long size = (*env)->CallLongMethod(env, jSAFDocument, midGetSize); + jlong size = (*env)->GetLongField(env, jSAFDocument, fidSize); if (Android_JNI_ExceptionOccurred(false)) { LocalReferenceHolder_Cleanup(&refs); return false;