Sử dụng hồ sơ cơ sở
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hồ sơ cơ sở cải thiện tốc độ thực thi mã khoảng 30% kể từ lần khởi chạy đầu tiên bằng cách tránh các bước diễn giải và các bước biên dịch đúng thời điểm (JIT) cho các đường dẫn mã đi kèm. Bằng cách truyền Hồ sơ cơ sở trong một ứng dụng hoặc thư viện, bạn bật Android Runtime (ART) để tối ưu hoá các đường dẫn mã đi kèm thông qua tính năng biên dịch trước khi thực thi (AOT), cung cấp các tính năng nâng cao hiệu suất cho mỗi lượt cài đặt ứng dụng mới và trong mọi bản cập nhật ứng dụng. Tính năng tối ưu hoá theo hồ sơ (PGO) này cho phép ứng dụng tối ưu hoá quá trình khởi động, giảm hiện tượng giật khi tương tác và cải thiện hiệu suất tổng thể khi chạy từ lần khởi chạy đầu tiên cho người dùng cuối.
Những điều cần cân nhắc về hiệu suất của Compose
Compose được phân phối dưới dạng một thư viện thay vì dưới dạng một phần của nền tảng Android.
Phương pháp này cho phép nhóm Compose thường xuyên cập nhật Compose và hỗ trợ nhiều phiên bản Android. Tuy nhiên, việc phân phối Compose dưới dạng thư viện sẽ gây ra hao tổn.
Mã nền tảng Android đã được biên dịch và cài đặt trên thiết bị.
Tuy nhiên, thư viện cần được tải khi ứng dụng khởi chạy và diễn giải JIT khi cần. Điều này có thể làm chậm ứng dụng khi khởi động và trong lần đầu dùng một tính năng của thư viện.
Lợi ích của hồ sơ cơ sở
Bạn có thể cải thiện hiệu suất bằng cách xác định Hồ sơ cơ sở. Các hồ sơ này xác định các lớp và phương thức cần thiết trên các hành trình trọng yếu của người dùng và được phân phối bằng APK hoặc AAB của ứng dụng. Trong quá trình cài đặt ứng dụng, ART biên dịch AOT mã quan trọng này để sẵn sàng sử dụng khi ứng dụng khởi chạy.
Một định nghĩa Hồ sơ cơ sở phù hợp không phải lúc nào cũng đơn giản. Do đó, theo mặc định, Compose sẽ cung cấp một định nghĩa cho Hồ sơ cơ sở. Bạn có thể không cần phải làm gì để thấy lợi ích này. Tuy nhiên, Hồ sơ cơ sở đi kèm với Compose chỉ chứa các tính năng tối ưu hoá cho mã trong thư viện Compose.
Macrobenchmark
Để tối ưu hoá hiệu quả nhất, hãy tạo Hồ sơ cơ sở cho ứng dụng sử dụng Macrobenchmark để bao gồm các hành trình trọng yếu của người dùng. Khi xác định hồ sơ của riêng mình, bạn phải kiểm thử hồ sơ để xác minh rằng hồ sơ đó hữu ích. Để làm việc này, bạn nên viết các thử nghiệm Macrobenchmark cho ứng dụng và kiểm tra kết quả thử nghiệm khi viết và sửa đổi Hồ sơ cơ sở.
Để biết ví dụ về cách viết thử nghiệm Macrobenchmark cho giao diện người dùng Compose, hãy xem mẫu Macrobenchmark Compose.
Tài nguyên khác
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Use a baseline profile\n\n[Baseline Profiles](/baseline-profiles) improve code execution speed by about 30% from the first\nlaunch by avoiding interpretation and just-in-time (JIT) compilation steps for\nincluded code paths. By shipping a Baseline Profile in an app or library, you\nenable Android Runtime (ART) to optimize included code paths through\nahead-of-time (AOT) compilation, providing performance enhancements for every\nnew app install and every app update. This profile-guided optimization (PGO)\nlets apps optimize startup, reduce interaction jank, and improve overall runtime\nperformance from the first launch for end users.\n\nCompose performance considerations\n----------------------------------\n\nCompose is distributed as a library instead of as part of the Android platform.\nThis approach lets the Compose team update Compose frequently and support a wide\nrange of Android versions. However, distributing Compose as a library imposes a\ncost.\n\nAndroid platform code is already compiled and installed on the device.\nLibraries, however, need to be loaded when the app launches and interpreted JIT\nwhen needed. This can slow the app on startup and when it uses a library feature\nfor the first time.\n\nBenefits of baseline profiles\n-----------------------------\n\nYou can improve performance by defining [Baseline Profiles](/topic/performance/baselineprofiles/overview). These profiles\ndefine classes and methods needed on critical user journeys and are distributed\nwith your app's APK or AAB. During app installation, ART compiles this critical\ncode AOT so that it's ready for use when the app launches.\n\nA good Baseline Profile definition is not always straightforward, and because\nof this, Compose ships with one by default. You might not have to do any work to\nsee this benefit. However, the Baseline Profile that ships with Compose only\ncontains optimizations for the code within the Compose library.\n\n### Macrobenchmark\n\nTo get the best optimization, [create a Baseline Profile](/topic/performance/baselineprofiles/create-baselineprofile)\nfor your app that uses [Macrobenchmark](/studio/profile/macrobenchmark-overview) to cover critical user journeys. When\nyou define your own profile, you must test the profile to verify that it's\nhelping. A good way to do that is to write [Macrobenchmark](/studio/profile/macrobenchmark-overview) tests for your\napp and check the test results as you write and revise your Baseline Profile.\n\nFor an example of how to write Macrobenchmark tests for your Compose UI, see the\n[Macrobenchmark Compose sample](https://github.com/android/performance-samples/tree/main/MacrobenchmarkSample).\n\nAdditional Resources\n--------------------\n\n- **[App performance guide](/topic/performance/overview)**: Discover best practices, libraries, and tools to improve performance on Android.\n- **[Inspect Performance](/topic/performance/inspecting-overview):** Inspect app performance.\n- **[Benchmarking](/topic/performance/benchmarking/benchmarking-overview):** Benchmark app performance.\n- **[App startup](/topic/performance/appstartup/analysis-optimization):** Optimize app startup.\n- **[Baseline profiles](/baseline-profiles):** Understand baseline profiles."]]