A bytecode instruction set reduction method and system
By dividing the constant pool into multiple sub-constant pools and generating instructions with single-byte indexes, the problems of large storage space and poor execution performance of the Dalvik virtual machine instruction set are solved, achieving storage space saving and execution performance improvement.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING WATCH DATA SYSTEM CO LTD
- Filing Date
- 2020-12-24
- Publication Date
- 2026-08-04
AI Technical Summary
The bytecode generated by the existing Dalvik virtual machine instruction set occupies a large amount of storage space, while the Java Card virtual machine instruction set has poor execution performance, making it difficult to meet the requirements of both storage space and execution performance at the same time.
The constant pool is divided into multiple sub-constant pools. The number of times instructions appear is counted based on the frequency of reference types. Instructions that generate single-byte constant pool indices are generated, and reference types that are accessed frequently are placed first. This method is suitable for virtual machine instruction sets based on operand stacks and registers.
It effectively reduces bytecode storage space and improves execution performance, and is suitable for Java Card, Dalvik and TGoMOS virtual machine instruction sets.
Smart Images

Figure CN112631722B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to virtual machine instruction sets, and more specifically to a method and system for simplifying bytecode instruction sets. Background Technology
[0002] A virtual machine (VM) is an abstract computer created by a software application or sequence of instructions executed by a processor. VMs can execute instruction sets supported by the VM. Instruction sets are categorized into two types: operand stack-based and register-based. The Java Card VM uses an operand stack-based instruction set, while the Dalvik VM uses a register-based instruction set. Bytecode generated using the existing Dalvik VM instruction set occupies a larger storage space but offers better execution performance, while bytecode generated using the Java Card VM instruction set occupies less storage space but has generally lower performance. Therefore, to meet higher performance requirements, it is urgent to design a method for reducing instruction size to save storage space, improve execution performance, and be applicable to both instruction sets. Summary of the Invention
[0003] In view of the shortcomings of the existing technology, the purpose of this invention is to provide a bytecode instruction set simplification method and system, which can simplify virtual machine bytecode, reduce bytecode storage space, and improve bytecode execution performance.
[0004] To achieve the above objectives, the technical solution adopted by the present invention is as follows:
[0005] A bytecode instruction set simplification method, applicable to operand stack-based and register-based instruction sets, includes the following steps:
[0006] S100. Based on different reference types, the constant pool is divided into multiple corresponding sub-constant pools;
[0007] S200. Based on the number of references for each reference type, count the number of times the instruction corresponding to each reference type appears, and put them into the corresponding sub-constant pool in descending order. The constant pool index of the instruction corresponding to each reference type is the index of the sub-constant pool corresponding to each reference type.
[0008] S300. Based on the generation conditions of the instruction corresponding to each reference type, generate the instruction for the corresponding single-byte constant pool index.
[0009] Furthermore, in the simplified method described above, the reference types include class references, static method references, virtual method references, super method references, static field references, and instance field references. The multiple sub-constant pools include: the class sub-constant pool, the static method sub-constant pool, the virtual method sub-constant pool, the super method sub-constant pool, the static field sub-constant pool, and the instance field sub-constant pool.
[0010] Furthermore, in the simplification method described above, S200 includes:
[0011] If the number of references to the instance field exceeds 256, the number of times the instance field access instructions of the reference type appear is counted, and they are placed into the sub-constant pool of the instance field in descending order of the number of occurrences; otherwise, they are placed directly into the sub-constant pool of the instance field.
[0012] If the number of references to the static field exceeds 256, the number of times the static field access instructions of the reference type in the package are counted, and they are placed into the sub-constant pool of the static field in descending order of the number of times; otherwise, they are placed directly into the sub-constant pool of the static field.
[0013] If the number of references to the virtual method exceeds 256, the number of times each virtual method call instruction appears is counted, and the virtual method is placed into the sub-constant pool of the virtual method in descending order of the number of times it appears; otherwise, it is placed directly into the sub-constant pool of the virtual method.
[0014] If the number of references to the static method exceeds 256, first count the number of times the static method instruction appears in the calling library package, and put it into the sub-constant pool of the static method in descending order of the number of times. Then count the number of times the static method instruction appears in each calling package, and put it into the sub-constant pool of the static method in descending order of the number of times. Otherwise, put it directly into the sub-constant pool of the static method.
[0015] Furthermore, in the simplification method described above, the number of occurrences of static field access instructions of the reference type within the statistical package is placed into the sub-constant pool of the static field in descending order of frequency, including:
[0016] The number of times static field access instructions of reference types within the count package are counted, and they are placed at the beginning of the sub-constant pool of the static field in descending order of frequency.
[0017] Furthermore, in the simplification method described above, S300 includes:
[0018] If the constant pool index of an instance field access instruction is within one byte, then an instance field access instruction with a corresponding single-byte constant pool index is generated.
[0019] If a static field access instruction has a constant pool index within one byte and is a static field access instruction of the in-package reference type, then a static field access instruction with a corresponding single-byte constant pool index is generated.
[0020] If the constant pool index of a virtual method call instruction is within one byte, then a virtual method call instruction with a corresponding single-byte constant pool index is generated.
[0021] Furthermore, in addition to the simplification method described above, S300 also includes:
[0022] Based on the statistical number of times each library package's static methods are called in a large number of applications, a list of commonly used library package static methods is generated and placed in a configuration file. The number of static methods in the list of commonly used library package static methods is less than 256 and has a complete Java method name.
[0023] If the constant pool index of a static method call instruction of a library package is within one byte range, then the configuration file is queried to confirm whether the static method of the library package called by the static method call instruction of the library package exists in the list of commonly used static methods of the library package;
[0024] If it is confirmed that the static method of the library package exists in the list of commonly used static methods of the library package, then the library package static method call instruction with the corresponding single-byte constant pool index is generated.
[0025] Furthermore, in the simplification method described above, the step of generating a list of commonly used static methods of library packages based on the statistical number of times each library package's static methods are called in a large number of applications and placing it in a configuration file includes:
[0026] Sort the number of times each library package's static method is called from highest to lowest. If the total number of static methods in all libraries exceeds 256, add the top 256 static methods to the list of commonly used static methods. If the number is less than 256, add all static methods to the list of commonly used static methods.
[0027] A bytecode instruction set reduction system, applicable to operand stack-based and register-based instruction sets, the reduction system comprising:
[0028] The constant pool partitioning module is used to divide the constant pool into multiple corresponding sub-constant pools based on different reference types.
[0029] The constant pool component generation module is used to count the number of times the instruction corresponding to each reference type appears based on the number of references of each reference type, and put them into the corresponding sub-constant pool in descending order. The constant pool index of the instruction corresponding to each reference type is the index of the sub-constant pool corresponding to each reference type.
[0030] The instruction generation module is used to generate instructions with corresponding single-byte constant pool indices based on the instruction generation conditions for each reference type.
[0031] Furthermore, in the simplified system described above, the reference types include class references, static method references, virtual method references, super method references, static field references, and instance field references. The multiple sub-constant pools include: the class sub-constant pool, the static method sub-constant pool, the virtual method sub-constant pool, the super method sub-constant pool, the static field sub-constant pool, and the instance field sub-constant pool.
[0032] Furthermore, in the simplified system described above, the constant pool component generation module is specifically used for:
[0033] If the number of references to the instance field exceeds 256, the number of times each instance field access instruction of the reference type appears is counted, and the instances are placed into the sub-constant pool of the instance field in descending order of the number of occurrences; otherwise, they are placed directly into the sub-constant pool of the instance field.
[0034] If the number of references to the static field exceeds 256, the number of times the static field access instructions of the reference type in the package are counted, and they are placed into the sub-constant pool of the static field in descending order of the number of times; otherwise, they are placed directly into the sub-constant pool of the static field.
[0035] If the number of references to the virtual method exceeds 256, the number of times each virtual method call instruction appears is counted, and the virtual method is placed into the sub-constant pool of the virtual method in descending order of the number of times it appears; otherwise, it is placed directly into the sub-constant pool of the virtual method.
[0036] If the number of references to the static method exceeds 256, first count the number of times the static method instruction appears in the calling library package, and put it into the sub-constant pool of the static method in descending order of the number of times. Then count the number of times the static method instruction appears in each calling package, and put it into the sub-constant pool of the static method in descending order of the number of times. Otherwise, put it directly into the sub-constant pool of the static method.
[0037] The beneficial effects of this invention are as follows: This invention is applicable to both operand stack-based and register-based virtual machine instruction sets. Before generating instructions, it counts the frequency of method calls and instructions accessing reference type fields. The constant pool is divided into multiple sub-constant pools, with each sub-constant pool prioritizing the placement of frequently occurring references. Instructions accessing instance fields frequently use single-byte constant pool indexes. Instructions accessing virtual method calls frequently use single-byte constant pool indexes. Instructions accessing static fields frequently use single-byte constant pool indexes. Instructions accessing library package static method calls frequently use single-byte constant pool indexes. Through the above design, the bytecode size of methods can be effectively reduced without using many opcodes. Attached Figure Description
[0038] Figure 1 This is a schematic diagram of the structure of the constant pool provided in an embodiment of the present invention;
[0039] Figure 2 This is a schematic diagram illustrating the usage of the single-byte static field constant pool index provided in this embodiment of the invention;
[0040] Figure 3 This is a diagram illustrating the usage of the single-byte static method constant pool index provided in this embodiment of the invention (before parsing), and a static method call diagram of the library package.
[0041] Figure 4 This is a diagram illustrating the usage of the single-byte static method constant pool index provided in this embodiment of the invention (after parsing), and a static method call diagram of the library package.
[0042] Figure 5 This is another way of using the single-byte static method constant pool index provided in this embodiment of the invention (after parsing), and a schematic diagram of static method calls within the package and library package;
[0043] Figure 6 This is a flowchart illustrating a bytecode instruction set simplification method provided in an embodiment of the present invention;
[0044] Figure 7 This is a schematic diagram of the structure of a bytecode instruction set simplification system provided in an embodiment of the present invention. Detailed Implementation
[0045] The present invention will now be described in further detail with reference to the accompanying drawings and specific embodiments.
[0046] Glossary
[0047] Constant pool: The virtual machine instruction set accesses symbolic references stored in the constant pool through constant pool indexes, including classes (interfaces), static fields, static methods, instance fields, virtual methods, and super methods.
[0048] Within a package: The bytecode generation program performs conversion on a per-Java-package basis. In this invention, "within a package" refers to the Java package to be converted, which imports library packages.
[0049] A library package is a Java package that can be imported by other Java packages. The Java Card API is implemented as a Java library and can be referenced by classes in other packages. A Java class defined in a package (within the package) can call methods provided by classes in the library package.
[0050] The method of this invention is applicable to the Java Card virtual machine instruction set, the Dalvik virtual machine instruction set, and the TGoMOS virtual machine instruction set. The method of this invention will be described below using the TGoMOS virtual machine instruction set. The TGoMOS virtual machine instruction set is a reduced instruction set of micro-operating system bytecode, comprising six instructions.
[0051] The first instruction consists of an opcode, which implicitly contains the parameter information of the first instruction. The first instruction includes: an instruction that implicitly contains both operands and register numbers in the opcode; an instruction that implicitly contains register numbers in the opcode; an instruction that implicitly contains constant operands in the opcode; an array member access instruction that implicitly contains the member type of array members in the opcode; and an instruction that implicitly contains the type and parameters of a method call in the opcode.
[0052] The second instruction is a high-frequency instruction with multiple instruction formats; the second instruction includes: common arithmetic operation instructions with multiple operation formats; array member access instructions using 4-bit register format; array creation instructions that implicitly embed the member type of array members into the opcode; and branch instructions with multiple instruction formats based on equality and inequality comparison results.
[0053] The third instruction includes instructions with different instruction formats based on different numbers of parameters; the third instruction includes: static method call instructions with different instruction formats based on different numbers of parameters; virtual method call instructions with different instruction formats based on different numbers of parameters; and private instance method call instructions with different instruction formats based on different numbers of parameters.
[0054] The fourth instruction includes instructions for commonly used data types and instructions for less commonly used data types. Instructions for commonly used data types have multiple instruction formats, while instructions for less commonly used data types have only one instruction format. Instructions for commonly used data types include instructions for the short data type, while instructions for less commonly used data types include instructions for the int data type.
[0055] The fifth instruction is an instruction with a single-byte constant pool index; the fifth instruction includes: a static method call instruction with a single-byte constant pool index; a virtual method call instruction with a single-byte constant pool index; a static field access instruction with a single-byte constant pool index; and an instance field access instruction with a single-byte constant pool index.
[0056] The sixth instruction is a macro instruction. The sixth instruction includes macro instructions formed based on static method call instruction conversion and macro instructions formed by merging multiple instructions.
[0057] The seventh instruction is any instruction other than the first six categories.
[0058] The implementation of this invention includes the following four parts.
[0059] I. Design of the constant pool component in a loadable and executable file format
[0060] 1. Group reference types together to form 6 sub-constant pools. There are 6 reference types: 1) Class reference; 2) Static method reference; 3) Virtual method reference; 4) Super method reference; 5) Static field reference; 6) Instance field reference. Each reference type is grouped together to form a sub-constant pool. Specifically, all class reference types are grouped together to form the class sub-constant pool, all static method reference types are grouped together to form the static method sub-constant pool, all virtual method reference types are grouped together to form the virtual method sub-constant pool, all Super method reference types are grouped together to form the Super method sub-constant pool, all static field reference types are grouped together to form the static field sub-constant pool, and all instance field reference types are grouped together to form the instance field sub-constant pool.
[0061] 2. If the number of references in each sub-constant pool exceeds 256, references of a specific type will be sorted by frequency of use from high to low and placed into the sub-constant pool first.
[0062] 3. The constant pool index in the instruction is the index of the corresponding sub-constant pool. Some instructions use constant pool indexes, and different instructions will use different types of constant pool indexes. For example, instance field access instructions include instance field sub-constant pool index operands, and the corresponding instance field reference can be found in the instance field sub-constant pool through the instance field sub-constant pool index.
[0063] Compared to the design without a sub-constant pool, if the size of the entire constant pool exceeds 256, the index of a specific constant pool is likely to exceed the range of a single byte. By splitting it into 6 sub-constant pools, the index of each constant pool item will basically not exceed the range of 1 byte.
[0064] Figure 1An implementation of a constant pool is shown, which includes 6 sub-constant pools. The order of each sub-constant pool within the constant pool is not required, nor is the method of accessing the sub-constant pools (i.e., the way to record the offset and size of each sub-constant pool) required.
[0065] II. Instruction Set Design
[0066] In the TGoMO virtual machine instruction set, a one-byte opcode can specify the operation type, whether operands are required and their types, and whether a result is returned. Some instructions require access to the constant pool (such as the fifth instruction), therefore, the instruction will include the corresponding constant pool index operand. The constant pool index of an instruction can be one or two bytes long. The length of the constant pool index depends on two conditions: 1) the size of the index of the specific reference in the corresponding sub-constant pool; 2) the length of bytes required after the specific reference is resolved in the constant pool. For frequently used instructions, using a single-byte index can significantly reduce the bytecode length of the method. If an instruction with a single-byte constant pool index is used N times, it will save N bytes compared to an instruction using a 2-byte index. Through analysis of numerous application conversion results, the following instructions were found to occur relatively frequently:
[0067] 1. Instance field access instructions for reference types;
[0068] 2. Static field access instructions for reference types defined within a package;
[0069] 3. Static method invocation instructions in the library package;
[0070] 4. Virtual method invocation instruction.
[0071] By designing the constant pool component in the aforementioned loadable and executable file format, it can be ensured that commonly used reference indices do not exceed one byte. Other methods employed by the virtual machine ensure that a one-byte index can store the parsed result. Table 1 shows the instructions that can use single-byte indices, including their functions and applicable conditions.
[0072] Table 1
[0073] Invoke static Calling static methods static methods in the library package Invoke virtual Calling virtual methods No special restrictions Get Static Accessing static domains Reference type fields Get Field Access Instance Domain Reference type fields
[0074] Before generating instructions, index parsing is required, which is a process performed when the application is installed into the virtual machine. Specifically:
[0075] 1. Instance field reference type
[0076] The sub-constant pool of instance fields is resolved to indices of instance field references, with each class numbered individually starting from 0. The condition for using single-byte constant pool indices for instance field references is that the value of the index referenced by the instance field is a single-byte range, i.e., 0 to 255. Single-byte constant pool indices for instance field references can satisfy common application scenarios. One byte can address 256 words. Primitive types of boolean, byte, and short each occupy one word, reference types occupy one word, and primitive variables of type int occupy two words. A word is an abstract storage unit defined by the virtual machine specification. One word must be large enough to accommodate one byte, short, reference, or returnAddress data type, and two words can accommodate one int type.
[0077] 2. Static field reference type
[0078] The index of a static field reference is resolved to an offset of the static field mirror. Static fields declared in all classes within a Java package are grouped together to form a static field mirror. This static field mirror is then offset by a single byte, placing the static fields of reference types at the beginning of the mirror. This allows for single-byte offsets, thus enabling addressing of these frequently accessed static fields of reference types. Figure 2 This shows one way of using the single-byte static field constant pool index. Serial number 20 indicates the offset of the static field mirror in the static field access instruction (replaced after parsing the static field sub-constant pool index), and serial number 21 indicates the static field mirror.
[0079] 3. Virtual method reference
[0080] The index of a virtual method reference is resolved to an index of the virtual method table for each class. The virtual method table is divided into public (protected) type virtual method tables and package-visible virtual method tables. The virtual method invocation instructions in the TGoMOS virtual machine instruction set explicitly specify the number of method parameters, eliminating the need to resolve the parameter count from the virtual method sub-constant pool index.
[0081] 4. Static method references
[0082] The TGoMOS virtual machine's executable calls static methods from library packages. These static methods are masked static methods within the same package, and the resolved result is the global reference table index of the static method. Frequently used static method references need to be added to the front of the global reference table (within a 1-byte access range). This allows the sub-constant pool references of static methods to be resolved into single-byte global reference table indices.
[0083] Figure 3This demonstrates one usage of the single-byte static method constant pool index (before parsing), calling a static method in a library package. Number 32 indicates the content referenced in the sub-constant pool of the library package's static method, including the package index, which can be used to query information about a specified mask package, including Export components. Number 35 indicates the Export component of the specified package; querying the Export component using the Classindex and Methodindex yields the method's bytecode location information. Number 34 indicates the method component of the specified package, including the bytecode of the specified method.
[0084] Figure 4 This illustrates one usage of the single-byte static method constant pool index (after parsing) for a library package static method call. Serial number 40 indicates the index of the global reference table in the static method call instruction (replaced by the constant pool index after parsing). Serial number 41 indicates an entry in the global reference table, including the package index and the offset of the method component within the specified package. During bytecode conversion, frequently used static methods should be prioritized and placed at the beginning of the global reference table, i.e., indices ranging from 0 to 255. Serial number 42 indicates the location of the static method bytecode.
[0085] Figure 5 This illustrates another usage of the single-byte static method constant pool index (after parsing): static method calls within a package or library package. Serial number 51 represents the offset table for frequently called methods within the package; the offsets of commonly used methods are placed in a table, which is then replaced with the corresponding method index during constant pool parsing. Serial number 52 represents the global reference table index for frequently called library package methods; the global reference table indexes of commonly used methods are placed in a table, which is then replaced with the corresponding method index during constant pool parsing. Serial number 53 exemplifies a method component, and serial number 54 exemplifies the global reference table.
[0086] The above method allows for the provision of single-byte indexed versions of frequently used instructions, such as Invvirtual, getstatic-o, getfield-o, and invstatic. For other relatively frequently used instructions, single-byte indexed versions can also be provided if performance and increased RAM requirements are not a concern. For less frequently used instructions, single-byte indexing can be disregarded. This method effectively reduces bytecode size without requiring numerous opcodes.
[0087] III. Conversion Implementation Process
[0088] 1. Generate constant pool components
[0089] The constant pool component consists of 6 sub-constant pools: class sub-constant pool, static method sub-constant pool, virtual method sub-constant pool, super method sub-constant pool, static field sub-constant pool, and instance field sub-constant pool.
[0090] If the number of references to an instance field exceeds 256, count the number of times the instruction appears in each instance field of each reference type, and put them into the instance field sub-constant pool in descending order of the number of occurrences; otherwise, there is no requirement for the order.
[0091] If the number of references to a virtual method exceeds 256, count the number of times each virtual method call instruction is executed and place them into the virtual method constant pool in descending order of the number of times it is executed; otherwise, there is no requirement for the order.
[0092] If the number of references to a static field exceeds 256, count the occurrences of instructions for static fields of reference types within the package and place them into the static field sub-constant pool in descending order of occurrence; otherwise, there is no requirement for the order.
[0093] If the number of references to a static method exceeds 256, count the number of static method call instructions for each library package and place them into the static method sub-constant pool in descending order of the number of calls; otherwise, there is no requirement for the order.
[0094] The above operations ensure that the index values referenced by the constant pool for the following frequently occurring instructions do not exceed one byte. These frequently occurring instructions are: getfield-o (for instance fields of a reference type), getstatic-o (for static fields of a reference type), invvirtual (for virtual methods), and invstatic (for static methods).
[0095] 2. Generate static domain components
[0096] Count the number of times the instructions for each reference type's static field appear, and place them at the beginning of the static field area in descending order of frequency.
[0097] 3. Generation method components
[0098] This mainly refers to static method call instructions. Instructions with single-byte constant pool indices are only applicable to calling commonly used static methods masked into the virtual machine system's library packages. Generation conditions: 1) A configuration file is required specifying a list of commonly used static methods in the library package. The list cannot exceed 256, and each line in the list includes the full name of a static method; 2) The constant pool index of the static methods cannot exceed 256; 3) When generating the executable file, this configuration file is queried when generating static method call instructions. If the method to be called is in this list, a single-byte constant pool index instruction is generated.
[0099] 4. Generate instructions for accessing static fields.
[0100] The single-byte constant pool index instruction is only applicable to accessing static fields of reference types within a package, and the constant pool index has a range that can be represented in one byte.
[0101] 5. Generate instructions to access instance domains
[0102] The conditions for generating a single-byte constant pool index instruction are that when retrieving an instance field of a reference type, the value of the constant pool index does not exceed 1 byte.
[0103] 6. Generate instructions for accessing virtual methods
[0104] The generation condition for a single-byte constant pool index instruction is that the value of the constant pool index does not exceed 1 byte.
[0105] 7. Generation of mask to equipment library
[0106] You need to provide a configuration file specifying a list of commonly used static methods in the library package. The list cannot exceed 256, and each line in the list includes the fully qualified name of a static method. The implementation must make all methods in this list accessible via a one-byte index.
[0107] IV. Virtual Machine Implementation
[0108] In the preferred case where single-byte indexing is used, the executable file of the virtual machine should implement the following functions: calling static methods of the mask library package through single-byte indexing, calling static fields within the package through single-byte indexing, calling virtual methods through single-byte indexing, and calling instance fields through single-byte indexing.
[0109] In addition to the above-mentioned cases where single-byte indexes are used, the present invention also includes the following cases where single-byte indexes are used:
[0110] 1. The executable file calls static methods of dynamically downloaded library packages via single-byte indexes. It supports both package-specific and library-specific static methods, with a maximum of 128 methods (the highest bit must distinguish between package-specific and library-specific methods). For methods supporting only library-specific static methods, the maximum number of methods is 256. A table needs to be added to the global reference table index of static methods that are called multiple times. The index of the table that resolves the constant pool to the global reference table index of static methods is also needed. The global reference table index of the method is found using the index, and the method's offset and the package index of the package containing the method's class are obtained from the global reference table index.
[0111] 2. Executable files call static methods (including private instance methods and...) within a package using single-byte indexes. <init>(Methods). It supports both package-level and library-level static methods, with a maximum of 128 methods (the highest bit needs to distinguish between package-level and library-level methods). For methods supporting only package-level static methods, the maximum number of methods is 256. An offset table needs to be added for static methods that are called multiple times. The constant pool is resolved to an index in the offset table of static methods. The method's offset within the method component is found using the index.
[0112] Based on the above design concept, this invention provides a bytecode instruction set simplification method applicable to the JavaCard instruction set, Dalvik instruction set, and the aforementioned TGoMOS virtual machine instruction set. The fifth instruction of the TGoMOS virtual machine instruction set includes: a static method call instruction with a single-byte constant pool index; a virtual method call instruction with a single-byte constant pool index; a static field access instruction with a single-byte constant pool index; and an instance field access instruction with a single-byte constant pool index. The simplification method of this invention can generate the fifth instruction of the TGoMOS virtual machine instruction set.
[0113] like Figure 6 As shown, the simplification method includes the following steps:
[0114] S100. Based on different reference types, the constant pool is divided into multiple corresponding sub-constant pools;
[0115] Reference types include references to classes, static methods, virtual methods, super methods, static fields, and instance fields. Multiple sub-constant pools include: sub-constant pools of classes, static methods, virtual methods, super methods, static fields, and instance fields.
[0116] S200. Based on the number of references for each reference type, count the number of times the instruction corresponding to each reference type appears, and put them into the corresponding sub-constant pool in descending order. The constant pool index of the instruction corresponding to each reference type is the index of the sub-constant pool corresponding to each reference type.
[0117] S200 includes:
[0118] If the number of references to an instance field exceeds 256, count the number of times each instance field access instruction of the reference type appears and put it into the instance field's sub-constant pool in descending order of the number of occurrences; otherwise, put it directly into the instance field's sub-constant pool.
[0119] If the number of references to a static field exceeds 256, count the number of times each static field access instruction of the reference type appears and put it into the sub-constant pool of the static field in descending order of the number of times; otherwise, put it directly into the sub-constant pool of the static field.
[0120] Specifically, count the number of times each static field access instruction for a reference type appears, and prioritize placing it at the front of the static field's sub-constant pool in descending order of frequency.
[0121] If the number of references to a virtual method exceeds 256, then count the number of times each virtual method call instruction appears and put it into the virtual method's sub-constant pool in descending order of the number of times; otherwise, put it directly into the virtual method's sub-constant pool.
[0122] If the number of references to a static method exceeds 256, first count the number of times the static method instruction appears in the calling library package, and put it into the static method's sub-constant pool in descending order of the number of times. Then count the number of times the static method instruction appears in each calling package, and put it into the static method's sub-constant pool in descending order of the number of times. Otherwise, put it directly into the static method's sub-constant pool.
[0123] S300. Based on the generation conditions of the instruction corresponding to each reference type, generate the instruction for the corresponding single-byte constant pool index.
[0124] Instructions for generating corresponding single-byte constant pool indices for instance field access instructions, static field access instructions, and virtual method call instructions, S300 includes:
[0125] 1) If the constant pool index of an instance field access instruction is within one byte, then an instance field access instruction with a corresponding single-byte constant pool index is generated.
[0126] 2) If the constant pool index of a static field access instruction is within one byte and is a static field access instruction of the package reference type, then a static field access instruction with a corresponding single-byte constant pool index is generated.
[0127] 3) If the constant pool index of a virtual method call instruction is within one byte, then a virtual method call instruction with a corresponding single-byte constant pool index is generated.
[0128] 4) Instructions for generating corresponding single-byte constant pool indices for static method call instructions, including:
[0129] 4.1) Based on the statistical number of times each library package's static methods are called in a large number of applications, generate a list of commonly used library package static methods and put it into the configuration file. The number of static methods in the list of commonly used library package static methods is less than 256 and has a complete name.
[0130] Specifically, the number of times each library's static methods are called in a large number of applications is sorted from high to low. If the total number of static methods in all libraries exceeds 256, the top 256 static methods are added to the list of commonly used static methods. If the number is less than 256, all static methods are added directly to the list of commonly used static methods.
[0131] 4.2) If the constant pool index of a static method call instruction of a library package is within one byte range, then query the configuration file to confirm whether the static method of the library package corresponding to the static method call instruction exists in the list of commonly used static methods of the library package.
[0132] 4.3) If it is confirmed that the static method of the library package exists in the list of commonly used library package static methods, then generate the library package static method call instruction with the corresponding single-byte constant pool index.
[0133] This invention is applicable to both operand stack-based and register-based virtual machine instruction sets. Before generating instructions, the frequency of method calls and instructions accessing reference type fields is statistically analyzed. The constant pool is divided into multiple sub-constant pools, and each sub-constant pool prioritizes placing frequently occurring references. Frequently accessed static methods are pre-placed at the front of the reference table, and frequently accessed static fields of reference types are pre-placed at the front of the static field image. Instructions accessing instance fields, virtual method calls, static fields, and static method calls are all indexed using a single-byte constant pool. Through this design, the bytecode size of methods can be effectively reduced without using many opcodes.
[0134] To verify the effectiveness of the above techniques, we used a constant pool index from a typical financial application. Without the sub-constant pool technique, the constant pool has 333 items, and 77 of them have indexes exceeding 256. With the sub-constant pool technique, the number of references to each sub-constant pool does not exceed 256, therefore the sub-constant pool index can use a single byte. Table 2 below shows the instruction call details for a typical financial application.
[0135] Table 2
[0136]
[0137] As shown in Table 2, using single-byte indexes can reduce the bytecode size by 3261 bytes. Therefore, the simplification method of this invention can significantly reduce virtual machine bytecode, decrease bytecode storage space, and improve bytecode execution performance.
[0138] like Figure 7 As shown, this embodiment of the invention also provides a bytecode instruction set simplification system, applicable to operand stack-based and register-based instruction sets. The simplification system includes:
[0139] The constant pool partitioning module 100 is used to divide the constant pool into multiple corresponding sub-constant pools based on different reference types.
[0140] The constant pool component generation module 200 is used to count the number of times the instruction corresponding to each reference type appears based on the number of references of each reference type, and put them into the corresponding sub-constant pool in descending order. The constant pool index of the instruction corresponding to each reference type is the index of the sub-constant pool corresponding to each reference type.
[0141] The instruction generation module 300 is used to generate instructions with corresponding single-byte constant pool indices based on the instruction generation conditions corresponding to each reference type.
[0142] Reference types include references to classes, static methods, virtual methods, super methods, static fields, and instance fields. Multiple sub-constant pools include: sub-constant pools of classes, static methods, virtual methods, super methods, static fields, and instance fields.
[0143] The constant pool component generation module 200 is specifically used for:
[0144] If the number of references to an instance field exceeds 256, count the number of times each instance field access instruction of the reference type appears and put it into the instance field's sub-constant pool in descending order of the number of occurrences; otherwise, put it directly into the instance field's sub-constant pool.
[0145] If the number of references to a static field exceeds 256, count the number of times each static field access instruction of the reference type appears and put it into the sub-constant pool of the static field in descending order of the number of times; otherwise, put it directly into the sub-constant pool of the static field.
[0146] If the number of references to a virtual method exceeds 256, then count the number of times each virtual method call instruction appears and put it into the virtual method's sub-constant pool in descending order of the number of times; otherwise, put it directly into the virtual method's sub-constant pool.
[0147] If the number of references to a static method exceeds 256, first count the number of times the static method instruction appears in the calling library package, and put it into the static method's sub-constant pool in descending order of the number of times. Then count the number of times the static method instruction appears in each calling package, and put it into the static method's sub-constant pool in descending order of the number of times. Otherwise, put it directly into the static method's sub-constant pool.
[0148] In the instruction generation module 300, instructions for generating corresponding single-byte constant pool indices are generated for instance field access instructions, static field access instructions, and virtual method call instructions, including:
[0149] 1) If the constant pool index of an instance field access instruction is within one byte, then an instance field access instruction with a corresponding single-byte constant pool index is generated.
[0150] 2) If the constant pool index of a static field access instruction is within one byte and is a static field access instruction of the package reference type, then a static field access instruction with a corresponding single-byte constant pool index is generated.
[0151] 3) If the constant pool index of a virtual method call instruction is within one byte, then a virtual method call instruction with a corresponding single-byte constant pool index is generated.
[0152] 4) Instructions for generating corresponding single-byte constant pool indices for static method call instructions, including:
[0153] 4.1) Based on the statistical number of times each library package's static methods are called in a large number of applications, generate a list of commonly used library package static methods and put it into the configuration file. The number of static methods in the list of commonly used library package static methods is less than 256 and has a complete Java method name.
[0154] Specifically, the number of times each library package's static method is called is sorted from highest to lowest. If the total number of static methods in all libraries exceeds 256, the top 256 static methods are added to the list of commonly used static methods. If the number is less than 256, all static methods are added directly to the list of commonly used static methods.
[0155] 4.2) If the constant pool index of a static method call instruction of a library package is within one byte range, then query the configuration file to confirm whether the static method of the library package corresponding to the static method call instruction exists in the list of commonly used static methods of the library package.
[0156] 4.3) If it is confirmed that the static method of the library package exists in the list of commonly used library package static methods, then generate the library package static method call instruction with the corresponding single-byte constant pool index.
[0157] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, this invention is also intended to include these modifications and variations.< / init>
Claims
1. A method for simplifying bytecode instruction sets, characterized in that, Applicable to operand stack-based and register-based instruction sets, the simplification method includes the following steps: S100. Based on different reference types, the constant pool is divided into multiple corresponding sub-constant pools; S200. Based on the number of references for each reference type, count the number of times the instruction corresponding to each reference type appears, and put them into the corresponding sub-constant pool in descending order. The constant pool index of the instruction corresponding to each reference type is the index of the sub-constant pool corresponding to each reference type. S300. Based on the generation conditions of the instruction corresponding to each reference type, generate the instruction for the corresponding single-byte constant pool index.
2. The simplification method according to claim 1, characterized in that, The reference types include class references, static method references, virtual method references, super method references, static field references, and instance field references. The multiple sub-constant pools include: the class sub-constant pool, the static method sub-constant pool, the virtual method sub-constant pool, the super method sub-constant pool, the static field sub-constant pool, and the instance field sub-constant pool.
3. The simplification method according to claim 2, characterized in that, S200 includes: If the number of references to the instance field exceeds 256, the number of times the instance field access instruction of each reference type appears is counted, and the instructions are placed into the sub-constant pool of the instance field in descending order of the number of occurrences; otherwise, they are placed directly into the sub-constant pool of the instance field. If the number of references to the static field exceeds 256, the number of times the static field access instructions of the reference types defined in the package appear is counted, and they are placed into the sub-constant pool of the static field in descending order of the number of occurrences; otherwise, they are directly placed into the sub-constant pool of the static field. If the number of references to the virtual method exceeds 256, the number of times each virtual method call instruction appears is counted, and the virtual method is placed into the sub-constant pool of the virtual method in descending order of the number of times it appears; otherwise, it is placed directly into the sub-constant pool of the virtual method. If the number of references to the static method exceeds 256, first count the number of times the static method instruction appears in the calling library package, and put it into the sub-constant pool of the static method in descending order of the number of times. Then count the number of times the static method instruction appears in each calling package, and put it into the sub-constant pool of the static method in descending order of the number of times. Otherwise, put it directly into the sub-constant pool of the static method.
4. The simplification method according to claim 3, characterized in that, The number of occurrences of static field access instructions of the reference types defined in the statistics package are placed into the sub-constant pool of the static field in descending order of frequency, including: The number of times static field access instructions of reference types within the count package are counted, and they are placed at the beginning of the sub-constant pool of the static field in descending order of frequency.
5. The simplification method according to claim 3, characterized in that, The S300 includes: If the constant pool index of an instance field access instruction is within one byte, then an instance field access instruction with a corresponding single-byte constant pool index is generated. If a static field access instruction has a constant pool index within one byte and is a static field access instruction of the in-package reference type, then a static field access instruction with a corresponding single-byte constant pool index is generated. If the constant pool index of a virtual method call instruction is within one byte, then a virtual method call instruction with a corresponding single-byte constant pool index is generated.
6. The simplification method according to claim 3, characterized in that, The S300 also includes: Based on the statistical count of the number of times each library package's static method is called in a large number of applications, a list of commonly used library package static methods is generated and placed in a configuration file. The number of library package static methods in the list of commonly used library package static methods is less than 256 and has a complete Java method name. If the constant pool index of a static method call instruction of a library package is within one byte range, then the configuration file is queried to confirm whether the static method of the library package corresponding to the static method call instruction of the library package exists in the list of commonly used static methods of the library package; If it is confirmed that the static method of the library package exists in the list of commonly used static methods of the library package, then the library package static method call instruction with the corresponding single-byte constant pool index is generated.
7. The simplification method according to claim 6, characterized in that, The method generates a list of commonly used static methods from various library packages based on the statistical number of times each static method is called in a large number of applications, and stores it in a configuration file. This list includes: The static methods of each library package are sorted from highest to lowest based on the number of times they are called in a large number of applications. If the total number of static methods of all library packages exceeds 256, the top 256 static methods of the library packages are added to the list of commonly used static methods of the library packages. If the number is less than 256, all static methods of the library packages are added to the list of commonly used static methods of the library packages.
8. A bytecode instruction set simplification system, characterized in that, The reduced system, applicable to operand stack-based and register-based instruction sets, includes: The constant pool partitioning module is used to divide the constant pool into multiple corresponding sub-constant pools based on different reference types. The constant pool component generation module is used to count the number of times the instruction corresponding to each reference type appears based on the number of references of each reference type, and put them into the corresponding sub-constant pool in descending order. The constant pool index of the instruction corresponding to each reference type is the index of the sub-constant pool corresponding to each reference type. The instruction generation module is used to generate instructions with corresponding single-byte constant pool indices based on the instruction generation conditions for each reference type.
9. The simplified system according to claim 8, characterized in that, The reference types include class references, static method references, virtual method references, super method references, static field references, and instance field references. The multiple sub-constant pools include: the class sub-constant pool, the static method sub-constant pool, the virtual method sub-constant pool, the super method sub-constant pool, the static field sub-constant pool, and the instance field sub-constant pool.
10. The simplified system according to claim 9, characterized in that, The constant pool component generation module is specifically used for: If the number of references to the instance field exceeds 256, the number of times the instance field access instruction of each reference type appears is counted, and the instructions are placed into the sub-constant pool of the instance field in descending order of the number of occurrences; otherwise, they are placed directly into the sub-constant pool of the instance field. If the number of references to the static field exceeds 256, the number of times the static field access instructions of the reference type in the package are counted, and they are placed into the sub-constant pool of the static field in descending order of the number of times; otherwise, they are placed directly into the sub-constant pool of the static field. If the number of references to the virtual method exceeds 256, the number of times each virtual method call instruction appears is counted, and the virtual method is placed into the sub-constant pool of the virtual method in descending order of the number of times it appears; otherwise, it is placed directly into the sub-constant pool of the virtual method. If the number of references to the static method exceeds 256, first count the number of times the static method instruction appears in the calling library package, and put it into the sub-constant pool of the static method in descending order of the number of times. Then count the number of times the static method instruction appears in each calling package, and put it into the sub-constant pool of the static method in descending order of the number of times. Otherwise, put it directly into the sub-constant pool of the static method.