Conscrypt class initialization performs resource I/O, adding 50 ms–3 s to app startup

未关闭
#1,571 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
半天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java

调研方向

Read common/src/main/java/org/conscrypt/Conscrypt.java and the build script that currently generates conscrypt.properties; identify where version constants can be generated at build time. Update the version-loading path and verify that Conscrypt.version() still returns the expected values and that class initialization performs no resource I/O. The issue provides a fork commit for comparison, but it also states that external pull requests are not currently accepted.

由索引模型根据 Issue 内容生成。

描述

Summary

org.conscrypt.Conscrypt reads conscrypt.properties via
Class.getResourceAsStream() in its static initializer. Because this runs
during class initialization, the resource lookup and property parsing happen
on whichever thread first initializes Conscrypt — in Android apps this is
often the UI thread during provider setup.

Impact (measured in our app)
  • Typical phones: about +50 ms cold app startup.
  • Low-end TV boxes: about +2–3 s cold app startup.
  • The cost is paid unconditionally, even when Conscrypt.version() is never
    called.

Measured on release builds using cold start (adb shell am start -W, averaged
over several runs). Removing the lookup removes the delta. We can share
traces/methodology if useful.

Root cause

common/src/main/java/org/conscrypt/Conscrypt.java:

private static final Version VERSION;

static {
    int major = -1;
    int minor = -1;
    int patch = -1;
    InputStream stream = null;
    try {
        stream = Conscrypt.class.getResourceAsStream("conscrypt.properties");
        if (stream != null) {
            Properties props = new Properties();
            props.load(stream);
            major = Integer.parseInt(props.getProperty("org.conscrypt.version.major", "-1"));
            minor = Integer.parseInt(props.getProperty("org.conscrypt.version.minor", "-1"));
            patch = Integer.parseInt(props.getProperty("org.conscrypt.version.patch", "-1"));
        }
    } catch (IOException e) {
        // ...
    } finally {
        IoUtils.closeQuietly(stream);
    }
    // VERSION = ...
}

getResourceAsStream() triggers a classloader resource lookup (APK/zip
lookup and stream setup), then Properties.load() parses the stream — all
on the calling thread inside <clinit>.

Suggested fix

Generate the version as compile-time constants at build time. The build script
that currently generates conscrypt.properties would emit:

// Generated from the Gradle project version. Do not edit.
package org.conscrypt;

final class ConscryptVersion {
    static final int MAJOR = 2;
    static final int MINOR = 10;
    static final int PATCH = 0;

    private ConscryptVersion() {}
}

and Conscrypt.java would use:

private static final Version VERSION = new Version(
        ConscryptVersion.MAJOR, ConscryptVersion.MINOR, ConscryptVersion.PATCH);

Because the values are static final int constants, javac inlines them, so
<clinit> contains only a few iconst/putstatic instructions and performs
no I/O. The version still stays in sync with build.gradle for both the
OpenJDK and Android artifacts.

A less invasive alternative is to move the lookup into an
initialization-on-demand holder, so merely loading Conscrypt does no I/O
and the properties are read only on the first version() call.

Additional notes
  • conscrypt.properties is only consumed by Conscrypt.java
    (org.conscrypt.version.*), so it can be replaced by generated constants.
  • The current code silently returns a null version if the resource is
    missing or unreadable, so the runtime resource is not a reliable source
    even today.
  • I understand external PRs are no longer accepted; filing this for
    consideration. A patch against current master is available on my fork:
    https://github.com/wuyisheng/conscrypt/commit/76fa451e13c0bed9fb7f1a1a33679208c6d274f2
主要语言
Java
星标
1.4k
派生
332
平均合并
2 天 56 分钟
30 天内合并 PR
5

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/conscrypt 的其他 Issue

查看 google/conscrypt 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。