Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: less nest in filesystem iteration When CleanOldLogFiles::Run #801

Merged
merged 3 commits into from Feb 1, 2024

Conversation

fxliang
Copy link
Contributor

@fxliang fxliang commented Jan 31, 2024

Pull request

Issue tracker

Fixes will automatically close the related issue

Fixes #

Feature

Describe feature of pull request

less nest when filesystem iteration in CleanOldLogFiles::Run(Deployer* deplyer) for better performance. esp under Windows with log dir %TEMP% and too much files inside that directory.

Unit test

  • Done

Manual test

  • Done

Code Review

  1. Unit and manual test pass
  2. GitHub Action CI pass
  3. At least one contributor reviews and votes
  4. Can be merged clean without conflicts
  5. PR will be merged by rebase upstream base

Additional Info

simple test code, target iteration folder ./files with 1 million files. compiled with command
g++ -Wall iterator.cpp -std=c++17 -o iterator
image

#include <chrono>
#include <iostream>
#include <filesystem>
#include <vector>

using namespace std;
namespace fs = std::filesystem;

int main()
{
    auto start = chrono::high_resolution_clock::now();
    vector<string> dirs;
    // with 1 million files inside ./files
    dirs.push_back("./files");
    // ----------------------------------------------------------------
    vector<string> files;
    for (const auto& dir : dirs) {
        for (const auto& entry : fs::directory_iterator(dir)){
            if(entry.is_regular_file() && !entry.is_symlink())
                files.push_back(entry.path().string());
        }
    }
    for (const auto& file: files) {
    }
    vector<string>().swap(files);
    auto _end = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::microseconds>(_end - start);
    cout << "with new process, duration: " << duration.count() << " us" << endl;

    // ----------------------------------------------------------------
    start = chrono::high_resolution_clock::now();
    for (auto i = dirs.cbegin(); i != dirs.cend(); ++i) {
        for (fs::directory_iterator j(*i), end; j != end; ++j) {
            fs::path entry(j->path());
            string file_name(entry.filename().string());
            if (fs::is_regular_file(entry) && !fs::is_symlink(entry));
        }
    }
    _end = chrono::high_resolution_clock::now();
    duration = chrono::duration_cast<chrono::microseconds>(_end - start);
    cout << "with old process, duration: " << duration.count() << " us" << endl;
    return 0;
}

src/rime/lever/deployment_tasks.cc Outdated Show resolved Hide resolved
@eagleoflqj eagleoflqj merged commit 9ec1711 into rime:master Feb 1, 2024
8 checks passed
fxliang added a commit to fxliang/librime that referenced this pull request Feb 2, 2024
@fxliang fxliang deleted the iterator_perf branch February 2, 2024 05:04
graphemecluster pushed a commit to TypeDuck-HK/librime that referenced this pull request Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants