An interface implementation method for an object-oriented language based on class view

CN122593789APending Publication Date: 2026-08-18成都融见软件科技有限公司 +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611063817.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-17
Publication Date
2026-08-18

AI Technical Summary

Technical Problem

虽然已有不少成熟的方法对现有的实现做些优化,比如内联缓存,虚调用转换等,但优化通常都仅针对于特殊场景,对于一般场景并没有特别有效的方法消除额外的开销

Benefits of technology

本发明编译器接收类文件和用户输入文件,对类文件中的每一实现类进行修改以及将用户输入文件中对接口变量的赋值语句进行替换,通过在编译期为每个实现类的每个接口类型预先构建专用虚函数表,并引入类视图,将传统接口调用中每次执行均需进行的接口方法动态查找,转变为接口变量赋值时一次性完成。具体地,赋值时通过将查得的虚表地址直接缓存于类视图的虚表指针域,使得后续所有接口方法调用均可转换为基于虚表指针加固定偏移量的普通虚函数调用,从根本上消除了运行期invokeinterface指令所需的额外指令周期,缩短了接口调用的指令执行路径,提高了接口调用效率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122593789A_ABST
    Figure CN122593789A_ABST
Patent Text Reader

Abstract

This application relates to the field of compilers, and in particular to a method for implementing interfaces in an object-oriented language based on class views. The compiler receives a class file and a user input file, modifies each implementation class in the class file, replaces assignment statements for interface variables in the user input file, and compiles the modified class file and the replaced user input file to obtain a first compilation result. The modification includes adding a virtual function table for all interface types implemented by each implementation class. An executor executes the compilation result corresponding to the class view definition file and the first compilation result. The class view definition file includes a description of a class view object, which includes an object pointer for storing the address of a concrete implementation class object and a virtual table pointer for storing the address of the virtual function table. This invention can improve the efficiency of interface calls.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of compilers, and in particular to a method for implementing interfaces of object-oriented languages ​​based on class views. Background Technology

[0002] An interface is an abstract type in object-oriented programming languages. It defines a set of method signatures but does not provide concrete implementations of these methods. Classes implementing an interface must provide concrete implementations for all abstract methods declared in the interface, ensuring that the class conforms to the contract defined by the interface. Many object-oriented programming languages ​​support this interface syntax, such as the software development languages ​​Java and C#, and the digital circuit verification language System Verilog. While the abstractness of interfaces brings programming flexibility and design elegance, its implementation complexity can also impact runtime performance. Taking Java as an example, each runtime call to an interface function requires first locating the address of the corresponding virtual function in the concrete implementation class based on the interface type, and then calling the function through a function pointer. The common interface function call flow includes: starting the interface method call; executing the invokeinterface instruction; obtaining the object class pointer; obtaining the array of pointers to the class's interface method table (itable); traversing the itable to find the interface; comparing whether the interface ID matches, if a match is found, obtaining the offset of the interface method table, retrieving the method pointer from the offset in the interface method table, executing the method and returning the result, and the call is complete; if a match fails, checking if there are more entries in the itable, if so, continuing the loop to traverse the next interface; if not, throwing an exception, entering the exception handling process, and the call ends.

[0003] Relevant statistics show that the overhead of calling functions through interfaces is 2 to 3 times that of direct calls, and 1.5 to 2 times that of virtual function calls. Although there are many mature methods to optimize existing implementations, such as inline caching and virtual call conversion, these optimizations are usually only for specific scenarios, and there are no particularly effective methods to eliminate the additional overhead for general scenarios. Summary of the Invention

[0004] The purpose of this invention is to provide an interface implementation method for an object-oriented language based on class views, so as to reduce the overhead of interface calls and improve the efficiency of interface calls.

[0005] According to the present invention, a method for implementing an interface of an object-oriented language based on class views is provided, the method comprising the following steps: S100: The compiler receives the class file and the user input file, modifies each implementation class in the class file and replaces the assignment statements of interface variables in the user input file, and compiles the modified class file and the replaced user input file to obtain the first compilation result.

[0006] The modification includes: adding a virtual function table for all interface types implemented by the implementation class in each implementation class; the virtual function table for any interface type implemented by any implementation class includes the addresses of each virtual function corresponding to that interface type in the implementation class; the implementation class is a class in an object-oriented language that provides concrete implementations for all abstract functions declared in the interface.

[0007] The replacement is used to: in response to the creation of a class view object, obtain the address of a concrete implementation class object, and configure the virtual table pointer of the class view object to point to the virtual function table of the concrete implementation class object corresponding to the interface type; in response to a virtual function call instruction for a function index of a target virtual function, read the address of the target virtual function from the virtual function table according to the virtual table pointer and the function index of the target virtual function; and call the address of the target virtual function using the address of the concrete implementation class object as the context parameter for the virtual function call; the user input file is a file written in an object-oriented language.

[0008] S200, the executor executes the compilation result corresponding to the class view definition file and the first compilation result; the class view definition file includes a description of a class view object, and the class view object includes an object pointer for storing the address of a concrete implementation class object and a virtual table pointer for storing the address of a virtual function table.

[0009] Compared with the prior art, the present invention has at least the following beneficial effects: This invention's compiler receives class files and user input files, modifies each implementation class in the class files, and replaces the assignment statements for interface variables in the user input files. By pre-constructing a dedicated virtual function table for each interface type of each implementation class during compilation and introducing a class view, the dynamic lookup of interface methods, which is required for each execution in traditional interface calls, is transformed into a one-time completion during interface variable assignment. Specifically, during assignment, the obtained virtual table address is directly cached in the virtual table pointer field of the class view, so that all subsequent interface method calls can be converted into ordinary virtual function calls based on the virtual table pointer plus a fixed offset. This fundamentally eliminates the extra instruction cycle required by the runtime invokeinterface instruction, shortens the instruction execution path of interface calls, and improves interface call efficiency.

[0010] This invention is particularly suitable for scenarios where interface variables are bound to concrete implementation class objects and remain unchanged for a long period, and where interface methods are called frequently. Because the one-time lookup cost during the assignment phase is amortized infinitely with the number of calls, the performance advantages of this invention accumulate over time, resulting in a system throughput increase several times greater than traditional interface call mechanisms. Furthermore, this invention, through the reassignment mechanism of class views, retains the runtime polymorphism and type safety required for object-oriented interface programming, without sacrificing the flexibility and expressiveness of the programming paradigm. Attached Figure Description

[0011] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0012] Figure 1 A flowchart illustrating the interface implementation method of an object-oriented language based on class views provided in this embodiment of the invention; Figure 2 This is a flowchart of the assignment process during the execution phase provided in an embodiment of the present invention. Detailed Implementation

[0013] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0014] According to this embodiment, a method for implementing an interface of an object-oriented language based on class views is provided. The method includes the following steps, such as... Figure 1 As shown: S100: The compiler receives the class file and the user input file, modifies each implementation class in the class file and replaces the assignment statements of interface variables in the user input file, and compiles the modified class file and the replaced user input file to obtain the first compilation result.

[0015] The modification includes: adding a virtual function table for all interface types implemented by each implementation class; the virtual function table for any interface type implemented by any implementation class includes the addresses of each virtual function corresponding to that interface type in the implementation class; the implementation class is a class in an object-oriented language that provides concrete implementations for all abstract functions declared in the interface. As a specific implementation, the modification further includes: binding the added virtual function table to the corresponding interface type identifier, so that the virtual function table pointed to by the virtual table pointer of the class view object is determined based on the interface type identifier during the stage of determining the virtual function table pointed to by the virtual table pointer of the class view object.

[0016] The replacement is used to: in response to the creation of a class view object, obtain the address of a concrete implementation class object, and configure the virtual table pointer of the class view object to point to the virtual function table of the concrete implementation class object corresponding to the interface type; in response to a virtual function call instruction for a function index of a target virtual function, read the address of the target virtual function from the virtual function table according to the virtual table pointer and the function index of the target virtual function; and call the address of the target virtual function using the address of the concrete implementation class object as the context parameter for the virtual function call; the user input file is a file written in an object-oriented language.

[0017] As a specific implementation, the object-oriented language includes languages ​​such as Java, C#, and SV. In this object-oriented language, the interface is an abstract type and does not provide a concrete implementation. The user input file only includes calls to the interface and assignment statements to interface variables (which can indicate which implementation class to use to implement the interface), and does not include classes that provide concrete implementations for all abstract functions declared in the interface (i.e., implementation classes). The classes that provide concrete implementations for all abstract functions declared in the interface are in the class file.

[0018] As a specific implementation method, the virtual function table of all interface types implemented by a certain implementation class is as follows: class ClassObject { / / implements Intf1, Intf2, ... public: static IntfVTableMap intfVtMap = { { intf1Id, intf1VtPtr}, { intf2Id, intf2VtPtr}, ... }; static VirtualTable intf1VtPtr[] = { vfunc1, vfunc2, ...}; static VirtualTable intf2VtPtr[] = { vfunc3, vfunc4, ...}; ... }; Here, `class ClassObject` indicates that a class named `ClassObject` is defined; `implementsIntf1, Intf2, ...` indicates that the class can implement interface 1, interface 2, etc.; `public` is the public access permission identifier; `static IntfVTableMap intfVtMap` indicates that a static variable `intfVtMap` of type `IntfVTableMap` is defined; `{ intf1Id, intf1VtPtr}` and `{ intf2Id, intf2VtPtr}` are used to bind the type identifier of the interface with the dedicated virtual function table generated for the interface; `static VirtualTable intf1VtPtr[]` indicates that a static array of type `VirtualTable` named `intf1VtPtr` is defined, which stores the function addresses corresponding to interface 1; `static VirtualTable intf2VtPtr[]` indicates that a static array of type `VirtualTable` named `intf2VtPtr` is defined, which stores the function addresses corresponding to interface 2.

[0019] As a specific implementation, the assignment statements for interface variables in the user input file are replaced with preset code, wherein the preset code includes: Code used to create a concrete implementation class object.

[0020] Code used to create a class view object.

[0021] This is used to bind the address of the concrete implementation class object to the inside of the class view object through the assignment function of the class view object, and to make the virtual table pointer inside the class view object point to the code of the virtual function table of the concrete implementation class object corresponding to the interface type.

[0022] This is used to extract the address of the target virtual function from the function virtual table pointed to by the virtual table pointer inside the class view object when the target virtual function needs to be called; and to call the address of the target virtual function as an implicit parameter to execute the code of the target virtual function in the concrete implementation class object.

[0023] As a specific implementation, the preset code is: ClassObject obj1; ClassView intf1; intf1.assignobject(&obj1); / / intf1 = obj1; (*intf1.intfVtPtr_[index_of_vfunc1])(...); / / intf1.vfunc1(); In the code above, `ClassObject obj1` is used to create a concrete implementation class object, where `ClassObject` represents any concrete implementation class, and `obj1` is the name of the concrete implementation class being created; `ClassView` `intf1` is used to create a class view object, where `ClassView` represents the class view and `intf1` is the name of the created class view. `intf1.assignobject(&obj1)` is used to bind the address of the concrete implementation class object to the class view object through its assignment function, and to make the virtual table pointer inside the class view object point to the virtual function table of the concrete implementation class object corresponding to the interface type. `assignobject` is the binding function used to implement the binding from the concrete implementation class object to the class view. `(*intf1.intfVtPtr_[index_of_vfunc1])(...)` is used to extract the address of the target virtual function from the function virtual table pointed to by the virtual table pointer inside the class view object when the target virtual function needs to be called, based on the function index of the target virtual function. It then uses the address of the concrete implementation class object as an implicit parameter to call the address of the target virtual function to execute the target virtual function in the concrete implementation class object. `intfVtPtr` is the virtual table pointer, `index_of_vfunc1` is the function index, and `(*...)(...)` indicates that the retrieved address is used as a function pointer for execution.

[0024] S200, the executor executes the compilation result corresponding to the class view definition file and the first compilation result; the class view definition file includes a description of a class view object, and the class view object includes an object pointer for storing the address of a concrete implementation class object and a virtual table pointer for storing the address of a virtual function table.

[0025] It should be understood that the compiler only obtains an executable compilation result, which is actually executed during the execution phase.

[0026] As a specific implementation, the class view definition file is a file prepared in advance by the compiler designer before the compiler starts compiling. By using the compiler to compile the class view definition file, the compilation result corresponding to the class view definition file can be obtained.

[0027] In this embodiment, the class view acts as a proxy for some methods of a concrete implementation class. Through the class view, only a set of functions defined in the corresponding interface of the implementation class can be called as virtual functions. In implementation, the class view is defined as a pure virtual class containing a set of virtual methods. In usage and performance, the class view is equivalent to a pure virtual class. The difference between it and a traditional pure virtual class is that a traditional pure virtual class does not have an entity, but is merely a pointer to the entity class. A class view, however, requires a proxy entity containing a pointer to the implementation class object (i.e., an object pointer) and a pointer to a virtual function table covering the interface functions (i.e., a virtual table pointer). When an implementation class or another interface is assigned to the current interface, the object pointer of the class view's proxy entity points to the specific implementation class. Simultaneously, the virtual function table of the corresponding view is queried through the implementation class, and the table address is filled into the proxy entity. When an interface function is called, the function address is directly obtained by adding the virtual function index to the virtual function table proxied by the interface, and then the function is called.

[0028] In one specific implementation, the class view object also includes a static member for storing an identifier of the interface type. The class view object also provides a binding function. When a pointer to a concrete implementation class object is received, the binding function stores the pointer to the concrete implementation class object in an object pointer, and uses the interface type identifier stored in the static member as a lookup key to retrieve the corresponding virtual function table address from the virtual function table of the concrete implementation class object and stores it in a virtual table pointer. The class view object also provides a proxy binding function. When another type of view object is received, the proxy binding function retrieves the pointer to the concrete implementation class object stored in the object pointer of the other type of view object and calls the binding function to perform binding.

[0029] As a specific implementation method, the class view object is described as follows: class ClassView { / / virtual function: vfunc1, vfunc2 ... public: static InterfaceIdType intfId_; Classobject*objPtr_; VirtualTable*intfVtPtr_; public: void assignobject(Classobject* obj) { objPtr_ = obj; intfVtPtr_ = QueryVirtualTableFromobject(obj, intfId_); } void assignClassView(ClassView* intf) { assignobject(intf->objPtr_); } static VirtualTable* QueryVirtualTableFromObject( Classobject* obj, InterfaceIdType intfId) { return obj->intfVtMap[intfId]; } }; In the code above, `intfId` is a static member of `ClassView`, and its type is `InterfaceIdType`; `objPtr_` is a pointer to an object contained in `ClassView`, and its type is `Classobject*`; `intfVtPtr_` is a pointer to a virtual table contained in `ClassView`, and its type is `VirtualTable*`; `ClassView` contains the functions `assignobject`, `assignClassView`, and `QueryVirtualTableFromObject`; `assignobject` is the binding function, and its parameter is `ClassView* intf`. `assignobject` is used to store the passed-in `obj` into `objPtr_`, and then calls `QueryVirtualTableFromObject` to look up the virtual function table in `obj` based on `intfId_`, and stores the result into `intfVtPtr_`; `assignClassView` is the proxy binding function, and its parameter is `ClassView* intf`. `assignClassView` is used for assignment between interface variables; `QueryVirtualTableFromObject` is the virtual table lookup function, and its parameters are `Classobject* obj` and `intfId`.

[0030] As a specific implementation method, the assignment process during the execution phase includes, for example... Figure 2 As shown: S210: Determine whether the object assigned to the interface variable is a concrete implementation class. If yes, directly obtain the address of the concrete implementation class and proceed to S230; otherwise, proceed to S220.

[0031] S220: Determine whether the object assigned to the interface variable is an interface. If it is, obtain the address of the specific implementation class pointed to by the interface and proceed to S230. If not, determine that the assignment failed and set the interface variable to null, and the assignment ends.

[0032] S230, Generate a class view, store the address of the obtained specific implementation class into the object pointer field of the class view object, and proceed to S240.

[0033] S240: Query the address of the corresponding virtual function table based on the interface type identifier, and store the address of the virtual function table in the virtual table pointer field of the class view object. The assignment ends.

[0034] The assignment process can be completed based on S210-S240.

[0035] In this embodiment, the compiler receives class files and user input files, modifies each implementation class in the class files, and replaces the assignment statements for interface variables in the user input files. By pre-constructing a dedicated virtual function table for each interface type of each implementation class during compilation and introducing a class view, the dynamic lookup of interface methods, which is required for each execution in traditional interface calls, is transformed into a one-time completion during interface variable assignment. Specifically, during assignment, the obtained virtual table address is directly cached in the virtual table pointer field of the class view, so that all subsequent interface method calls can be converted into ordinary virtual function calls based on the virtual table pointer plus a fixed offset. This fundamentally eliminates the extra instruction cycle required by the runtime invokeinterface instruction, shortens the instruction execution path of interface calls, and improves interface call efficiency.

[0036] This embodiment is particularly suitable for scenarios where interface variables are bound to concrete implementation class objects and remain unchanged for a long time, and interface methods are called frequently. Because the one-time lookup cost during the assignment phase is amortized infinitely with the number of calls, the performance advantages brought by this invention have a cumulative effect as program runtime increases, resulting in a system throughput improvement several times greater than traditional interface call mechanisms. Simultaneously, this invention, through the reassignment mechanism of class views, still retains the runtime polymorphism and type safety required for object-oriented interface programming, without sacrificing the flexibility and expressiveness of the programming paradigm.

[0037] While specific embodiments of the invention have been described in detail by way of example, those skilled in the art should understand that the examples are for illustrative purposes only and not intended to limit the scope of the invention. It should also be understood that various modifications can be made to the embodiments without departing from the scope and spirit of the invention. The scope of the invention is defined by the appended claims.

Claims

1. A method for implementing an interface in an object-oriented language based on class views, characterized in that, The method includes the following steps: S100: The compiler receives the class file and the user input file, modifies each implementation class in the class file and replaces the assignment statements of interface variables in the user input file, and compiles the modified class file and the replaced user input file to obtain the first compilation result. The modification includes: adding a virtual function table for all interface types implemented by the implementation class in each implementation class; the virtual function table for any interface type implemented by any implementation class includes the addresses of each virtual function corresponding to that interface type in the implementation class; the implementation class is a class in an object-oriented language that provides concrete implementations for all abstract functions declared in the interface; The replacement is used to implement: in response to the creation of a class view object, obtaining the address of a concrete implementation class object, and configuring the virtual table pointer of the class view object to point to the virtual function table of the concrete implementation class object corresponding to the interface type; in response to a virtual function call instruction for a function index of a target virtual function, reading the address of the target virtual function from the virtual function table according to the virtual table pointer and the function index of the target virtual function; and calling the address of the target virtual function using the address of the concrete implementation class object as the context parameter for the virtual function call; the user input file is a file written in an object-oriented language; S200, the executor executes the compilation result corresponding to the class view definition file and the first compilation result; the class view definition file includes a description of a class view object, and the class view object includes an object pointer for storing the address of a concrete implementation class object and a virtual table pointer for storing the address of a virtual function table.

2. The interface implementation method of an object-oriented language based on class views according to claim 1, characterized in that, Replace the assignment statements of interface variables in the user input file with preset codes, wherein the preset codes include: Code used to create a concrete implementation class object; Code used to create a view object; Used to bind the address of the concrete implementation class object to the inside of the class view object through the assignment function of the class view object, and to make the virtual table pointer inside the class view object point to the code of the virtual function table of the concrete implementation class object corresponding to the interface type; This is used to extract the address of the target virtual function from the function virtual table pointed to by the virtual table pointer inside the class view object when the target virtual function needs to be called; and to call the address of the target virtual function as an implicit parameter to execute the code of the target virtual function in the concrete implementation class object.

3. The interface implementation method of an object-oriented language based on class views according to claim 1, characterized in that, The class view object also includes a static member for storing the identifier of the interface type. The class view object also provides a binding function. When the binding function receives a pointer to a concrete implementation class object, it stores the pointer to the concrete implementation class object into an object pointer, and uses the interface type identifier stored in the static member as the lookup key to look up the corresponding virtual function table address from the virtual function table of the concrete implementation class object and stores it into the virtual table pointer. The class view object also provides a proxy binding function. When receiving another class view object, the proxy binding function extracts the pointer to the specific implementation class object stored in the object pointer of the other class view object and calls the binding function to perform binding.

4. The interface implementation method of an object-oriented language based on class views according to claim 1, characterized in that, The object-oriented languages ​​include Java, C#, and SV.

5. The interface implementation method of an object-oriented language based on class views according to claim 1, characterized in that, The modification also includes binding the added virtual function table to the corresponding interface type identifier, so as to determine the virtual function table pointed to by the virtual table pointer of the class view object according to the interface type identifier during the stage of determining the virtual function table pointed to by the virtual table pointer of the class view object.

6. The interface implementation method of an object-oriented language based on class views according to claim 2, characterized in that, The assignment process during the execution phase includes: S210: Determine whether the object assigned to the interface variable is a concrete implementation class. If yes, directly obtain the address of the concrete implementation class and proceed to S230; otherwise, proceed to S220. S220: Determine whether the object assigned to the interface variable is an interface. If it is, obtain the address of the concrete implementation class pointed to by the interface and proceed to S230. If not, determine that the assignment failed and set the interface variable to null, and the assignment ends. S230, Generate a class view, store the address of the obtained specific implementation class into the object pointer field of the class view object, and proceed to S240; S240: Query the address of the corresponding virtual function table based on the interface type identifier, and store the address of the virtual function table in the virtual table pointer field of the class view object. The assignment ends.

7. The interface implementation method of an object-oriented language based on class views according to claim 1, characterized in that, In the object-oriented language, the interface is an abstract type and does not provide a concrete implementation.