Class structure compilation optimization method for transaction strategy backtesting

By adopting class structure compilation optimization method in the database system DolphinDB, the efficiency problems caused by dynamic language and hash table storage form are solved, and more efficient transaction strategy backtesting is achieved.

CN119668624AInactive Publication Date: 2025-03-21DOLPHINDB INC (CN)
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202510179837.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-19
Publication Date
2025-03-21
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

The existing database system DolphinDB is insufficient in the operational efficiency and coding efficiency in the backtest of trading strategy, and the dynamic language characteristics lead to large overhead of type checking, and the hash table storage form is not conducive to frequent access.

Method used

The class structure compilation optimization method is adopted, and the backtest information is converted from a hash table to a class structure through syntax parsing. The class structure is used to complete the mapping of scripts to numerical memory addresses in the syntax parsing stage, combining instant compilation technology and type derivation to optimize member access.

Benefits of technology

It significantly improves the computing efficiency of the database system DolphinDB in policy backtesting, reduces the overhead of type checking and member access, and improves the cache hit rate and running speed.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119668624A_ABST
    Figure CN119668624A_ABST
Patent Text Reader

Abstract

The invention discloses a class structure compiling optimization method for transaction strategy backtesting, and belongs to the technical field of databases, source codes are decomposed into minimum meaningful lexical units through grammar analysis, and lexical unit sequences are combined into an abstract syntax tree with a hierarchical structure according to grammar rules; an intermediate code is generated according to the abstract syntax tree, member access is optimized through type pushing, specifically, a large number of virtual methods exist in a class achieved by an interpreter, and virtual method calling is converted into direct method calling through type derivation on the basis of type information; compiling the intermediate code to obtain a machine code; executing the machine code to obtain a machine instruction; a global member variable access cache is added, a member variable in the access request is searched firstly, the found member variable is returned, and if the found member variable is not found, the global member variable access cache is updated, and the member variable is returned; executing the interpreter to obtain a machine instruction; and the operating system receives and executes the machine instruction.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of databases, and in particular relates to a class structure compilation optimization method for trading strategy backtesting. Background Art

[0002] In the financial field, strategy backtesting evaluates the performance of investment strategies by simulating historical data, thereby helping researchers verify and iterate strategies. Fast and efficient replay strategies can help researchers get feedback on strategy performance more quickly, thereby making more timely decisions. This timely feedback helps researchers respond quickly to market changes in actual transactions, improve the quality and timeliness of decision-making, and thus maintain competitiveness in the highly competitive financial market.

[0003] Because the database system DolphinDB provides a rich function library for the development of event-driven strategies, and its syntax is concise and easy to learn, it is suitable for trading researchers who are not professional developers to develop strategies.

[0004] The trading data used in trading strategy backtesting has the characteristics of fixed structure and clear field types. The trading market stipulates the fields contained in the trading data and the corresponding numerical types of the fields, such as time, current price, and number of traded items. The frequency of access to this data information in trading may reach millions or even tens of millions. The trading strategy code is built around this data and will not change at runtime after the strategy is built.

[0005] Although the database system DolphinDB can greatly improve development efficiency, due to the characteristics of the dynamic language of DolphinDB scripts and the process-oriented programming paradigm, the operating efficiency and coding efficiency cannot fully meet the business needs of trading strategy backtesting. The dynamic language characteristics require type checking before calling the DolphinDB calculation link. The lack of class syntax means that the backtest data can only be stored in a dictionary in the form of a hash table. Since the types of the various fields of a single backtest data are not uniform, the hash table can only record the data address instead of metadata, and accessing the hash table requires a second jump to obtain the metadata. Because the hash table is not an ordered storage data structure, its cache hit rate is low. Therefore, the storage format of the hash table is not conducive to frequent access to the backtest system.

[0006] Combined with the trading strategy backtesting scenario, the data structure of trading information is fixed, the strategy is clear, and the core function revolves around the characteristics of trading data. The present invention proposes to use the class structure compilation optimization method to improve the operating efficiency of trading strategy backtesting. By introducing the class, the fields and metadata are bound during syntax parsing to avoid secondary jumps; secondly, the data in the class is stored in order, which is conducive to cache consistency. Since the frequently called strategy functions have been determined before running, the backtest data fields used and their types have also been determined, the strategy functions can be compiled and then executed using just-in-time compilation technology. During the compilation process, compilation optimization technologies such as type inference and loop unrolling can be introduced to further improve the program running efficiency. Summary of the invention

[0007] In order to solve the shortcomings of the prior art and achieve the purpose of improving the backtesting efficiency of event-driven trading strategies, the present invention adopts the following technical solutions: A class structure compilation optimization method for trading strategy backtesting includes the following steps: Step 1: Syntax parsing: Decompose the source code into the smallest meaningful lexical units, combine the lexical unit sequence into an abstract syntax tree with a hierarchical structure according to the grammatical rules of the database system DolphinDB, and perform JIT compilation based on the JIT identifier. In this step, the present invention adds grammatical parsing of the class structure on the basis of the DolphinDB scripting language, and converts the backtest information from the hash table storage form to the class structure storage form. The advantage of the class structure over the hash table is that the mapping of the script to the numerical memory address can be completed in the syntax parsing stage, while the hash table needs to be known at the time of execution; Step 2: Code generation: Generate intermediate code based on the abstract syntax tree so that the compiler can perform optimization. In the process of intermediate code generation, optimize member access through type inference. Specifically, in order to support dynamic types, there are a large number of virtual methods in the classes implemented by the database system DolphinDB interpreter. These virtual methods will be called frequently in strategy backtesting. Perform type inference on the database system DolphinDB, and convert virtual method calls into direct method calls through type information, thereby improving performance. Step 3: Compile; compile the intermediate code to obtain machine code; Step 4: Execute the machine code to get the machine instructions. Since not all variables can be derived into specific types, member access still requires calling functions. Member variables will be frequently accessed in strategy backtesting, which brings great overhead. By adding a global member variable access cache, first search for the member variables in the access request and return the found ones. If not found, update the global member variable access cache and return the member variable, thereby optimizing the member variable access cache.

[0008] Furthermore, in step 1, the syntax of the database system DolphinDB includes a top-level syntax structure and an object syntax structure, wherein the top-level syntax structure is used to describe the behavior framework of the source code, and the object syntax structure is used to describe the specific behavior of the source code.

[0009] Furthermore, the top-level syntax statements of the top-level syntax structure include RETURN variable return, ASSIGN assignment, IF judgment, FOR loop, DO loop, BREAK loop exit, and CONTINUE loop skip; The RETURN is used to return variables; The ASSIGN assignment is used for assignment between variables; The IF judgment executes the first statement when a specific condition is met, otherwise, the second statement is executed; The FOR loop determines whether the loop continues to execute through the loop condition. If not, it jumps to the next step of the loop. Otherwise, after executing a specific statement, it updates the loop variable and performs the next loop. The DO loop first executes a specific statement, and then determines whether the loop condition is met based on the execution result. If it is met, the loop continues to be executed, otherwise, it jumps to the next step of the loop; The BREAK is used to jump out of the loop and to jump out of the current loop statement; The CONTINUE skip loop is used to skip the execution of the current round of loop statements.

[0010] Furthermore, the statements of the grammatical structure of the object include VAR variable declaration, EXPRESSION calculation expression, CONSTOBJ constant definition, FUNCTION function call, TUPLE tuple definition, DIM dimension definition, ATTR object member acquisition, METHODCALL object method call, SELF object return, and CLASS class definition; The VAR variable declaration is used to declare variables; The EXPRESSION calculation expression is used for the calculation of the expression; The CONSTOBJ constant definition is used to define constants; The FUNCTION function call is used to call a function; The TUPLE tuple definition is used to define the tuple members of the tuple; The DIM dimension definition is used to define the dimension; The ATTR object member acquisition is used to acquire members of an object; The METHODCALL object method call is used to call the method of the object; The SELF object is returned to return the object itself; The CLASS class definition is used to declare a class.

[0011] Furthermore, in step 2, type derivation includes type initialization, type transfer, and type determination; The type initialization and type deduction are based on the types of all immutable objects; the types of immutable objects include: immutable input parameters of functions; constants (including constant expressions); class members; function return values; the types of immutable input parameters are determined when the function is called, and through the lazy compilation mechanism, compilation is performed when the function is called; the types of constants are immutable and can be determined after the syntax parsing is completed; the properties of class members are clearly stated in the class declaration and cannot be changed; the type of the function return value is determined by building a mapping from the function name to the return value type; The type transfer is a part of the value transfer. The type determination is that a variable may be assigned multiple times and may be of different types in different contexts; if the type of the variable is unique in the current context, the corresponding type optimization is performed; if it is not unique, no optimization is performed.

[0012] Furthermore, in the source code of the database system DolphinDB, only the ASSIGN statement will execute the transfer of values, and therefore only the ASSIGN statement will transfer types.

[0013] Furthermore, in step 2, when code is generated, each member variable is stored as 8 bytes to ensure alignment with the size of the operating system cache line to reduce the probability of cache misses; for unstructured members, they are accessed in the form of pointers. Modern CPUs usually access memory in 4-byte or 8-byte format. If the data is not aligned, the CPU may need to access the memory multiple times to read the complete data, resulting in performance degradation. Aligned data is more likely to be hit by the CPU cache, reducing the probability of cache misses and thus improving access efficiency.

[0014] Furthermore, in step 4, a global member variable cache is added between the member variable access request and the accessed object space, and the global member variable cache is searched by the member variable name requested for access. If a hit is found, the member variable in the hit cache is returned; if a hit is not found, the accessed object space is searched, the member variable is returned and the global member variable cache is updated.

[0015] Furthermore, the method further comprises: Step 5: Interpreter execution: For functions that need to be executed by the interpreter, the corresponding source code is called back through JIT, and the called-back source code is executed by the database system DolphinDB interpreter to obtain machine instructions; Step 6: Operating system execution; the operating system receives and executes the machine instructions obtained in steps 4 and 5.

[0016] Furthermore, the JIT script code of the database system DolphinDB uses classes instead of dictionaries in strategy backtesting. With the help of class declarations, it obtains type information of fields in the class during JIT compilation, performs JIT optimization on the class structure, and improves the efficiency of the script program, thereby improving the user's backtesting efficiency. It greatly improves the running speed without affecting the efficiency of backtesting strategy development.

[0017] The advantages and beneficial effects of the present invention are: The present invention uses JIT technology to dynamically compile database system DolphinDB scripts to improve program efficiency; performs JIT optimization on the class structure of DolphinDB around strategy backtesting, and proposes corresponding solutions to problems such as system cache hits, frequent member variable accesses, and low efficiency of virtual method calls. Ultimately, the computational efficiency of the database system DolphinDB class structure in strategy backtesting is significantly improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Figure 1 It is a schematic diagram of the basic architecture of the strategy backtesting platform in an embodiment of the present invention.

[0019] Figure 2 It is a flowchart of JIT compilation of DolphinDB script of the database system in an embodiment of the present invention.

[0020] Figure 2a It is a schematic diagram of the syntax tree of the top-level syntax statement RETURN in an embodiment of the present invention.

[0021] Figure 2b It is a schematic diagram of the syntax tree of the top-level syntax statement ASSIGN in an embodiment of the present invention.

[0022] Figure 2c It is a syntax tree diagram of the top-level syntax statement IF in an embodiment of the present invention.

[0023] Figure 2d It is a schematic diagram of a syntax tree of a top-level syntax statement FOR in an embodiment of the present invention.

[0024] Figure 2e It is a schematic diagram of the syntax tree of the top-level syntax statement DO in the embodiment of the present invention.

[0025] Figure 2fIt is a syntax tree diagram of the object syntax statement EXPRESSION in the embodiment of the present invention.

[0026] Figure 2g It is a syntax tree diagram of the object syntax statement FUNCTION in an embodiment of the present invention.

[0027] Figure 2h It is a syntax tree diagram of the object syntax statement ATTR in an embodiment of the present invention.

[0028] Figure 2i It is a syntax tree diagram of the object syntax statement METHODCALL in an embodiment of the present invention.

[0029] Figure 2j It is a schematic diagram of the syntax number of the object syntax statement CLASS in the embodiment of the present invention.

[0030] Figure 3 It is a flow chart of caching global member variables in an embodiment of the present invention. DETAILED DESCRIPTION

[0031] The specific implementation of the present invention is described in detail below in conjunction with the accompanying drawings. It should be understood that the specific implementation described here is only used to illustrate and explain the present invention, and is not used to limit the present invention.

[0032] Strategy backtesting is a method used in the financial field to evaluate the effectiveness of investment strategies. It simulates the strategy's past performance through historical data to predict its future performance. By simulating the strategy's execution with historical data, investors can evaluate the strategy's performance, manage risks, and optimize strategy parameters.

[0033] The basic structure of a strategy backtesting platform is as follows: Figure 1 As shown, it mainly includes three links: 1. Market data playback. When the strategy is used in actual trading, the program that processes real-time data is usually event-driven. In order to use the same set of strategy logic for backtesting and real trading, the strategy backtesting platform generally needs to obtain historical market data in batches and inject it into the backtesting engine in strict chronological order.

[0034] 2. Simulated order matching. The matching of backtest and real trading orders is different, which will lead to poor results of the strategy in actual trading. In order to ensure that the matching of the strategy's entrusted orders simulates the order matching in real trading as much as possible, a simulated matching system is introduced in the medium and high frequency backtesting process.

[0035] 3. Strategy development and strategy backtesting performance evaluation. When designing medium- and high-frequency trading strategies, some indicators, models or machine learning methods are usually used to assist in judging market trends. These indicator calculations and models require a rich function library.

[0036] The present invention uses JIT (Just-In-Time) compilation technology to improve the running speed of the script without affecting the development efficiency. JIT compilation is a technology that dynamically compiles and optimizes code when the program is running. This technology is mainly used to improve the execution efficiency of programs in virtual machines and interpreters. The JIT compiler converts bytecodes into machine codes when the program is running. During the compilation process, the JIT compiler will optimize according to the runtime behavior of the program. Since the JIT compiler can optimize according to the actual runtime data and behavior, the execution efficiency of the program can be significantly improved.

[0037] When processing unstructured data structures such as dictionaries, JIT compilation technology cannot use runtime information to improve operating efficiency. In strategy backtesting, dictionary structures are frequently used to transmit transaction information. Table 1 shows some of the fields contained in the msg structure used in option scenario strategy backtesting. The fields contained in the dictionary structure used in the strategy backtesting scenario are clear. Based on this feature, the present invention uses a class instead of a dictionary, and with the help of the class declaration, the type information of the fields in the class can be obtained during JIT compilation.

[0038] Table 1 Partial fields of the msg structure of the strategy backtesting scenario

[0039] The present invention is to improve the execution speed of the database system DolphinDB script in the strategy backtesting scenario, break through the limitations of the existing backtesting framework, propose a class structure compilation optimization method for trading strategy backtesting, adopt a JIT compiler to improve the efficiency of event-driven trading strategy backtesting, implement a JIT version of the backtesting operation, and allow users to use a convenient scripting language to define classes, thereby implementing strategy backtesting, performing JIT optimization on the class structure, improving the efficiency of the script program, and thus improving the user's backtesting efficiency. This method greatly improves the running speed without affecting the efficiency of backtesting strategy development.

[0040] like Figure 2 The figure shows the overall process of JIT script from JIT compilation to execution of the database system DolphinDB, which includes the following steps: Step 1: Syntax parsing; Input: DolphinDB source code Output: Abstract syntax tree The syntax parsing module first breaks down the source code into the smallest meaningful units, called lexical units or tokens. Next, according to the grammatical rules of the database system DolphinDB, it combines the lexical unit sequences into a hierarchical structure, namely the abstract syntax tree. Then, it decides whether to perform JIT compilation based on whether there is a JIT identifier in the script.

[0041] The syntax of the database system DolphinDB is divided into two parts: one is the top-level syntax structure, and the other is the object syntax structure. The top-level syntax structure describes the behavior framework of the source code, and the object syntax structure describes the specific behavior of the source code.

[0042] As shown in Table 2, there are currently 7 top-level syntax structures, namely: RETURN statement, ASSIGN statement, IF statement, FOR statement, DO statement, PROCEDURE statement, ADHOC statement, BREAK statement, and CONTINUE statement.

[0043] Table 2 Top-level syntax directory

[0044] As shown in Table 3, the grammatical structure of an object has 9 Table 3 Object syntax directory

[0045] Step 2: Code generation; Input: Abstract syntax tree Output: LLVM intermediate code The purpose of the syntax design of the database system DolphinDB is to facilitate customers to quickly and effectively learn and practice. Since such a form is inconvenient for computers to process, it is necessary to generate intermediate code from the source code so that the compiler can perform optimization.

[0046] Some optimizations are added during code generation to generate more efficient code: (1) Member variable field alignment In the operating system, the read and write efficiency of the cache is much higher than that of the memory, so the rational use of the system cache is the key to improving the operating efficiency. When the code is generated, each member variable is stored as 8 bytes to ensure alignment with the size of the system cache line to reduce the probability of cache misses. Unstructured members are accessed in the form of pointers.

[0047] Modern CPUs usually access memory in 4-byte or 8-byte blocks. If the data is not aligned, the CPU may need to access the memory multiple times to read the complete data, resulting in performance degradation. Aligned data is more likely to be hit by the CPU cache, reducing the probability of cache misses and thus improving access efficiency.

[0048] After testing (see Table 4 for the test environment), the efficiency of aligned access is about 30% higher than that of unaligned access.

[0049] (2) Type inference optimizes member access In order to support dynamic types, there are a large number of virtual methods in the classes implemented by the database system DolphinDB interpreter, such as variable addition, subtraction, multiplication and division, vector subscript access, etc. These operations will be called frequently in strategy backtesting. The present invention proposes to perform type deduction on the database system DolphinDB, and convert virtual method calls into direct method calls through type information, thereby improving performance.

[0050] The entire process of type deduction is divided into three parts: type initialization, type transfer, and type determination.

[0051] Type initialization: The basis of type deduction is the type of all immutable objects. Types of immutable objects include: 1) immutable input parameters of functions; 2) constants (including constant expressions); 3) class members; 4) function return values. The type of immutable input parameters has been determined when the function is called, so through the lazy compilation mechanism, compilation can be performed when the function is called. The types of constants are all immutable, so they can be determined after the syntax parsing is completed. The properties of class members are clearly stated to be unchangeable when the class is declared. In order to utilize the type of the function return value, the present invention constructs a mapping from function name to return value type.

[0052] Type transfer: Type transfer is a part of value transfer. Currently, in the DolphinDB database system source code, only the ASSIGN statement can perform value transfer, and therefore only the ASSIGN statement can perform type transfer.

[0053] Type determination: A variable may be assigned multiple times, and may be of different types in different contexts. If the type of the variable is unique in the current context, the corresponding type optimization is performed. If it is not unique, no optimization is performed.

[0054] The overall efficiency of the code after type inference optimization is improved by about 50% compared to the unoptimized code.

[0055] Step 3: LLVM compilation; Input: Intermediate code Output: machine code After code generation, the source code of the database system DolphinDB is converted into LLVM intermediate code that can be compiled by LLVM. With the help of the LLVM compiler, it finally generates executable machine code.

[0056] Step 4: Machine code execution; Input: Machine code Output: Machine instructions Since not all variables can be derived into specific types, member access still requires calling functions. Member variables are frequently accessed in strategy backtesting, which brings huge overhead. The present invention optimizes member variable access cache, such as Figure 3 As shown in the figure, using the principle of locality, a global member variable access cache is added during execution. The cache system will search for the member variable name in the access request. If it hits, the corresponding member variable is directly returned; if it does not hit, the information in the cache is updated first, and then the member variable is returned.

[0057] Step 5: Database system DolphinDB interpreter executes; Input: DolphinDB source code Output: Machine instructions For some functions that need to be executed with the help of the original interpreter, JIT will call back this part of the code, and the called-back code will be executed by the database system DolphinDB interpreter.

[0058] Step 6: Operating system execution; Input: Machine instructions Output: None The operating system receives the execution of the interpreter or machine code and executes it accordingly.

[0059] Example: Combined with a trading strategy database system DolphinDB script, the entire optimization execution process is explained. First, the script encapsulates the transaction information into a class, which contains indicators of transaction data, such as the lowest transaction amount, the highest transaction amount, and the transaction timestamp. The member type needs to be specified when declaring a member. The value of the class instance is sent by the upstream data.

[0060] @jit class Context { buySignalRSI :: DOUBLE sellSignalRSI :: DOUBLE closeLine :: DOUBLE lowPrice :: DOUBLE highPrice :: DOUBLE tradeTime :: TIMESTAMP tradeDate :: DATE barTime :: TIMESTAMP engine :: LONG def Context() { ... } } def onBar(mutable contextDict, indicator){ msg = getMsg() length = msg.size() lowDict = contextDict.lowPrice highDict = contextDict.highPrice istock = msg.symbol price=msg.close atr= indicator.ATR matr=indicator.mATR rsi=indicator.RSI if (atr>matr and rsi>contextDict.buySignalRSI and matr>0){ continue } if (atr>matr and rsi<contextDict.sellSignalRSI and matr>0){ continue } if (price>highDict) { highDict=max(price, highDict) } if (price<lowDict) { lowDict=min(price, lowDict) } contextDict.lowPrice = lowDict contextDict.highPrice = highDict } After defining the class of trading information, researchers can write their own strategies for backtesting. The above is a code example of a trading strategy. In this example, an onBar function is defined, and a transaction is completed in the onBar function. The main variables are: contextDict is an instance of the Context class; msg is the return value of the internal function getMsg, and the return value type of getMsg has been registered in the database system DolphinDB memory; indicator is an instance of an unknown class. The definition of the Context class and the return type of getMsg are known, so type inference optimization can be performed, while the indicator type is unknown, so type inference optimization is not performed.

[0061] 1. Syntax parsing: The above database system DolphinDB source code is passed into the syntax parsing module, and an abstract syntax tree is generated according to the above syntax rules. The top-level statements mainly included in the example of the present invention are IF, ASSIGN and CONTINUE statements. Object statements include VAR, EXPRESSION, ATTR, and FUNCTION statements.

[0062] 2. Code generation. Corresponding code is generated for each syntax unit. Type deduction: In the example of the present invention, type deduction only occurs on the variable msg, and the type of msg is passed by the function getMsg(). Member access optimization: Because the type is known, the ATTR statements about contextDict and msg can be optimized. By looking up the table, it can be known that the accessed member is the serial number in the member list. However, the definition of indicator is unknown, and the serial number of the member list cannot be obtained in the code generation stage. The specific type can only be obtained in the execution stage, which brings the overhead of table lookup.

[0063] 3. LLVM compiles. Generates executable machine code and returns a function address pointing to the execution entry of the onBar function.

[0064] 4. Machine code execution. Call the function execution entry returned by LLVM to execute the onBar function. Since the indicator type is unknown, each member access requires a table lookup. In this process, the cache mechanism designed by the present invention can optimize a table lookup operation when the onBar function is called repeatedly.

[0065] 5. Execution by the DolphinDB interpreter of the database system. Internal functions such as min and max cannot be optimized by JIT, so the related execution will enter the DolphinDB interpreter of the database system.

[0066] In the example of the present invention, using the test environment shown in Table 4, the execution time of the JIT-optimized code is only 12% of the execution time of the non-JIT-optimized code. The execution efficiency of the JIT-optimized code without the optimization (innovation) introduced by the present invention is 28% of that of the non-JIT-optimized code.

[0067] Table 4 Test environment information table

[0068] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some or all of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A class structure compilation optimization method for trading strategy backtesting, characterized in that The steps include: Step 1: Syntax parsing: decompose the source code into the smallest meaningful lexical units, and combine the lexical unit sequences into an abstract syntax tree with a hierarchical structure according to the grammatical rules; Step 2: Code generation: Generate intermediate code based on the abstract syntax tree; in the process of intermediate code generation, optimize member access operations through type inference. Specifically, there are a large number of virtual methods in the class implemented by the interpreter. Through type inference, virtual method calls are converted into direct method calls based on type information; Step 3: Compile; compile the intermediate code to obtain machine code; Step 4: Execute the machine code to obtain the machine instruction; by adding a global member variable access cache, first search for the member variable in the access request and return the member variable if found; if not found, update the global member variable access cache and return the member variable.

2. A class structure compilation optimization method for trading strategy backtesting according to claim 1, characterized in that: In the step 1, the syntax of the database system includes a top-level syntax structure and an object syntax structure. The top-level syntax structure is used to describe the behavior framework of the source code, and the object syntax structure is used to describe the specific behavior of the source code.

3. A class structure compilation optimization method for trading strategy backtesting according to claim 2, characterized in that: The top-level syntax statements of the top-level syntax structure include RETURN variable return, ASSIGN assignment, IF judgment, FOR loop, DO loop, BREAK loop jump, CONTINUE loop skip; The RETURN is used to return variables; The ASSIGN assignment is used for assignment between variables; The IF judgment executes the first statement when a specific condition is met, otherwise, the second statement is executed; The FOR loop determines whether the loop continues to execute through the loop condition. If not, it jumps to the next step of the loop. Otherwise, after executing a specific statement, it updates the loop variable and performs the next loop. The DO loop first executes a specific statement, and then determines whether the loop condition is met based on the execution result. If it is met, the loop continues to be executed, otherwise, it jumps to the next step of the loop; The BREAK is used to jump out of the loop and to jump out of the current loop statement; The CONTINUE skip loop is used to skip the execution of the current round of loop statements.

4. A class structure compilation optimization method for trading strategy backtesting according to claim 2, characterized in that: The grammatical structure of the object includes VAR variable declaration, EXPRESSION calculation expression, CONSTOBJ constant definition, FUNCTION function call, TUPLE tuple definition, DIM dimension definition, ATTR object member acquisition, METHODCALL object method call, SELF object return, and CLASS class declaration; The VAR variable declaration is used to declare variables; The EXPRESSION calculation expression is used for the calculation of the expression; The CONSTOBJ constant definition is used to define constants; The FUNCTION function call is used to call a function; The TUPLE tuple definition is used to define the tuple members of the tuple; The DIM dimension definition is used to define the dimension; The ATTR object member acquisition is used to obtain the members of the object The METHODCALL object method call is used to call the method of the object; The SELF object is returned to return the object itself; The CLASS class declaration is used to declare a class.

5. The class structure compilation optimization method for trading strategy backtesting according to claim 1, characterized in that: In step 2, type derivation includes type initialization, type transfer, and type determination; The type initialization and type deduction are based on the types of all immutable objects; the types of immutable objects include: immutable input parameters of functions; constants; class members; function return values; the types of immutable input parameters are determined when the function is called, and through the lazy compilation mechanism, compilation is performed when the function is called; the types of constants are immutable and can be determined after the syntax parsing is completed; the properties of class members are clearly stated in the class declaration and cannot be changed; the type of the function return value is determined by building a mapping from the function name to the return value type; The type transfer is a part of the value transfer. The type determination is that a variable may be assigned multiple times and may be of different types in different contexts; if the type of the variable is unique in the current context, the corresponding type optimization is performed; if it is not unique, no optimization is performed.

6. A class structure compilation optimization method for trading strategy backtesting according to claim 3, characterized in that: In the source code of the database system, only the ASSIGN statement will execute the value transfer, and therefore only the ASSIGN statement will perform the type transfer.

7. A class structure compilation optimization method for trading strategy backtesting according to claim 1, characterized in that: In step 2, when generating code, each member variable is stored to be aligned with the size of the operating system cache line; unstructured members are accessed in the form of pointers.

8. The class structure compilation optimization method for trading strategy backtesting according to claim 1, characterized in that: In step 4, a global member variable cache is added between the member variable access request and the accessed object space, and the global member variable cache is searched by the member variable name requested for access. If a hit is found, the member variable in the hit cache is returned. If a hit is not found, the accessed object space is searched, the member variable is returned and the global member variable cache is updated.

9. A class structure compilation optimization method for trading strategy backtesting according to claim 1, characterized in that: The method further comprises: Step 5: Interpreter execution: For functions that need to be executed by the interpreter, the corresponding source code is called back, and the called-back source code is executed by the interpreter to obtain machine instructions; Step 6: Operating system execution; the operating system receives and executes the machine instructions obtained in steps 4 and 5.

10. A class structure compilation optimization method for trading strategy backtesting according to claim 1, characterized in that: In the strategy backtesting, the JIT script code of the database system uses classes instead of dictionaries. With the help of class declarations, the type information of the fields in the class is obtained during JIT compilation, and the class structure is JIT optimized.

Citation Information

Patent Citations

  • System and method for database query

    CN108027838A

  • First Futamura projection in context of SQL expression evaluation

    CN114341832A

  • Text programming language compiling method based on decoupling architecture

    CN116501330A

  • Program adaptive execution system and method of program language

    CN117806645A