Hot Code Replacement Using Incremental Bytecode Patching
Find Innovative SolutionsGenerate Solutions
Solution Overview
Problem
Existing debugging methods for large-scale software applications are inefficient due to the need for repeated stopping and restarting, which is time-consuming and affects debugging efficiency, especially when modifications beyond method bodies are required.
Innovation Solution
A hot code replace method that involves obtaining and transferring first and second bytecodes, patching instructions to invoke corresponding content in a third bytecode, and loading target bytecodes to support modifications beyond method bodies without full application restarts, using incremental compilation and dynamic generation of bytecodes.
Engineering Contradictions & Design Principles
Engineering Contradiction Analysis
1Reliability
If repeated stopping and restarting is performed to modify code and verify running results, then code debugging can be completed, but debugging time increases significantly
Solution Approach 1:
The patent applies preliminary action by pre-compiling modified code into new bytecode before actually needing to verify it. The bytecode replacement mechanism is prepared in advance, allowing immediate switching between old and new code versions without restarting the application, thus reducing debugging time while maintaining verification accuracy
Solution Approach 2:
The patent creates a copy of the original bytecode and modifies it to generate new bytecode. This copying approach allows the modified code to be tested independently by replacing the original bytecode in memory, enabling rapid verification without affecting the running application or requiring restarts
2Reliability
If full compilation is performed after code modification, then code changes are processed, but compilation time increases debugging costs
Solution Approach 1:
The patent applies partial action by performing only the necessary compilation steps for modified code portions rather than full compilation. The incremental compilation approach compiles only the changed classes and generates only the necessary bytecode segments, which are then selectively replaced in memory, significantly reducing compilation time while ensuring complete processing of code modifications
3Loss of time
If existing hot code replace solution is used for method body modification, then debugging time is reduced, but it cannot support modifications beyond method bodies
Solution Approach 1:
The patent achieves universality by creating a bytecode replacement mechanism that handles multiple types of code modifications beyond just method bodies. It supports class-level modifications, field changes, method signature changes, and structural modifications by generating and replacing entire bytecode segments or classes, making the debugging tool versatile for various modification scenarios while maintaining rapid replacement capability
Data Source
Figure 1~2
Figure 3~4
Figure 5~6
AI summary
This application provides a hot code replace method and apparatus. The method includes: obtaining a first bytecode obtained by compiling source code and a second bytecode obtained by performing incremental compilation on the source code after modification; transferring, to a third bytecode, a declaration of first-type modification content in modification content in the second bytecode relative to the first bytecode, where the first-type modification content includes a modification other than a modification of a method body; patching at least one instruction in the second bytecode after transfer or the third bytecode, where the at least one instruction is used to invoke at least one piece of content in the second bytecode, and the at least one patched instruction is used to invoke content that is in the third bytecode and that corresponds to the at least one piece of content; and loading a target second bytecode and a target third bytecode. Solutions in embodiments of this application help improve debugging efficiency.