Skip to content

Commit d111072

Browse files
authored
[android lua] improve performance of lua loader (cocos2d#19234)
* [lua] improve performance of lua loader * remove cache fix
1 parent 53d8b68 commit d111072

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

cocos/platform/CCFileUtils.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ void FileUtils::purgeCachedEntries()
624624
{
625625
DECLARE_GUARD;
626626
_fullPathCache.clear();
627+
_fullPathCacheDir.clear();
627628
}
628629

629630
std::string FileUtils::getStringFromFile(const std::string& filename) const
@@ -935,6 +936,7 @@ void FileUtils::setSearchResolutionsOrder(const std::vector<std::string>& search
935936
bool existDefault = false;
936937

937938
_fullPathCache.clear();
939+
_fullPathCacheDir.clear();
938940
_searchResolutionsOrderArray.clear();
939941
for(const auto& iter : searchResolutionsOrder)
940942
{
@@ -1010,6 +1012,7 @@ void FileUtils::setDefaultResourceRootPath(const std::string& path)
10101012
if (_defaultResRootPath != path)
10111013
{
10121014
_fullPathCache.clear();
1015+
_fullPathCacheDir.clear();
10131016
_defaultResRootPath = path;
10141017
if (!_defaultResRootPath.empty() && _defaultResRootPath[_defaultResRootPath.length()-1] != '/')
10151018
{
@@ -1028,6 +1031,7 @@ void FileUtils::setSearchPaths(const std::vector<std::string>& searchPaths)
10281031
_originalSearchPaths = searchPaths;
10291032

10301033
_fullPathCache.clear();
1034+
_fullPathCacheDir.clear();
10311035
_searchPathArray.clear();
10321036

10331037
for (const auto& path : _originalSearchPaths)
@@ -1084,6 +1088,7 @@ void FileUtils::setFilenameLookupDictionary(const ValueMap& filenameLookupDict)
10841088
{
10851089
DECLARE_GUARD;
10861090
_fullPathCache.clear();
1091+
_fullPathCacheDir.clear();
10871092
_filenameLookupDict = filenameLookupDict;
10881093
}
10891094

@@ -1115,7 +1120,6 @@ std::string FileUtils::getFullPathForFilenameWithinDirectory(const std::string&
11151120
ret += '/';
11161121
}
11171122
ret += filename;
1118-
11191123
// if the file doesn't exist, return an empty string
11201124
if (!isFileExistInternal(ret)) {
11211125
ret = "";
@@ -1164,8 +1168,8 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
11641168
}
11651169

11661170
// Already Cached ?
1167-
auto cacheIter = _fullPathCache.find(dirPath);
1168-
if( cacheIter != _fullPathCache.end() )
1171+
auto cacheIter = _fullPathCacheDir.find(dirPath);
1172+
if( cacheIter != _fullPathCacheDir.end() )
11691173
{
11701174
return isDirectoryExistInternal(cacheIter->second);
11711175
}
@@ -1179,7 +1183,7 @@ bool FileUtils::isDirectoryExist(const std::string& dirPath) const
11791183
fullpath = fullPathForDirectory(searchIt + dirPath + resolutionIt);
11801184
if (isDirectoryExistInternal(fullpath))
11811185
{
1182-
_fullPathCache.emplace(dirPath, fullpath);
1186+
_fullPathCacheDir.emplace(dirPath, fullpath);
11831187
return true;
11841188
}
11851189
}

cocos/platform/android/CCFileUtils-android.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ bool FileUtilsAndroid::isDirectoryExistInternal(const std::string& dirPath) cons
213213
return false;
214214
}
215215

216-
const char* s = dirPath.c_str();
216+
std::string dirPathCopy = dirPath;
217+
if(dirPathCopy[dirPathCopy.length() - 1] == '/')
218+
{
219+
dirPathCopy.erase(dirPathCopy.length() - 1);
220+
}
221+
222+
const char* s = dirPathCopy.c_str();
217223

218224
// find absolute path in flash memory
219225
if (s[0] == '/')
@@ -317,6 +323,11 @@ std::vector<std::string> FileUtilsAndroid::listFiles(const std::string& dirPath)
317323
return fileList;
318324
}
319325

326+
if(relativePath[relativePath.length() - 1] == '/')
327+
{
328+
relativePath.erase(relativePath.length() - 1);
329+
}
330+
320331
auto *dir = AAssetManager_openDir(assetmanager, relativePath.c_str());
321332
if(nullptr == dir) {
322333
LOGD("... FileUtilsAndroid::failed to open dir %s", relativePath.c_str());

cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,23 @@ extern "C"
9696
pos = prefix.find_first_of("?", pos + filename.length() + 1);
9797
}
9898
chunkName = prefix + BYTECODE_FILE_EXT;
99-
if (utils->isFileExist(chunkName) && !utils->isDirectoryExist(chunkName))
99+
if (utils->isFileExist(chunkName)) // && !utils->isDirectoryExist(chunkName))
100100
{
101101
chunk = utils->getDataFromFile(chunkName);
102102
break;
103103
}
104104
else
105105
{
106106
chunkName = prefix + NOT_BYTECODE_FILE_EXT;
107-
if (utils->isFileExist(chunkName) && !utils->isDirectoryExist(chunkName))
107+
if (utils->isFileExist(chunkName) ) //&& !utils->isDirectoryExist(chunkName))
108108
{
109109
chunk = utils->getDataFromFile(chunkName);
110110
break;
111111
}
112112
else
113113
{
114114
chunkName = prefix;
115-
if (utils->isFileExist(chunkName) && !utils->isDirectoryExist(chunkName))
115+
if (utils->isFileExist(chunkName)) // && !utils->isDirectoryExist(chunkName))
116116
{
117117
chunk = utils->getDataFromFile(chunkName);
118118
break;

tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ FileUtilsTests::FileUtilsTests()
4848
ADD_TEST_CASE(TestWriteStringAsync);
4949
ADD_TEST_CASE(TestWriteDataAsync);
5050
ADD_TEST_CASE(TestListFiles);
51+
ADD_TEST_CASE(TestIsFileExistRejectFolder);
5152
}
5253

5354
// TestResolutionDirectories
@@ -1429,3 +1430,43 @@ std::string TestListFiles::subtitle() const
14291430
{
14301431
return "";
14311432
}
1433+
1434+
1435+
1436+
void TestIsFileExistRejectFolder::onEnter()
1437+
{
1438+
FileUtilsDemo::onEnter();
1439+
1440+
auto winSize = Director::getInstance()->getWinSize();
1441+
1442+
auto infoLabel = Label::createWithTTF("tests folder 'NavMesh/maps', expect to be false", "fonts/Thonburi.ttf", 18);
1443+
this->addChild(infoLabel);
1444+
infoLabel->setPosition(winSize.width / 2, winSize.height * 3 / 4);
1445+
1446+
auto cntLabel = Label::createWithTTF("waiting...", "fonts/Thonburi.ttf", 18);
1447+
this->addChild(cntLabel);
1448+
cntLabel->setPosition(winSize.width / 2, winSize.height / 3);
1449+
1450+
auto exists = FileUtils::getInstance()->isFileExist("NavMesh/maps");
1451+
auto isDirectory = FileUtils::getInstance()->isDirectoryExist("NavMesh/maps");
1452+
1453+
char cntBuffer[200] = { 0 };
1454+
snprintf(cntBuffer, 200, "isDir: %s, isFile: %s, %s", isDirectory ? "true": "false" , exists ? "true" : "false", exists ? "failure!" : "ok!" );
1455+
cntLabel->setString(cntBuffer);
1456+
1457+
}
1458+
1459+
void TestIsFileExistRejectFolder::onExit()
1460+
{
1461+
FileUtilsDemo::onExit();
1462+
}
1463+
1464+
std::string TestIsFileExistRejectFolder::title() const
1465+
{
1466+
return "FileUtils: isFileExist(direname)";
1467+
}
1468+
1469+
std::string TestIsFileExistRejectFolder::subtitle() const
1470+
{
1471+
return "";
1472+
}

tests/cpp-tests/Classes/FileUtilsTest/FileUtilsTest.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,15 @@ class TestListFiles : public FileUtilsDemo
269269
virtual std::string subtitle() const override;
270270
};
271271

272+
class TestIsFileExistRejectFolder : public FileUtilsDemo
273+
{
274+
public:
275+
CREATE_FUNC(TestIsFileExistRejectFolder);
276+
277+
virtual void onEnter() override;
278+
virtual void onExit() override;
279+
virtual std::string title() const override;
280+
virtual std::string subtitle() const override;
281+
};
282+
272283
#endif /* __FILEUTILSTEST_H__ */

0 commit comments

Comments
 (0)