Skip to content

perf: cache statx results in DFileInfoPrivate::attributesBySelf#325

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
liyigang1:master-fix
Jun 25, 2026
Merged

perf: cache statx results in DFileInfoPrivate::attributesBySelf#325
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
liyigang1:master-fix

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

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码实现了statx系统调用结果的缓存机制,消除了严重的重复系统调用开销
逻辑正确且重构彻底,无任何安全漏洞与性能缺陷,符合满分标准

■ 【详细分析】

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

ensureStatxCached函数中的缓存状态判断与statx调用逻辑严密,mutable关键字的使用正确保证了在const成员函数中修改缓存状态的合法性。url.path().toStdString().data()产生的临时std::string对象生命周期在完整表达式结束时销毁,传给statx系统调用是安全的,不存在悬空指针风险。

  • 2.代码质量(优秀)✓

成功将attributesBySelf函数中6个时间属性分支里高度重复的10余行代码提取为独立的ensureStatxCached方法,极大提升了代码可读性与可维护性。新增的statxBuf、statxCached、statxValid等成员变量命名规范且注释清晰。

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

彻底解决了原代码在连续获取多个时间属性时重复执行statx系统调用导致的严重内核态开销问题。通过引入内存级布尔标记与结构体缓存,将最坏情况下的6次系统调用降低为严格的1次,性能提升显著。

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

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码未引入任何新的安全风险,系统调用参数由Qt的QUrl::path()严格生成,不存在路径遍历或命令注入等攻击面。

■ 【改进建议代码示例】

// 当前代码已非常优秀,无需额外修改,此处仅展示保持当前实现的参考结构
bool DFileInfoPrivate::ensureStatxCached() const
{
    if (statxCached)
        return statxValid;

    statxCached = true;

    const QUrl &url = q->uri();
    if (!url.isLocalFile())
        return false;

    unsigned mask = STATX_BASIC_STATS | STATX_BTIME;
    int ret = statx(AT_FDCWD, url.path().toStdString().data(),
                    AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT, mask, &statxBuf);
    statxValid = (ret == 0);
    return statxValid;
}

@liyigang1 liyigang1 changed the title fix: prevent BOM character loss in path concatenation perf(dfm-io): cache statx results in DFileInfoPrivate::attributesBySelf Jun 25, 2026
@liyigang1 liyigang1 changed the title perf(dfm-io): cache statx results in DFileInfoPrivate::attributesBySelf perf: cache statx results in DFileInfoPrivate::attributesBySelf Jun 25, 2026
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs, 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

@Johnson-zs

Copy link
Copy Markdown
Contributor

/merge

@deepin-bot deepin-bot Bot merged commit 25702d2 into linuxdeepin:master Jun 25, 2026
23 checks passed
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.

3 participants