Skip to content

perf: optimize startup time by reading scale factor from config#206

Merged
yixinshark merged 1 commit into
linuxdeepin:masterfrom
yixinshark:fix/startup-block-optimization
May 14, 2026
Merged

perf: optimize startup time by reading scale factor from config#206
yixinshark merged 1 commit into
linuxdeepin:masterfrom
yixinshark:fix/startup-block-optimization

Conversation

@yixinshark
Copy link
Copy Markdown
Contributor

@yixinshark yixinshark commented May 13, 2026

Read the scale factor from DConfig instead of using a blocking D-Bus call to XSettings1. This avoids a synchronous wait (around 600ms) during session initialization on X11.

通过读取 DConfig 配置获取缩放比例,代替原本阻塞的 D-Bus 调用。
这避免了 X11 环境下 session 初始化过程中约 600ms 的同步等待时间。

Log: optimize startup time by reading scale factor from config
Change-Id: I29dcb24d8cf1f2d5f63d5c70907c399ce0bff328

Summary by Sourcery

Optimize session startup by reading the display scale factor from persisted configuration instead of a blocking XSettings D-Bus call on X11.

Enhancements:

  • Retrieve X11 display scale factor from DConfig-backed settings rather than via synchronous D-Bus to XSettings1 to reduce startup latency.
  • Add validation and logging around the configured scale factor, defaulting to 1.0 when configuration is missing or invalid.
  • Update SPDX copyright years for the environments manager source file.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 13, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replaces a blocking D-Bus call to XSettings1 with reading a persisted scale factor from DConfig in the X11 session initialization path, adds basic validation/logging, and updates the file copyright years.

Sequence diagram for X11 scale factor retrieval during session startup

sequenceDiagram
    participant EnvironmentsManager
    participant DConfigFactory as DConfig
    participant DConfigObject as DConfigConfig

    EnvironmentsManager->>DConfigFactory: create(org.deepin.dde.daemon, org.deepin.XSettings)
    DConfigFactory-->>EnvironmentsManager: DConfigConfig
    EnvironmentsManager->>DConfigObject: isValid()
    alt [config is valid]
        EnvironmentsManager->>DConfigObject: value(scale-factor, 1.0)
        DConfigObject-->>EnvironmentsManager: scaleFactor
        EnvironmentsManager->>EnvironmentsManager: [validate scaleFactor > 0]
    else [config not valid]
        EnvironmentsManager->>EnvironmentsManager: [fallback scaleFactor = 1.0]
    end
    EnvironmentsManager->>DConfigObject: deleteLater()
Loading

File-Level Changes

Change Details Files
Use DConfig-stored scale factor instead of synchronous D-Bus XSettings1 call during X11 environment initialization to reduce startup latency.
  • Include DConfig header and use DTK_CORE_NAMESPACE::DConfig::create to open the org.deepin.XSettings config for org.deepin.dde.daemon
  • Read the "scale-factor" value from config with a default of 1.0, convert to double, and log the resulting scale factor via qInfo
  • Ensure the DConfig object is cleaned up with deleteLater when done
  • Guard against invalid or non-positive scaleFactor by resetting it to 1.0 as a fallback
  • Update SPDX copyright year range from 2021 - 2023 to 2021 - 2026
src/dde-session/environmentsmanager.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • When the DConfig lookup fails or returns the default value, consider falling back to the previous XSettings D-Bus path to preserve behavior for fresh or misconfigured environments.
  • The qInfo() log for the scale factor will run on every session startup; consider downgrading to qDebug() or adding a verbosity guard to avoid noisy logs in normal operation.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When the DConfig lookup fails or returns the default value, consider falling back to the previous XSettings D-Bus path to preserve behavior for fresh or misconfigured environments.
- The `qInfo()` log for the scale factor will run on every session startup; consider downgrading to `qDebug()` or adding a verbosity guard to avoid noisy logs in normal operation.

## Individual Comments

### Comment 1
<location path="src/dde-session/environmentsmanager.cpp" line_range="85-88" />
<code_context>
+        // 此时获取的值可能为默认值 1.0。但为了避免 D-Bus 同步调用阻塞启动流程(阻塞时间600ms左右),此处优先使用配置中的持久化值。
+        auto config = DTK_CORE_NAMESPACE::DConfig::create("org.deepin.dde.daemon", "org.deepin.XSettings");
+        if (config && config->isValid()) {
+            scaleFactor = config->value("scale-factor", 1.0).toDouble();
+            qInfo() << "scale factor: " << scaleFactor;
+        }
+
</code_context>
<issue_to_address>
**suggestion:** Reconsider using qInfo for per-session scale-factor logging.

This will log on every session startup and may add unnecessary noise to production logs. If this is mainly for debugging, consider using `qDebug` or only logging when the value changes or is invalid to avoid log spam.

```suggestion
        if (config && config->isValid()) {
            scaleFactor = config->value("scale-factor", 1.0).toDouble();
            qDebug() << "scale factor:" << scaleFactor;
        }
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/dde-session/environmentsmanager.cpp
Read the scale factor from DConfig instead of using a blocking D-Bus
call to XSettings1. This avoids a synchronous wait (around 600ms)
during session initialization on X11.

通过读取 DConfig 配置获取缩放比例,代替原本阻塞的 D-Bus 调用。
这避免了 X11 环境下 session 初始化过程中约 600ms 的同步等待时间。

Log: optimize startup time by reading scale factor from config
Change-Id: I29dcb24d8cf1f2d5f63d5c70907c399ce0bff328
@yixinshark yixinshark force-pushed the fix/startup-block-optimization branch from 3c349c7 to a8ddc4b Compare May 13, 2026 09:33
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX。我已仔细审查了你提供的 Git Diff。本次修改主要是将 X11 环境下获取缩放比例的方式从 D-Bus 同步调用 改为了 读取 DConfig 持久化配置,以避免阻塞启动流程,这是一个很好的性能优化方向。

不过,代码在语法逻辑、性能、代码质量和安全性方面还有进一步优化的空间。以下是我的详细审查意见:

1. 语法与逻辑

  • deleteLater() 的时序问题:在 Qt 的事件循环中,deleteLater() 会将销毁事件推迟到当前事件循环结束。但在某些启动流程中,如果 EnvironmentsManager 的后续代码立即依赖配置文件的释放(例如 DConfig 监听了文件变动,或者有资源锁),deleteLater() 可能会导致生命周期滞后。更推荐使用 std::unique_ptr 或直接在栈上管理对象生命周期,确保立即释放。
  • 浮点数比较逻辑if (scaleFactor < 1.0) 对于浮点数的直接比较在严格意义上是不安全的,虽然在此场景下(1.0 是个精确表示的数字)不会引发严重 Bug,但作为最佳实践,建议增加对无效值的容错处理(例如极大值、0或负数)。

2. 代码性能

  • DConfig 对象的重复创建与销毁DConfig::create 内部通常会涉及文件 I/O 和 D-Bus 通信(与配置后端服务交互)。如果 createGeneralEnvironments 会被多次调用,或者在启动阶段其他地方也需要读取同样的配置,每次都 createdeleteLater 会带来不必要的性能开销。
  • 建议:考虑将 config 作为 EnvironmentsManager 的成员变量(在构造时初始化,析构时自动释放),或者使用局部静态变量(如果配置在运行期间不会变),以复用 DConfig 实例。

3. 代码质量

  • 硬编码的魔法字符串"org.deepin.dde.daemon", "org.deepin.XSettings", "scale-factor" 这些字符串属于业务常量,直接硬编码在逻辑中不利于维护。建议将其提取为常量或宏定义。
  • qInfo() 日志输出:日志中包含了比例值,这是好的。但建议加上上下文信息,例如 qInfo() << "Read scale factor from DConfig:" << scaleFactor;,以便在复杂日志中快速定位来源。
  • 命名空间冗余DTK_CORE_NAMESPACE::DConfig 如果在当前文件作用域已经 using namespace DTK_CORE_NAMESPACE; 或者可以通过更简洁的方式引入,则不需要写全,显得臃肿。

4. 代码安全

  • DConfig 创建失败的风险:虽然代码检查了 config && config->isValid(),但如果 DConfig::create 失败(例如后端服务异常、权限不足),代码会静默地使用默认值 1.0。这对于排查问题极其不利,必须增加 else 分支的日志告警,明确告知开发者配置读取失败。
  • 类型转换安全config->value("scale-factor", 1.0).toDouble() 假设了配置文件中的值一定可以安全转为 double。如果配置文件被恶意篡改或损坏,存入了一个字符串,toDouble() 会返回 0.0,这会导致后续 scaleFactor < 1.0 的判断生效并重置为 1.0,虽然不会崩溃,但属于隐式容错,最好显式校验。

改进后的代码建议

综合以上意见,我为你重构了这部分代码:

// 建议在文件头部或类内部定义常量,避免魔法字符串
namespace {
    const QString kXSettingsConfigAppId = "org.deepin.dde.daemon";
    const QString kXSettingsConfigName = "org.deepin.XSettings";
    const QString kScaleFactorKey = "scale-factor";
}

void EnvironmentsManager::createGeneralEnvironments()
{
    // ... 前面的代码保持不变 ...
    double scaleFactor = 1.0;

    if (sessionType == "x11") {
        // FIXME: 首次启动桌面时 Xsettings可能尚未计算出推荐缩放比例(由 xsettings 服务计算),
        // 此时获取的值可能为默认值 1.0。但为了避免 D-Bus 同步调用阻塞启动流程(阻塞时间600ms左右),此处优先使用配置中的持久化值。
        
        // 建议:如果该函数可能被多次调用,将 config 提升为类成员变量以复用,避免反复 I/O
        auto config = DTK_CORE_NAMESPACE::DConfig::create(kXSettingsConfigAppId, kXSettingsConfigName);
        
        if (config && config->isValid()) {
            bool ok = false;
            double configScale = config->value(kScaleFactorKey, 1.0).toDouble(&ok);
            
            if (ok && configScale >= 1.0) {
                scaleFactor = configScale;
            } else {
                // 显式处理转换失败或非法值的情况
                qWarning() << "Invalid scale-factor value from DConfig, fallback to 1.0. Value:" << config->value(kScaleFactorKey);
                scaleFactor = 1.0;
            }
            qInfo() << "Read scale factor from DConfig:" << scaleFactor;
        } else {
            // 必须添加失败日志,否则配置系统异常时极难排查
            qWarning() << "Failed to create or validate DConfig for XSettings, using default scale factor 1.0";
        }

        // 立即释放资源,如果提升为成员变量则不需要此处手动释放
        if (config) {
            // 优先考虑直接 delete config; 如果 DConfig 的实现允许且没有挂载信号槽。
            // 如果必须使用 deleteLater,确保不会引起事件循环堆积
            config->deleteLater(); 
        }

    } else {
        // TODO: wayland环境下在这里通过xsettings获取是不合理的,此时xsettings服务还没有启动,并且也无法启动(没有DISPLAY,xwayland此时没有启动)
    }
    
    // ... 后续使用 scaleFactor 的代码 ...
}

总结

你本次修改的出发点非常好,解决了启动阻塞的痛点。主要需要关注的是异常分支的日志补充(这对后期维护至关重要),以及考虑是否需要将 DConfig 实例化过程复用以进一步提升性能。希望这些建议对你有所帮助!

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy, yixinshark

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

@yixinshark yixinshark merged commit a194c5a into linuxdeepin:master May 14, 2026
18 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