Skip to content

Commit

Permalink
Merge pull request #16190 from unknownbrackets/save-slow
Browse files Browse the repository at this point in the history
Reduce IO primarily during save operations
  • Loading branch information
hrydgard authored Oct 9, 2022
2 parents af995f2 + db5011d commit bc9215c
Show file tree
Hide file tree
Showing 24 changed files with 387 additions and 271 deletions.
12 changes: 9 additions & 3 deletions Common/File/AndroidStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ bool Android_FileExists(const std::string &fileUri) {
return exists;
}

std::vector<File::FileInfo> Android_ListContentUri(const std::string &path) {
std::vector<File::FileInfo> Android_ListContentUri(const std::string &path, bool *exists) {
if (!g_nativeActivity) {
*exists = false;
return std::vector<File::FileInfo>();
}
auto env = getEnv();
*exists = true;

double start = time_now_d();

Expand All @@ -224,8 +226,12 @@ std::vector<File::FileInfo> Android_ListContentUri(const std::string &path) {
jstring str = (jstring)env->GetObjectArrayElement(fileList, i);
const char *charArray = env->GetStringUTFChars(str, 0);
if (charArray) { // paranoia
std::string line = charArray;
File::FileInfo info;
if (ParseFileInfo(std::string(charArray), &info)) {
if (line == "X") {
// Indicates an exception thrown, path doesn't exist.
*exists = false;
} else if (ParseFileInfo(line, &info)) {
// We can just reconstruct the URI.
info.fullName = Path(path) / info.name;
items.push_back(info);
Expand All @@ -238,7 +244,7 @@ std::vector<File::FileInfo> Android_ListContentUri(const std::string &path) {

double elapsed = time_now_d() - start;
if (elapsed > 0.1) {
INFO_LOG(FILESYS, "Listing directory on content URI took %0.3f s (%d files)", elapsed, (int)size);
INFO_LOG(FILESYS, "Listing directory on content URI took %0.3f s (%d files)", elapsed, (int)items.size());
}
return items;
}
Expand Down
5 changes: 3 additions & 2 deletions Common/File/AndroidStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int64_t Android_GetFreeSpaceByFilePath(const std::string &filePath);
bool Android_IsExternalStoragePreservedLegacy();
const char *Android_ErrorToString(StorageError error);

std::vector<File::FileInfo> Android_ListContentUri(const std::string &uri);
std::vector<File::FileInfo> Android_ListContentUri(const std::string &uri, bool *exists);

void Android_RegisterStorageCallbacks(JNIEnv * env, jobject obj);

Expand All @@ -78,7 +78,8 @@ inline int64_t Android_GetFreeSpaceByContentUri(const std::string &uri) { return
inline int64_t Android_GetFreeSpaceByFilePath(const std::string &filePath) { return -1; }
inline bool Android_IsExternalStoragePreservedLegacy() { return false; }
inline const char *Android_ErrorToString(StorageError error) { return ""; }
inline std::vector<File::FileInfo> Android_ListContentUri(const std::string &uri) {
inline std::vector<File::FileInfo> Android_ListContentUri(const std::string &uri, bool *exists) {
*exists = false;
return std::vector<File::FileInfo>();
}

Expand Down
9 changes: 5 additions & 4 deletions Common/File/DirListing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ std::vector<File::FileInfo> ApplyFilter(std::vector<File::FileInfo> files, const

bool GetFilesInDir(const Path &directory, std::vector<FileInfo> *files, const char *filter, int flags) {
if (directory.Type() == PathType::CONTENT_URI) {
std::vector<File::FileInfo> fileList = Android_ListContentUri(directory.ToString());
bool exists = false;
std::vector<File::FileInfo> fileList = Android_ListContentUri(directory.ToString(), &exists);
*files = ApplyFilter(fileList, filter);
std::sort(files->begin(), files->end());
return true;
return exists;
}

std::set<std::string> filters;
Expand Down Expand Up @@ -219,7 +220,7 @@ bool GetFilesInDir(const Path &directory, std::vector<FileInfo> *files, const ch
HANDLE hFind = FindFirstFileEx((directory.ToWString() + L"\\*").c_str(), FindExInfoStandard, &ffd, FindExSearchNameMatch, NULL, 0);
#endif
if (hFind == INVALID_HANDLE_VALUE) {
return 0;
return false;
}
do {
const std::string virtualName = ConvertWStringToUTF8(ffd.cFileName);
Expand Down Expand Up @@ -266,7 +267,7 @@ bool GetFilesInDir(const Path &directory, std::vector<FileInfo> *files, const ch
struct dirent *result = NULL;
DIR *dirp = opendir(directory.c_str());
if (!dirp)
return 0;
return false;
while ((result = readdir(dirp))) {
const std::string virtualName(result->d_name);
// check for "." and ".."
Expand Down
9 changes: 3 additions & 6 deletions Core/Dialog/PSPGamedataInstallDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,9 @@ void PSPGamedataInstallDialog::CloseCurrentFile() {
void PSPGamedataInstallDialog::WriteSfoFile() {
ParamSFOData sfoFile;
std::string sfopath = GetGameDataInstallFileName(&request, SFO_FILENAME);
PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath);
if (sfoInfo.exists) {
std::vector<u8> sfoData;
if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) {
sfoFile.ReadSFO(sfoData);
}
std::vector<u8> sfoFileData;
if (pspFileSystem.ReadEntireFile(sfopath, sfoFileData) >= 0) {
sfoFile.ReadSFO(sfoFileData);
}

// Update based on the just-saved data.
Expand Down
40 changes: 37 additions & 3 deletions Core/Dialog/PSPSaveDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ const static int SAVEDATA_DIALOG_SIZE_V1 = 1480;
const static int SAVEDATA_DIALOG_SIZE_V2 = 1500;
const static int SAVEDATA_DIALOG_SIZE_V3 = 1536;

static bool IsNotVisibleAction(SceUtilitySavedataType type) {
switch (type) {
case SCE_UTILITY_SAVEDATA_TYPE_AUTOLOAD:
case SCE_UTILITY_SAVEDATA_TYPE_AUTOSAVE:
case SCE_UTILITY_SAVEDATA_TYPE_SIZES:
case SCE_UTILITY_SAVEDATA_TYPE_LIST:
case SCE_UTILITY_SAVEDATA_TYPE_FILES:
case SCE_UTILITY_SAVEDATA_TYPE_GETSIZE:
case SCE_UTILITY_SAVEDATA_TYPE_MAKEDATASECURE:
case SCE_UTILITY_SAVEDATA_TYPE_MAKEDATA:
case SCE_UTILITY_SAVEDATA_TYPE_WRITEDATASECURE:
case SCE_UTILITY_SAVEDATA_TYPE_WRITEDATA:
case SCE_UTILITY_SAVEDATA_TYPE_READDATASECURE:
case SCE_UTILITY_SAVEDATA_TYPE_READDATA:
case SCE_UTILITY_SAVEDATA_TYPE_DELETEDATA:
case SCE_UTILITY_SAVEDATA_TYPE_AUTODELETE:
return true;

default:
break;
}
return false;
}

PSPSaveDialog::PSPSaveDialog(UtilityDialogType type) : PSPDialog(type) {
param.SetPspParam(0);
Expand Down Expand Up @@ -87,6 +110,8 @@ int PSPSaveDialog::Init(int paramAddr)
Memory::Memcpy(&request, requestAddr, size);
Memory::Memcpy(&originalRequest, requestAddr, size);

param.SetIgnoreTextures(IsNotVisibleAction((SceUtilitySavedataType)(u32)request.mode));
param.ClearCaches();
int retval = param.SetPspParam(&request);

const u32 mode = (u32)param.GetPspParam()->mode;
Expand Down Expand Up @@ -239,6 +264,7 @@ int PSPSaveDialog::Init(int paramAddr)
ChangeStatusInit(SAVEDATA_INIT_DELAY_US);
}

param.ClearCaches();
UpdateButtons();
StartFade(true);

Expand Down Expand Up @@ -345,7 +371,7 @@ void PSPSaveDialog::DisplaySaveList(bool canMove) {
PPGeImageStyle imageStyle = FadedImageStyle();
auto fileInfo = param.GetFileInfo(displayCount);

if (fileInfo.size == 0 && fileInfo.texture != NULL)
if (fileInfo.size == 0 && fileInfo.texture && fileInfo.texture->IsValid())
imageStyle.color = CalcFadedColor(0xFF777777);

// Calc save image position on screen
Expand All @@ -370,7 +396,7 @@ void PSPSaveDialog::DisplaySaveList(bool canMove) {
continue;

int pad = 0;
if (fileInfo.texture != nullptr) {
if (fileInfo.texture != nullptr && fileInfo.texture->IsValid()) {
fileInfo.texture->SetTexture();
int tw = fileInfo.texture->Width();
int th = fileInfo.texture->Height();
Expand Down Expand Up @@ -420,7 +446,7 @@ void PSPSaveDialog::DisplaySaveIcon(bool checkExists)

int tw = 256;
int th = 256;
if (curSave.texture != NULL) {
if (curSave.texture != nullptr && curSave.texture->IsValid()) {
curSave.texture->SetTexture();
tw = curSave.texture->Width();
th = curSave.texture->Height();
Expand Down Expand Up @@ -618,6 +644,7 @@ int PSPSaveDialog::Update(int animSpeed)
param.SetPspParam(&request);
}

param.ClearCaches();
UpdateButtons();
UpdateFade(animSpeed);

Expand Down Expand Up @@ -1035,11 +1062,13 @@ int PSPSaveDialog::Update(int animSpeed)

if (ReadStatus() == SCE_UTILITY_STATUS_FINISHED || pendingStatus == SCE_UTILITY_STATUS_FINISHED)
Memory::Memcpy(requestAddr, &request, request.common.size, "SaveDialogParam");
param.ClearCaches();

return 0;
}

void PSPSaveDialog::ExecuteIOAction() {
param.ClearCaches();
auto &result = param.GetPspParam()->common.result;
std::lock_guard<std::mutex> guard(paramLock);
switch (display) {
Expand Down Expand Up @@ -1078,9 +1107,11 @@ void PSPSaveDialog::ExecuteIOAction() {
}

ioThreadStatus = SAVEIO_DONE;
param.ClearCaches();
}

void PSPSaveDialog::ExecuteNotVisibleIOAction() {
param.ClearCaches();
auto &result = param.GetPspParam()->common.result;

switch ((SceUtilitySavedataType)(u32)param.GetPspParam()->mode) {
Expand Down Expand Up @@ -1161,6 +1192,8 @@ void PSPSaveDialog::ExecuteNotVisibleIOAction() {
default:
break;
}

param.ClearCaches();
}

void PSPSaveDialog::JoinIOThread() {
Expand Down Expand Up @@ -1198,6 +1231,7 @@ int PSPSaveDialog::Shutdown(bool force) {
ChangeStatusShutdown(SAVEDATA_SHUTDOWN_DELAY_US);
}
param.SetPspParam(0);
param.ClearCaches();

return 0;
}
Expand Down
Loading

0 comments on commit bc9215c

Please sign in to comment.