Optimization method for multi-parameter function calling
By storing redundant parameters in memory and using their first addresses as new parameters, the problem of repeated storage and reading in function calls is solved, thereby improving the efficiency of function calls.
Patent Information
- Application Number
- CN202410285065.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-13
- Publication Date
- 2025-09-16
AI Technical Summary
In the prior art, when the number of function parameters is greater than four, repeated storage and reading operations occur, resulting in low function call efficiency.
Store the redundant parameters in the memory space, and use the first address of the memory space as a new parameter in the function call, thereby reducing the number of parameters that need to be passed when calling the function.
It reduces the number of parameter passing times when calling a function, eliminates the repeated storage and reading of redundant parameters, and improves the efficiency of function calling.
Smart Images

Figure CN120653329A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer image processing, and in particular relates to an optimization method for calling a multi-parameter function. Background Art
[0002] In the prior art, with the development of science and technology, especially the leap of artificial intelligence technology, the widespread application of computer programs has increasingly entered people's lives. Among them, including in programs such as image detection, image recognition, and image processing, the function body is executed by calling the function, and its process is similar to the subroutine call in other languages. Currently, image processing applications are more commonly used. Although other aspects are less used, they can also be applied. Image processing is used as an example here. For example, in the C language involved in image processing, the general form of function call is: function name (actual parameter list) There is no actual parameter list when calling a function without parameters. The parameters in the actual parameter list can be constants, variables, or other constructed type data and expressions. Each actual parameter is separated by a comma.
[0003] Its general form is:
[0004] Type specifier called function name (type parameter, type parameter...);
[0005] Or:
[0006] type specifier called_function_name(type, type...);
[0007] The parameter type and parameter name, or just the parameter type, are given in parentheses. This makes it easier for the compiler to perform error checking and prevent possible errors.
[0008] When a function call occurs in the program, the parameters that the function needs to pass will be placed in the four general registers $4 to $7 in the MIPS general registers according to the function's parameter list. The corresponding relationship is that the 0th parameter in the function parameter list is stored in the $4 general register, the 1st parameter in the function parameter list is stored in the $5 general register, the 2nd parameter in the function parameter list is stored in the $6 general register, and the 3rd parameter in the function parameter list is stored in the $7 general register.
[0009] If the function has more than 4 parameters, the first 4 parameters are stored in the corresponding 4 general registers $4 to $7 in the order of the parameters in the parameter list, and the remaining parameters are stored in a memory space. When the function implementation logic needs to use the parameter, the parameter will be read out from the stored memory space again and then participate in the logic operation.
[0010] After the function is loaded, the function's entry address still needs to be loaded into the general register (usually the $25 register), and then the jump instruction is used to jump to the corresponding code of the function implementation to start executing the logical calculation of the function implementation.
[0011] Finally, after completing the logical calculations implemented by the function, the jump instruction is used to jump back to the position before the function call occurred and continue to execute the code program.
[0012] Therefore, the prior art has the following defects:
[0013] From the above description, we can see that when the function has more than 4 parameters, the parameters except the first 4 parameters will be put into the memory space and then read again within the function. This process involves a repeated storage and reading operation.
[0014] In addition, terms commonly used in the prior art include:
[0015] 1. Type specifier: A symbol that describes a variable in a computer programming language, which can describe the type, length, etc. of the variable.
[0016] 2. General registers: MIPS32 contains 32 general registers, usually $0 to $31 are used to represent the general registers with corresponding numbers.
[0017] 3. Image bad pixels, also known as pixel absence, refer to defects in the display pixels on the LCD screen or image sensor. Summary of the Invention
[0018] In order to solve the above problem, the purpose of this application is to reduce the number of parameters that need to be passed when calling a function by storing redundant parameters (parameters other than the first 4 parameters in the function parameter list) or even all parameters in a memory space, and using the first address of this memory space instead of the parameters originally required to be passed as new parameters in the function call process, so that the number of parameters that need to be passed when calling a function is always <= 4 (MIPS only reserves 4 general registers for parameter passing when calling a function).
[0019] Specifically, the present invention provides a method for optimizing multi-parameter function calls. The method stores all program call parameters or parameters other than the first four parameters in the function parameter list in a memory space, and uses the first address of the memory space to replace the original parameters to be passed as new parameters in the function call process. Assuming that there are N parameters to be passed in the parameter list of function A, and N is greater than 4, the method specifically includes the following steps:
[0020] S1, redefine function A as function B. The parameter list of function B has only one parameter, addr. This addr parameter represents the first address of a memory space. From this address space, the N parameters of function A can be obtained.
[0021] S2, when function B is called, since function B has only one parameter addr, this parameter will be stored in the MIPS general register; when the program executes function B and needs to use the specific parameters of function A, the required specific parameters of function A will be read from the memory pointed to by parameter addr to the MIPS general register, and then participate in the calculation.
[0022] The method assumes that the parameters of function A include p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val, a total of 6 parameters that need to be passed, and specifically includes the following steps:
[0023] S1, redefine function A as function B. The parameter list of function B has only one parameter, addr. This addr parameter represents the first address of a memory space. From this address space, the parameters p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val in function A can be obtained.
[0024] S2, when function B is called, since function B has only one parameter addr, this parameter will be stored in the MIPS general register; when the program executes function B, when the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val are needed, the required parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val will be read from the memory pointed to by the parameter addr to the MIPS general register, and then participate in the operation.
[0025] In the method, an addition function is assumed. The function implemented by function A is to calculate the cumulative sum of the pixel values of the six pixels p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val. Function A needs to pass six parameters. The parameter list and calculation logic of function A are as follows: uint8_t A(uint8_t p0_val, uint8_t p1_val, uint8_t p2_val, uint8_t p3_val, uint8_t p4_val, uint8_t p5_val) {
[0026] return p0_val+p1_val+p2_val+p3_val+p4_val+p5_val;
[0027] };
[0028] The calling method of the function A in the program code is as follows:
[0029] L0: int main(input_ptr) {
[0030] L1: int sum;
[0031] L2: uint8_t*tmp_ptr=(uint8_t*)input_ptr;
[0032] L3: sum=A(tmp_ptr[0],tmp_ptr[1],tmp_ptr[2],tmp_ptr[3],
[0033] tmp_ptr[4],tmp_ptr[5]);
[0034] L4: return sum;
[0035] }
[0036] Reading a 6-byte number from the memory space pointed to by the input_ptr address passed in the main function, and then calculating the cumulative sum of the 6 numbers by calling function A; that is, storing all parameters into the memory; the method further includes:
[0037] S1, modify the function A, and leave only one parameter in the parameter list of A, which is used to represent the first address of the memory storing the parameters. The modified function A is called function B. Then, the parameter list of function B is as follows:
[0038] uint8_t B(uint8_t*arry);
[0039] S2, since the modified function B only passes one parameter set to arry to represent the address, when the original parameters (p0_val, p1_val, p2_val, p3_val, p4_val, p5_val) are needed in the function, the corresponding parameters must be read from the passed memory address and parameter operations must be performed; that is, all places in the original function A that directly use the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val for operations must be replaced with the parameter arry[index] (address + index subscript to obtain the number) in the same position of function B; the other logic of function A that does not directly use the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val remains unchanged in function B, thus obtaining function B with the same function. The implementation of function B is as follows:
[0040] uint8_t B(uint8_t*arry){
[0041] return arry[0]+arry[1]+arry[2]+arry[3]+arry[4]+arry[5];
[0042] }
[0043] Since the number of parameters in the parameter list of function B has been reduced, the code for calling the function in the main function of the main program must also be changed accordingly; that is, the code in line L3 of the main function of the original function A, which originally called function A and passed 6 parameters, is replaced by calling function B and passing 1 parameter. The other irrelevant logical codes remain unchanged, as follows: L0: int main(input_ptr) {
[0044] L1: int sum;
[0045] L2: uint8_t*tmp_ptr=(uint8_t*)input_ptr;
[0046] L3: sum = B (tmp_ptr);
[0047] L4: return sum;
[0048] }.
[0049] The function call can be divided into parameter transfer, function call jump, function logic execution and function jump back. Function B implemented by this method consists of these two parts: parameter transfer and function logic execution.
[0050] The method is to store all or redundant parameters in memory in advance. When the main function corresponding to function B calls function B, no additional storage and loading instructions are required for parameter passing, so only one instruction is needed for parameter passing.
[0051] Therefore, the advantages of this application are: by pre-storing redundant or even all parameters in memory, only the first address of the memory storing the parameters is passed during function calls. This effectively reduces the number of parameters required during function calls, eliminates the repeated storage and reading of redundant parameters during function calls, reduces the number of instructions required for function implementation, improves the execution efficiency of the image processing bad pixel program, and ultimately improves the operating efficiency of the entire image processing system, especially in the field of image detection and processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] The drawings described herein are used to provide a further understanding of the present invention, constitute a part of this application, and do not constitute a limitation of the present invention.
[0053] Figure 1 It is a schematic diagram of the process of this application method. DETAILED DESCRIPTION
[0054] In order to more clearly understand the technical content and advantages of the present invention, the present invention is now further described in detail with reference to the accompanying drawings.
[0055] like Figure 1 As shown, this application proposes an optimization method for multi-parameter function calls. In the field of image processing, sometimes some pixels in an image differ significantly from the surrounding pixels. These pixels are called bad pixels. To improve the overall image effect, the bad pixels are generally treated specially based on the pixel values of the hypothetical six pixels located around the bad pixel. This special treatment process involves accumulating and summing the pixel values of multiple pixels. The summation of these image pixels generally calls a corresponding summation function. For the convenience of subsequent description, this summation function is referred to as Function A. Since the summation operation needs to be performed on six pixels, Function A generally has six parameters.
[0056] The present invention mainly reduces the number of parameters that need to be passed when calling a function by storing redundant parameters (parameters other than the first four parameters in the function parameter list) or even all parameters in a memory space, and using the first address of the memory space as the new parameter instead of the original parameter, thereby avoiding the repeated storage and reading of redundant parameters when calling the parameters directly. Ultimately, the purpose of optimizing function calls is achieved.
[0057] For example, when there are 6 parameters p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val in the parameter list of function A, in fact, any parameters can be used, generally integer values. These 6 parameters represent the pixel values of the 6 related pixels around when processing bad pixels.
[0058] When using the existing old method, the six required parameters are passed directly when calling a function. Since MIPS only reserves four MIPS general-purpose registers for parameter passing, p0_val, p1_val, p2_val, and p3_val will be stored in the MIPS general-purpose registers ($4 to $7), and the following two parameters, e and f, will be stored in memory. When the program executes function A, when the four parameters a, b, c, and d are needed, the corresponding parameters will be obtained from the MIPS general-purpose registers ($4 to $7); when the two parameters e and f are needed, the required parameters will be read from the memory where the two parameters were stored when the function was called before, and then participate in the operation.
[0059] In contrast, when the new method of this solution is adopted, function A needs to be redefined as function B. The parameter list of function B has only one parameter, addr. This addr parameter represents the first address of a memory space. From this address space, the parameters p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val in function A can be obtained. Then, when function B is called, since function B has only one parameter addr, this parameter will be stored in the general register of MIPS. When the program executes function B and needs to use the parameters p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val, the required parameters p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val will be read from the memory pointed to by the parameter addr to the MIPS general register, and then participate in the calculation.
[0060] In summary, compared with the old method, the new method effectively reduces the number of parameters that need to be passed when calling a function by storing the redundant parameters in memory in advance, eliminating the repeated storage and reading operations of redundant parameters when calling a function, reducing the number of instructions implemented by the function, and improving the execution efficiency of the program, ultimately achieving the purpose of program optimization and improving program efficiency.
[0061] In order to verify the optimization of this method, during the optimization process of this method, we first need to clarify how to measure the cost overhead of function calls. In this solution, the cost of function calls is measured by looking at the number of assembly instructions in the entire process of function calls based on disassembled code.
[0062] Function calls can be roughly divided into four parts: parameter passing, function call jump, function logic execution, and function jump back.
[0063] For the sake of convenience, this paper compares the new and old implementations of the addition function involved in processing bad pixels in image processing, hereinafter referred to as function A (the function of this function is to calculate the cumulative sum of the pixel values of the six pixels p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val), to further describe how this solution handles the process of optimizing multi-parameter function calls.
[0064] Since the function A is to calculate the sum of the pixel values of the six pixels p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val, according to the old solution, function A needs to pass six parameters. The parameter list and calculation logic of function A are as follows:
[0065] uint8_t A(uint8_t p0_val,uint8_t p1_val,uint8_t p2_val,uint8_t p3_val,uint8_t p4_val,uint8_t p5_val){
[0066] return p0_val+p1_val+p2_val+p3_val+p4_val+p5_val;
[0067] };
[0068] To facilitate the comparison between the new and old solutions, the disassembled code of function A (9 instructions in total) is attached as shown in Table 1 below:
[0069]
[0070] The calling method of function A in the program code is shown in Table 2 below:
[0071]
[0072] Read 6 bytes of numbers from the memory space pointed to by the input_ptr address passed in the main function above, and then calculate the cumulative sum of these 6 numbers by calling function A.
[0073] In order to facilitate the subsequent description and comparison of the new and old solutions, the disassembly code corresponding to the L3 line of code is attached, as shown in Table 3 below:
[0074]
[0075] The above is the general version of the function implementation when there are multiple parameters.
[0076] Accordingly, the above implementation should be improved to the new solution described in this invention as follows:
[0077] It's clear that function A's parameter list has six parameters, and the number of parameters required to be passed when calling the function is greater than four. Therefore, you can try putting these parameters into memory and then passing only the address where the parameters are stored. The following example stores all parameters into memory.
[0078] After the above modification, there is only one parameter left in the parameter list of function A, which is used to represent the first address of the memory storing the parameters. For the convenience of explanation, the modified function A is called function B. Then, the parameter list of function B is as follows:
[0079] uint8_t B(uint8_t*arry);
[0080] Since the modified function B only passes one parameter arry to represent the address, when the original parameters (p0_val, p1_val, p2_val, p3_val, p4_val, p5_val) are needed in the function, the corresponding parameters must be read from the passed memory address and parameter operations must be performed. That is, all places in the original function A that directly use the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val for operations must be replaced with the parameter arry[index] (address + index subscript to obtain the number) in the same position in function B. The other logic of function A that does not directly use the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val remains unchanged in function B. In this way, a function B with the same functionality, more efficiency, and implemented using a new solution is obtained. The implementation of function B is as follows:
[0081] uint8_t B(uint8_t*arry){
[0082] return arry[0]+arry[1]+arry[2]+arry[3]+arry[4]+arry[5];
[0083] }
[0084] To facilitate subsequent comparison and explanation, the disassembled code of function B (a total of 13 instructions) is attached as shown in Table 4 below:
[0085]
[0086] Since the number of parameters in the parameter list of function B (the function implemented using the new solution for function A) has been reduced, the code that calls the function in the main program (the main function in this example) must also be changed accordingly. In this example, the code on line L3 of the main function is replaced by calling function B with 1 parameter instead of calling function A with 6 parameters. The rest of the irrelevant logic remains unchanged, as shown in Table 5 below:
[0087]
[0088] In order to facilitate the comparison of the subsequent solutions, the disassembly of the L3 line code of main of the improved solution is also attached, as shown in Table 6 below:
[0089]
[0090] At this point, function A that calculates the cumulative sum of 6 numbers implemented using the old solution is modified to function B implemented using the new solution.
[0091] Next, we use instruction debugging of the disassembled code as the evaluation basis to compare the function call costs between the implementation functions A and B of the new and old solutions.
[0092] According to the disassembled code in Appendix 1, since there are 6 parameters in the parameter list of function A, and only 4 of the 32 general-purpose registers of MIPS32, $4 to $7, are available for function call parameters, the extra 2 parameters need to be saved to memory before the function is called, and then reloaded when the parameter is needed in the function.
[0093] Therefore, in the main function that calls function A, in the disassembled code at line L3 (Appendix 3), we can see that $4 through $7 are loaded with parameters a through d, corresponding to lines 224, 228, 22c, and 230 in the disassembled code. a0, a1, a2, and a3 are aliases for registers $4 through $7, with a0 corresponding to $4, a1 to $5, and so on. e and f are stored in memory, corresponding to lines 234 and 244 in the disassembled code.
[0094] In addition, lines 23c and 240 correspond to function call jumps, jumping to the location of function A to execute the assembly code of function A, and the assembly code line 1c of Appendix 1 corresponds to the function jump back logic.
[0095] Therefore, the total number of instructions for calling function A is parameter transfer (8 instructions) + function call jump (2 instructions) + function logic execution (8 instructions) + function jump back (1 instruction), a total of 19 instructions.
[0096] Accordingly, since the improved solution implements function B with only one parameter in its parameter list, only one parameter (the first memory address of the parameter) needs to be passed when calling function B. Therefore, when the main function corresponding to function B calls function B, no additional store and load instructions are required for parameter passing. Therefore, parameter passing only requires one instruction, corresponding to line 258 of the disassembly code in Appendix 6.
[0097] The difference is that the improved implementation of function B pre-stores the six parameters in a memory segment and then passes the first address of this memory segment as a parameter to function B. Therefore, when function B encounters logic calculations requiring parameters p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val, these six parameters must first be read from the memory space represented by the passed parameters into registers before the calculation can proceed. This logic corresponds to lines 48, 4c, 50, 54, 5c, and 64 in the disassembled code of function B (Appendix 4).
[0098] In addition, lines 250 and 254 in Appendix 4 correspond to the function call jump, and line 74 corresponds to the function jump back logic.
[0099] In summary, the total number of instructions for calling function B is parameter transfer (1 instruction) + function call jump (2 instructions) + function logic execution (12 instructions) + function jump back (1 instruction), a total of 16 instructions.
[0100] From the above, we can see that the main differences between function A implemented using the old solution and function B implemented using the new solution lie in parameter passing and function logic execution.
[0101] To summarize with examples, when there are n parameters in the original function parameter list, n>4, the original function requires a total of (n-4)*2+4 instructions for parameter passing and ((n-4)+function logic calculation) instructions for function logic calculation.
[0102] The corresponding improved function parameter has only one parameter, and the improved function requires a total of one instruction for parameter passing and (n+function logic calculation) instructions for function logic calculation.
[0103] In summary, the number of additional instructions of the original function = (n-4)*2+4+n-4, and the improved function: n+1. When n>4, (n-4)*2+4+n-4 is always less than n+1.
[0104] The foregoing description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Those skilled in the art will readily appreciate that various modifications and variations of the present invention are possible. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of the present invention are intended to be within the scope of protection of the present invention.
Claims
1. A method for optimizing multi-parameter function calls, characterized in that: The method stores all the parameters of the program call or the parameters except the first 4 parameters in the function parameter list in a memory space, and uses the first address of the memory space to replace the original parameters to be passed as new parameters in the function call process. Assume that there are N parameters in the parameter list of function A that need to be passed, and N is greater than 4. The following steps are involved: S1, redefine function A as function B. The parameter list of function B has only one parameter, which represents the first address of a memory space. From this address space, the N parameters of function A can be obtained. S2, when function B is called, since function B has only one parameter, this parameter will be stored in the MIPS general register; when the program executes function B and needs to use the specific parameters of function A, the required specific parameters of function A will be read from the memory pointed to by the parameter to the MIPS general register, and then participate in the calculation.
2. The optimization method for multi-parameter function calls according to claim 1, characterized in that: The method assumes that the parameters of function A include p0_val, p1_val, p2_val, p3_val, p4_val, There are 6 parameters in p5_val that need to be passed. These 6 parameters represent the pixel values of the 6 pixels around the bad pixel when doing special processing on the bad pixel of the image. The following steps are involved: S1, redefine function A as function B. The parameter list of function B has only one parameter, addr. This addr parameter represents the first address of a memory space. From this address space, the parameters p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val in function A can be obtained. S2, when function B is called, since function B has only one parameter addr, this parameter will be stored in the MIPS general register; when the program executes function B, when the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val are needed, the required parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val will be read from the memory pointed to by the parameter addr to the MIPS general register, and then participate in the operation.
3. The optimization method for multi-parameter function calls according to claim 2, characterized in that: In the method, an addition function is assumed. Function A calculates the cumulative sum of six numbers: p0_val, p1_val, p2_val, p3_val, p4_val, and p5_val. Function A needs to pass six parameters. The parameter list and calculation logic of Function A are as follows: uint8_t A(uint8_t p0_val,uint8_t p1_val,uint8_t p2_val,uint8_t p3_val,uint8_t p4_val,uint8_t p5_val){ return p0_val+p1_val+p2_val+p3_val+p4_val+p5_val; }; The calling method of the function A in the program code is as follows: Reading a 6-byte number from the memory space pointed to by the input_ptr address passed in the main function, and then calculating the cumulative sum of the 6 numbers by calling function A, that is, storing all parameters in the memory; the method further includes: S1, modify the function A, and leave only one parameter in the parameter list of A, which is used to represent the first address of the memory storing the parameters. The modified function A is called function B. Then, the parameter list of function B is as follows: uint8_t B(uint8_t*arry); S2, since the modified function B only passes one parameter set to arry to represent the address, when the original parameters (p0_val, p1_val, p2_val, p3_val, p4_val, p5_val) are needed in the function, the corresponding parameters must be read from the passed memory address and parameter operations must be performed; that is, all places in the original function A that directly use the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val for operations must be replaced with the parameter arry[index] (address + index subscript to obtain the number) in the same position of function B; the other logic of function A that does not directly use the parameters p0_val, p1_val, p2_val, p3_val, p4_val, p5_val remains unchanged in function B, thus obtaining function B with the same function. The implementation of function B is as follows: uint8_t B(uint8_t*arry){ return arry[0]+arry[1]+arry[2]+arry[3]+arry[4]+arry[5]; } Since the number of parameters in the parameter list of function B has been reduced, the code for calling the function in the main function of the main program must also be changed accordingly. That is, the code in line L3 of the main function of function A, which originally called function A and passed 6 parameters, is replaced by calling function B and passing 1 parameter. The other irrelevant logical code remains unchanged, as shown below:
4. The optimization method for multi-parameter function calls according to claim 1, characterized in that: The function call can be divided into parameter transfer, function call jump, function logic execution and function jump back. Function B implemented by this method consists of these two parts: parameter transfer and function logic execution.
5. The optimization method for multi-parameter function calls according to claim 1, characterized in that: The method is to store all or redundant parameters in memory in advance. When the main function corresponding to function B calls function B, no additional storage and loading instructions are required for parameter passing, so only one instruction is needed for parameter passing.