From eae9ff15c72fec0485917ceb5b08b78cd6171883 Mon Sep 17 00:00:00 2001 From: Dariusz Olszewski Date: Sat, 7 Jan 2023 23:33:31 +0100 Subject: [PATCH] Avoid JSON deserialization in common, trivial cases Signed-off-by: Dariusz Olszewski --- .../datamodel/FileDataStorageManager.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 4321696775..ae00de742a 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -88,7 +88,8 @@ public class FileDataStorageManager { private static final String EXCEPTION_MSG = "Exception in batch of operations "; public static final int ROOT_PARENT_ID = 0; - public static final String NULL_STRING = "null"; + private static final String JSON_NULL_STRING = "null"; + private static final String JSON_EMPTY_ARRAY = "[]"; private final ContentResolver contentResolver; private final ContentProviderClient contentProviderClient; @@ -931,7 +932,10 @@ public class FileDataStorageManager { ocFile.setLockToken(fileEntity.getLockToken()); String sharees = fileEntity.getSharees(); - if (sharees == null || NULL_STRING.equals(sharees) || sharees.isEmpty()) { + // Surprisingly JSON deserialization causes significant overhead. + // Avoid it in common, trivial cases (null/empty). + if (sharees == null || sharees.isEmpty() || + JSON_NULL_STRING.equals(sharees) || JSON_EMPTY_ARRAY.equals(sharees)) { ocFile.setSharees(new ArrayList<>()); } else { try { @@ -944,11 +948,19 @@ public class FileDataStorageManager { } String metadataSize = fileEntity.getMetadataSize(); - ImageDimension imageDimension = gson.fromJson(metadataSize, ImageDimension.class); - if (imageDimension != null) { - ocFile.setImageDimension(imageDimension); + // Surprisingly JSON deserialization causes significant overhead. + // Avoid it in common, trivial cases (null/empty). + if (!(metadataSize == null || metadataSize.isEmpty() || JSON_NULL_STRING.equals(metadataSize))) { + ImageDimension imageDimension = gson.fromJson(metadataSize, ImageDimension.class); + if (imageDimension != null) { + ocFile.setImageDimension(imageDimension); + } } +// Log_OC.d(TAG, "createFileInstance - fileName: " + fileEntity.getPathDecrypted() + +// " sharees: -->" + (sharees == null ? "" : sharees) + "<--" + +// " metadataSize: -->" + (metadataSize == null ? "" : metadataSize) + "<--"); + return ocFile; }