Reuse the extracted native library across JVMs to avoid a slow load on macOS

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

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
49/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
java, macos

调研方向

Start by reading NativeLibraryLoader and the native library extraction and loading flow; the issue does not give file paths or test names. Explore how a stable content-derived copy can be safely reused across JVMs and how a fresh copy handles an already-loaded library in the same JVM. Done means repeated JVMs can reuse the extracted library, concurrent extraction remains safe, and the existing fallback still works.

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

描述

On macOS, loading Conscrypt's native library costs about 0.6 to 0.9 s in every new JVM, and more on machines with endpoint security software. NativeLibraryLoader copies the library out of the jar into a new temp file with a random name, loads it, and deletes it. macOS checks the code signature of every binary file it has not seen before, so each JVM pays that check in dlopen. Loading a file that has been loaded before takes under 1 ms.

This adds up for test suites and build tools that start many short-lived JVMs. AlexanderGH measured it in Robolectric (robolectric/robolectric#11839): a trivial test takes 3031 ms with Conscrypt loaded the usual way and 2414 ms when Conscrypt loads a previously loaded copy of the same library. A 16 KB dylib shows the same thing:

public class T {
  public static void main(String[] a) throws Exception {
    java.nio.file.Path p = java.nio.file.Paths.get(a[0]);
    if (a.length > 1) { // copy to a new temp file first, as Conscrypt does
      java.nio.file.Path t = java.nio.file.Files.createTempFile("tiny", ".dylib");
      java.nio.file.Files.copy(p, t, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
      p = t;
    }
    long t0 = System.nanoTime();
    System.load(p.toAbsolutePath().toString());
    System.out.printf("%s %.1f ms%n", a.length > 1 ? "fresh" : "stable", (System.nanoTime() - t0) / 1e6);
  }
}
printf 'int tiny_fn(void){return 42;}\n' > tiny.c && clang -dynamiclib -o libtiny.dylib tiny.c
javac T.java
java T libtiny.dylib fresh   # fresh 1335.6 ms
java T libtiny.dylib         # stable 0.5 ms

Would you consider extracting the library once, under a name derived from its contents (for example a hash of the resource), into a stable directory, and reusing it when it is already there? Writing it to a temp file and renaming it into place would keep concurrent JVMs safe, and the file would not be deleted after loading. If the stable copy is already loaded by another class loader in the same JVM, falling back to a fresh copy as today would keep that case working. This would also help with #933, since the temp directory would no longer fill up with copies.

The current workaround is to set org.conscrypt.native.workdir to an unwritable directory and put an extracted copy of the library on java.library.path, which relies on Conscrypt's fallback order. Seen with conscrypt-openjdk-uber 2.9.0, JDK 21, macOS 26 on Apple Silicon.

主要语言
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 摘要。