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

Đang mở
#1,570 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
49/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java, macos
Lĩnh vực
backend, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Java
Star
1.4k
Fork
332
Merge trung bình
2 ngày 56 phút
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của google/conscrypt

Tất cả issue của google/conscrypt

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.