การเขียนเฟสและประสิทธิภาพ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมื่อ Compose อัปเดตเฟรม จะมี 3 ขั้นตอนดังนี้
- การจัดองค์ประกอบ: Compose จะกำหนดสิ่งที่จะแสดง โดยจะเรียกใช้ฟังก์ชันที่ประกอบกันได้
และสร้างแผนผัง UI
- เลย์เอาต์: Compose จะกำหนดขนาดและตำแหน่งของแต่ละองค์ประกอบใน
แผนผัง UI
- การวาด: Compose จะแสดงผลองค์ประกอบ UI แต่ละรายการจริงๆ
Compose สามารถข้ามขั้นตอนเหล่านั้นได้อย่างชาญฉลาดหากไม่จำเป็น ตัวอย่างเช่น
สมมติว่าองค์ประกอบกราฟิกเดียวสลับระหว่างไอคอน 2 ไอคอนที่มีขนาดเท่ากัน
เนื่องจากองค์ประกอบนี้ไม่ได้เปลี่ยนขนาด และไม่มีการเพิ่มหรือนำองค์ประกอบใดๆ ออกจากแผนผัง UI ดังนั้น Compose จึงข้ามขั้นตอนการจัดองค์ประกอบและเลย์เอาต์ไปได้ และวาดองค์ประกอบนี้ใหม่
อย่างไรก็ตาม ข้อผิดพลาดในการเขียนโค้ดอาจทำให้ Compose ระบุได้ยากว่าควรข้ามเฟสใดอย่างปลอดภัย ในกรณีนี้ Compose จะเรียกใช้ทั้ง 3 เฟส ซึ่งอาจทำให้ UI ทำงานช้าลง ดังนั้น แนวทางปฏิบัติแนะนำด้านประสิทธิภาพหลายอย่างจึงมีไว้เพื่อช่วยให้ Compose
ข้ามเฟสที่ไม่จำเป็นต้องทำ
ดูข้อมูลเพิ่มเติมได้ที่คู่มือระยะต่างๆ ของ Jetpack Compose
หลักการทั่วไป
หลักการกว้างๆ 2 ข้อที่คุณควรปฏิบัติตามเพื่อปรับปรุงประสิทธิภาพโดยรวมมีดังนี้
- ย้ายการคำนวณออกจากฟังก์ชันที่ประกอบกันได้ทุกครั้งที่เป็นไปได้
คุณอาจต้องเรียกใช้ฟังก์ชันที่ประกอบกันได้อีกครั้งทุกครั้งที่ UI เปลี่ยนแปลง ระบบจะเรียกใช้โค้ดที่คุณใส่ในฟังก์ชันที่ประกอบกันได้อีกครั้ง ซึ่งอาจเป็นทุกเฟรม
ของภาพเคลื่อนไหว จำกัดโค้ดของฟังก์ชันที่ประกอบกันได้ให้มีเฉพาะสิ่งที่จำเป็นต่อการสร้าง UI เท่านั้น
- เลื่อนการอ่านสถานะให้นานที่สุด การย้ายการอ่านสถานะไปยังฟังก์ชันที่ประกอบกันได้ระดับล่างหรือเฟสที่ตามมาจะช่วยลดการจัดองค์ประกอบใหม่หรือข้ามเฟสการจัดองค์ประกอบไปเลยได้
คุณทำได้โดยส่งฟังก์ชัน Lambda
แทนค่าสถานะสำหรับสถานะที่เปลี่ยนแปลงบ่อย และโดยการเลือกใช้
ตัวแก้ไขที่อิงตาม Lambda เมื่อส่งสถานะที่เปลี่ยนแปลงบ่อย คุณดูตัวอย่างเทคนิคนี้ได้ในส่วนเลื่อนการอ่านให้นานที่สุด
ของทำตามแนวทางปฏิบัติแนะนำ
แหล่งข้อมูลเพิ่มเติม
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-11 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-08-11 UTC"],[],[],null,["# Compose phases and performance\n\nWhen Compose updates a frame, it goes through three phases:\n\n- **Composition:** Compose determines *what* to show. It runs composable functions and builds the UI tree.\n- **Layout:** Compose determines the size and placement of each element in the UI tree.\n- **Drawing:** Compose actually *renders* the individual UI elements.\n\nCompose can intelligently skip any of those phases if they aren't needed. For\nexample, suppose a single graphic element swaps between two icons of the same\nsize. Since this element isn't changing size, and no elements of the UI tree are\nbeing added or removed, Compose can skip over the composition and layout phases\nand redraw this one element.\n\nHowever, coding mistakes can make it hard for Compose to know which phases it\ncan safely skip, in which case Compose runs all three phases, which can slow\ndown your UI. So, many of the performance best practices are to help Compose\nskip the phases it doesn't need to do.\n\nFor more information, see the [Jetpack Compose Phases](/develop/ui/compose/phases) guide.\n\nGeneral principles\n------------------\n\nThere are a couple of broad principles to follow that can improve performance in\ngeneral:\n\n- **Whenever possible, move calculations out of your composable functions.** Composable functions might need to be rerun whenever the UI changes. Any code you put in the composable gets re-executed, potentially for every frame of an animation. Limit the composable's code to only what it needs to build the UI.\n- **Defer state reads for as long as possible.** By moving state reading to a child composable or a later phase, you can minimize recomposition or skip the composition phase entirely. You can do this by passing lambda functions instead of the state value for frequently changing state and by preferring lambda-based modifiers when you pass in frequently changing state. You can see an example of this technique in the [Defer reads as long as possible](/develop/ui/compose/performance/bestpractices#defer-reads) section of [Follow best practices](/develop/ui/compose/performance/bestpractices).\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."]]