mirror of
https://github.com/MaskRay/ccls.git
synced 2025-02-22 16:39:48 +00:00
Convert GetFilesInFolderHelper to use BFS (#268)
This function has a huge stack frame (> 8k, thanks to tinydir), and is called recursively. I have seen this function causing some stack overflows. So convert it to BFS to avoid that.
This commit is contained in:
parent
0ef5aec3fc
commit
b644b54614
12
src/utils.cc
12
src/utils.cc
@ -140,8 +140,11 @@ static void GetFilesInFolderHelper(
|
|||||||
bool recursive,
|
bool recursive,
|
||||||
std::string output_prefix,
|
std::string output_prefix,
|
||||||
const std::function<void(const std::string&)>& handler) {
|
const std::function<void(const std::string&)>& handler) {
|
||||||
|
std::queue<std::pair<std::string, std::string>> q;
|
||||||
|
q.push(make_pair(folder, output_prefix));
|
||||||
|
while(!q.empty()) {
|
||||||
tinydir_dir dir;
|
tinydir_dir dir;
|
||||||
if (tinydir_open(&dir, folder.c_str()) == -1) {
|
if (tinydir_open(&dir, q.front().first.c_str()) == -1) {
|
||||||
LOG_S(WARNING) << "Unable to open directory " << folder;
|
LOG_S(WARNING) << "Unable to open directory " << folder;
|
||||||
goto bail;
|
goto bail;
|
||||||
}
|
}
|
||||||
@ -166,11 +169,10 @@ static void GetFilesInFolderHelper(
|
|||||||
if (recursive) {
|
if (recursive) {
|
||||||
std::string child_dir = output_prefix + file.name + "/";
|
std::string child_dir = output_prefix + file.name + "/";
|
||||||
if (!IsSymLink(child_dir))
|
if (!IsSymLink(child_dir))
|
||||||
GetFilesInFolderHelper(file.path, true /*recursive*/, child_dir,
|
q.push(make_pair(file.path, child_dir));
|
||||||
handler);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
handler(output_prefix + file.name);
|
handler(q.front().second + file.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,6 +185,8 @@ static void GetFilesInFolderHelper(
|
|||||||
|
|
||||||
bail:
|
bail:
|
||||||
tinydir_close(&dir);
|
tinydir_close(&dir);
|
||||||
|
q.pop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> GetFilesInFolder(std::string folder,
|
std::vector<std::string> GetFilesInFolder(std::string folder,
|
||||||
|
Loading…
Reference in New Issue
Block a user