gpt4 book ai didi

c++ - libgit : Fastest way of fetching files in a commit

转载 作者:行者123 更新时间:2023-11-30 05:34:36 28 4
gpt4 key购买 nike

我需要遍历存储库的提交并为每次提交获取受影响的文件。这是我目前巨大的性能瓶颈。

我有一个 libgit 函数的 C++ 包装器,但这段代码应该足够容易理解。

std::vector<std::string> Commit::getAffectedFiles() const {
git_tree* tree = nullptr;
git_tree* tree2 = nullptr;
int error = git_commit_tree(&tree, get());
throw_on_error(error);

try {
error = git_commit_tree(&tree2, parent(0).get());
} catch (GitException e) {
tree2 = nullptr; // probably initial commit
}
git_diff* diff = nullptr;
git_diff_tree_to_tree(&diff, getRepo(), tree2, tree, 0);

std::vector<std::string> ret;
git_diff_foreach(diff,
[](const git_diff_delta* entry, float progress, void* payload) {
std::string str = entry->old_file.path;
((std::vector<std::string>*)payload)->push_back(str);
return 0;
}, nullptr, nullptr, nullptr, &ret);
git_tree_free(tree);
git_tree_free(tree2);
git_diff_free(diff);
return ret;
}

我只能希望我在这里做了一些根本性的错误。


例如

git log --stat > /dev/null

速度更快,并且提供相同的信息。


perf 按顺序报告 git__strncmpgit_buf_rfind_nextgit_tree__parse 的大部分使用情况。


我知道这是 IO 繁重的,但我没有看到减少它或并行运行它的简单方法。

最佳答案

这相当于 git 内部所做的,尽管 git 本身有更多的人关注它的性能,而 libgit2 在这方面的投资几乎没有那么多。

然而,一些补丁[0] 最近被 merge 到 libgit2 的 master 分支中,这可以减少多达 40% 的树解析时间。我建议尝试一下,看看你得到了什么数字(这些补丁也应该很容易向后移植到早期版本)。

还要考虑到你的 git 版本很可能是在 Release模式下编译的,而 libgit2 默认情况下是在 Debug模式下构建的,所以如果你还没有激活 Release模式,请使用 -DCMAKE_BUILD_TYPE=Release。这也显着加快了这些解析操作。

[0] 特别公关 3508和提交 0174f2fc4364

关于c++ - libgit : Fastest way of fetching files in a commit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34157047/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com