Skip to content

perf(dfm-io): cache statx results in DFileInfoPrivate::attributesBySelf#324

Open
liyigang1 wants to merge 1 commit into
linuxdeepin:develop/meagle-20260526from
liyigang1:v20/20260526
Open

perf(dfm-io): cache statx results in DFileInfoPrivate::attributesBySelf#324
liyigang1 wants to merge 1 commit into
linuxdeepin:develop/meagle-20260526from
liyigang1:v20/20260526

Conversation

@liyigang1

Copy link
Copy Markdown
Contributor

Introduce ensureStatxCached() to consolidate six independent statx syscalls into one. Cache the result in mutable member fields so that subsequent time-attribute queries reuse the same buffer.

在 attributesBySelf 中引入 ensureStatxCached() 方法,将 6 个时间 属性的重复 statx 系统调用合并为一次,结果缓存后各 case 直接读取。

Log: 优化 attributesBySelf 中 statx 调用,消除重复系统调用
Task: https://pms.uniontech.com/task-view-391003.html Influence: 查询 kTimeCreated/Modified/Access 等属性时,statx 从最多 6 次降为 1 次,减少不必要的系统调用开销。

Introduce ensureStatxCached() to consolidate six independent statx
syscalls into one. Cache the result in mutable member fields so that
subsequent time-attribute queries reuse the same buffer.

在 attributesBySelf 中引入 ensureStatxCached() 方法,将 6 个时间
属性的重复 statx 系统调用合并为一次,结果缓存后各 case 直接读取。

Log: 优化 attributesBySelf 中 statx 调用,消除重复系统调用
Task: https://pms.uniontech.com/task-view-391003.html
Influence: 查询 kTimeCreated/Modified/Access 等属性时,statx 从最多
6 次降为 1 次,减少不必要的系统调用开销。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @liyigang1, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: liyigang1

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码通过引入缓存机制彻底消除了冗余的系统调用,重构效果显著且逻辑严谨
语法、质量、性能、安全四个维度均无扣分项,属于高质量的重构提交

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

重构后的 ensureStatxCached 函数正确使用了 mutable 关键字以允许在 const 成员函数中修改缓存状态,标志位 statxCachedstatxValid 的状态机转换逻辑严密,无死锁或未定义行为风险。attributesBySelf 中的 6 个时间属性分支均正确调用缓存函数并提取对应字段。
建议:无需修改

  • 2.代码质量(良好)✓

成功将原本分散在 6 处、总计约 60 行的高度重复代码提取为单一的 ensureStatxCached 方法,极大提升了可维护性。移除了原本不严谨的 ctime 降级逻辑,使得时间属性的获取语义更加精确。[[nodiscard]] 属性的添加强化了返回值的检查约束。
建议:虽然将 #include <sys/stat.h> 移至头文件是由于需要定义 struct statx 成员变量的必要之举,但未来可考虑使用 Pimpl 模式进一步隔离系统头文件依赖,当前做法可接受

  • 3.代码性能(高效)✓

彻底解决了同一文件被重复调用最多 6 次 statx 系统调用的严重性能损耗问题。通过引入内存级的布尔标志位判断,将系统调用开销从 O(N) 降至 O(1),在批量文件属性查询场景下会有显著的性能提升。
建议:无需修改

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码未引入新的安全风险。url.path().toStdString().data() 传递给 statx 是安全的,toStdString() 会生成包含 null 终止符的独立内存副本,避免了悬空指针或缓冲区溢出风险。statxBuf 作为类成员变量由内核安全写入,不存在越界写可能。
建议:无需修改

■ 【改进建议代码示例】

--- a/src/dfm-io/dfm-io/dfileinfo.cpp
+++ b/src/dfm-io/dfm-io/dfileinfo.cpp
@@ -287,7 +287,8 @@ bool DFileInfoPrivate::ensureStatxCached() const
     if (!url.isLocalFile())
         return false;
 
-    int ret = statx(AT_FDCWD, url.path().toStdString().data(),
+    // 优化安全与性能:使用 toUtf8().constData() 避免因 toStdString() 触发不必要的堆内存分配与潜在的数据深拷贝开销
+    int ret = statx(AT_FDCWD, url.path().toUtf8().constData(),
                     AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT, mask, &statxBuf);
     statxValid = (ret == 0);
     return statxValid;

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.

2 participants