Skip to content

Commit

Permalink
filebrowser: Improve performance a lot (speed of file list appearing)
Browse files Browse the repository at this point in the history
Probably improvement for #1120 #1176
  • Loading branch information
gsantner committed Sep 9, 2021
1 parent aa5b543 commit 83a4e2f
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.gsantner.markor.R;
import net.gsantner.opoc.util.ContextUtils;
import net.gsantner.opoc.util.FileUtils;
import net.gsantner.opoc.util.FileWithCachedData;

import java.io.File;
import java.io.FilenameFilter;
Expand Down Expand Up @@ -468,6 +469,8 @@ public void loadFolder(final File folder) {
@Override
public void run() {
synchronized (LOAD_FOLDER_SYNC_OBJECT) {
ArrayList<File> oldAdapterData = new ArrayList<>();
Collections.copy(_adapterData, oldAdapterData);
_currentFolder = folder;
_adapterData.clear();
_virtualMapping.clear();
Expand Down Expand Up @@ -546,6 +549,15 @@ public void run() {
}
}

// Optimization - convert found File's to FileWithCachedData
// Sorting invokes a lot of filesystem i/o calls which do consume much time
// Changing sort order: Reuse information if available
for (int i = 0; i < _adapterData.size(); i++) {
final File o = _adapterData.remove(i);
final int at = oldAdapterData.indexOf(o);
_adapterData.add(i, at >= 0 ? oldAdapterData.get(at) : new FileWithCachedData(o));
}

try {
Collections.sort(_adapterData, FilesystemViewerAdapter.this);
} catch (IllegalArgumentException ignored) {
Expand Down
142 changes: 142 additions & 0 deletions app/src/main/java/net/gsantner/opoc/util/FileWithCachedData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// License of this file FileWithCachedData.java: Public Domain
// Created by Gregor Santner, 2021 - https://gsantner.net
//

package net.gsantner.opoc.util;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.io.File;
import java.net.URI;

@SuppressWarnings({"unused", "RedundantSuppression"})
public class FileWithCachedData extends File {
private Integer cHashCode;
private Boolean cCanRead, cCanWrite, cExists, cIsAbsolute, cIsDirectory, cIsFile;
private Long cLastModified, cLength;
private String cAbsolutePath;
private File cAbsoluteFile;
private String[] cList;

public FileWithCachedData(@NonNull String pathname) {
super(pathname);
}

public FileWithCachedData(@Nullable String parent, @NonNull String child) {
super(parent, child);
}

public FileWithCachedData(@Nullable File parent, @NonNull String child) {
super(parent, child);
}

public FileWithCachedData(@NonNull URI uri) {
super(uri);
}

public FileWithCachedData(@NonNull File f) {
super(f.getPath());
}


@NonNull
@Override
public synchronized String getAbsolutePath() {
if (cAbsolutePath == null) {
cAbsolutePath = super.getAbsolutePath();
}
return cAbsolutePath;
}

@NonNull
@Override
public File getAbsoluteFile() {
if (cAbsoluteFile == null) {
cAbsoluteFile = super.getAbsoluteFile();
}
return cAbsoluteFile;
}

@Override
public boolean isAbsolute() {
if (cIsAbsolute == null) {
cIsAbsolute = super.isAbsolute();
}
return cIsAbsolute;
}

@Override
public boolean canRead() {
if (cCanRead == null) {
cCanRead = super.canRead();
}
return cCanRead;
}

@Override
public boolean canWrite() {
if (cCanWrite == null) {
cCanWrite = super.canWrite();
}
return cCanWrite;
}

@Override
public boolean exists() {
if (cExists == null) {
cExists = super.exists();
}
return cExists;
}

@Override
public boolean isDirectory() {
if (cIsDirectory == null) {
cIsDirectory = super.isDirectory();
}
return cIsDirectory;
}

@Override
public boolean isFile() {
if (cIsFile == null) {
cIsFile = super.isFile();
}
return cIsFile;
}

@Override
public long lastModified() {
if (cLastModified == null) {
cLastModified = super.lastModified();
}
return cLastModified;
}

@Override
public long length() {
if (cLength == null) {
cLength = super.length();
}
return cLength;
}

@Nullable
@Override
public String[] list() {
if (cList == null) {
cList = super.list();
}
return cList;
}

@Override
public int hashCode() {
if (cHashCode == null) {
cHashCode = super.hashCode();
}
return cHashCode;
}
}

3 comments on commit 83a4e2f

@badrow
Copy link

@badrow badrow commented on 83a4e2f Sep 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, do you know when this will be released please ?
Thank you very much for your work !!

@gsantner
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1-4 weeks.

@badrow
Copy link

@badrow badrow commented on 83a4e2f Sep 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay thanks. I will try to build it myself. Good training to know how to build an android app.

Please sign in to comment.