Code file optimization method and device, electronic equipment and storage medium

By applying a culling strategy to the syntax tree of the shader code, new vertex and pixel files are generated, solving the problems of large area occupied by useless code and low performance, and achieving efficient code execution.

CN115469877BActive Publication Date: 2025-12-19CHONGQING CHANGAN TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211060438.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-08-31
Publication Date
2025-12-19
Estimated Expiration
2042-08-31

AI Technical Summary

Technical Problem

Existing technologies cannot effectively remove useless or erroneous code from shader code, resulting in large code footprint and reduced work efficiency and performance.

Method used

By obtaining the vertex and pixel files of the target code, parsing the syntax tree, and using a preset culling strategy to remove useless code, new vertex and pixel files are generated, thus optimizing the code structure.

Benefits of technology

Reduce code footprint, improve code execution efficiency, and optimize code performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115469877B_ABST
    Figure CN115469877B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of codes, in particular to a code file optimization method and device, electronic equipment and a storage medium, which comprises the following steps: acquiring a vertex file and a pixel file of a target code; loading the vertex file and the pixel file to obtain corresponding code strings, parsing the syntax of the code strings to obtain a syntax tree, and removing useless codes that do not meet preset conditions by using a preset removal strategy to obtain corresponding strings of the vertex file and the pixel file; matching a reference relationship between the vertex strings and the pixel strings, removing preset errors in the reference relationship by using the reference relationship to obtain removed vertex strings and pixel strings; and generating new vertex files and new pixel files based on the removed vertex strings and the pixel strings, so that the optimization purpose is achieved. Thus, the problems that useless codes or error codes cannot be removed in the related art, the occupied area is large, the work efficiency is reduced, the code performance is reduced and the like are solved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of code, and particularly relates to a code file optimization method and device, electronic equipment and storage medium. BACKGROUND

[0002] With the development of intelligent networked vehicles, vehicle machines based on the Android operating system are increasingly popular, and the demand for rendering gradually increases. The requirements for 3D rendering quality, real-time and performance are increasingly high, so any optimization of the 3D rendering process is particularly important. Glsl, that is, OpenGL Shading Language, is a language used for shading programming in OpenGL. The code written in this language is executed on a GPU (Graphic Processor Unit), replacing part of the fixed rendering pipeline.

[0003] In the related art, shader code is a program running on a GPU. It is a very performance-consuming operation to feed back information from the GPU, which makes debugging very difficult. In a large project, there are a large number of shader codes, which are complex and complicated. If the files are isolated and divided according to functions, the shader will be prone to errors and there will be unreasonable codes that are not found. SUMMARY

[0004] The present application provides a code file optimization method and device, electronic equipment and storage medium to solve the problem that useless codes or error codes cannot be removed in the related art, resulting in large occupied area, reduced work efficiency, and reduced code performance.

[0005] The first aspect of the present application provides a code file optimization method, comprising the following steps: obtaining a vertex file and a pixel file of a target code; loading the vertex file and the pixel file respectively to obtain code strings corresponding to the vertex file and the pixel file, parsing the syntax of the code strings to obtain a syntax tree, and removing useless codes in the syntax tree that do not meet a preset condition by using a preset removal strategy to obtain a vertex string of the vertex file and a pixel string of the pixel file; matching the reference relationship between the vertex string and the pixel string, and removing preset errors in the vertex string and the pixel string by using the reference relationship to obtain the vertex string and the pixel string after removal, generating a new vertex file and a new pixel file based on the vertex string and the pixel string after removal, and achieving the optimization purpose.

[0006] According to the technical means, the vertex file and the pixel file of the obtained code are loaded to obtain corresponding code strings, syntax processing is performed, useless codes not meeting the conditions are removed, fixed-point strings of the vertex file and pixel strings of the pixel file are obtained, and the fixed-point strings and the pixel strings are matched by using a reference relationship, error strings are removed to obtain removed vertex strings and pixel strings, and new vertex files and new pixel files are generated, the useless codes and error codes are removed, code occupation area is reduced, and the optimization purpose is achieved, so that the running efficiency of the code is improved.

[0007] Optionally, the removing the useless codes not meeting the preset conditions in the syntax tree by using the preset removing strategy to obtain the vertex strings of the vertex file and the pixel strings of the pixel file comprises:

[0008] when it is identified that there is a constant definition in the syntax tree, constant folding is performed; and / or, when it is identified that there is a preset equality check in the syntax tree, it is judged that a code corresponding to the preset equality check is invalid, and the code is removed; and / or, all leaf nodes of the syntax tree are scanned, a temporary variable is removed, and an expression of the syntax tree is replaced at a position where the temporary variable is used; and / or, when a function is identified, a called code position is matched according to a reference relationship, it is judged whether the function is inlined, if yes, the function is removed, and content of the function is replaced to the function calling position; and / or, when it is identified that there is a preset local variable, a preset constant or a preset structure without a reference relationship, a code corresponding to the preset local variable, the preset constant or the preset structure is removed; and / or, when a first preset keyword of the target code is identified, it is judged whether the preset keyword is in a preset condition branch code, if yes, a temporary variable is added, and the preset keyword is moved out of the preset condition branch by a value of the preset keyword; and / or, when a single-step assignment to a variable in a structure is identified, the single-step assignment is replaced by a preset swizzle operation; and / or, when an expression of a loop is identified, a second preset keyword is found in the expression, a processing logic of the second preset keyword is replaced by defining an identifier, and when a return code is identified, the return operation is replaced by defining an identifier, and the return operation is moved to a tail of the function.

[0009] According to the technical means, different removing strategies are used to remove or replace useless codes not meeting conditions according to different syntaxes, performance consumption of assignments and function calls is reduced, the optimization purpose is achieved, and the running efficiency of the code is improved.

[0010] Optionally, the matching the reference relationship between the vertex string and the pixel string comprises: matching global variables of the vertex string and the pixel string and built-in variables of the target code; if the global variables and the built-in variables are matched, generating the reference relationship based on the global variables and the built-in variables, otherwise, determining that the matching fails, and sending an exception information to a command line.

[0011] According to the above technical means, the global variables of the vertex string and the pixel string and the built-in variables of the code are matched, if matched, the reference relationship is generated, if not matched, the exception information is sent to the command line, by matching the reference relationship between the vertex string and the pixel string, it is convenient to determine whether the string meets the requirements or has errors, and then corresponding operations are made, so as to achieve the optimization purpose, thereby improving the running efficiency of the code.

[0012] Optionally, the loading the vertex file and the pixel file respectively to obtain the code strings corresponding to the vertex file and the pixel file comprises: obtaining file paths of the vertex file and the pixel file; locating the vertex file and the pixel file according to the file paths, and reading the code strings corresponding to the vertex file and the pixel file.

[0013] According to the above technical means, the file paths of the vertex file and the pixel file are obtained, and the file positions are located according to the file paths, and the code strings corresponding to the vertex file and the pixel file are obtained therefrom, by loading the related files, it is convenient to find and obtain the required code strings, thereby improving the work efficiency and saving time.

[0014] The second aspect embodiment of the present application provides a code file optimization device, comprising: an obtaining module, configured to obtain a vertex file and a pixel file of a target code; a first processing module, configured to load the vertex file and the pixel file respectively to obtain code strings corresponding to the vertex file and the pixel file, parse the syntax of the code strings to obtain a syntax tree, and use a preset elimination strategy to eliminate useless codes in the syntax tree which do not meet a preset condition, to obtain a vertex string of the vertex file and a pixel string of the pixel file; and a second processing module, configured to match a reference relationship between the vertex string and the pixel string, and use the reference relationship to eliminate preset errors in the vertex string and the pixel string to obtain the vertex string and the pixel string after elimination, and generate a new vertex file and a new pixel file based on the vertex string and the pixel string after elimination, to achieve the optimization purpose.

[0015] Optionally, the first processing module is configured to: when it is identified that there is a constant definition in the syntax tree, performing constant folding; and / or, when it is identified that there is a preset equality check in the syntax tree, judging that code corresponding to the preset equality check is invalid, and eliminating the code; and / or, scanning all leaf nodes of the syntax tree, eliminating temporary variables, and replacing expressions of the syntax tree at positions where the temporary variables are used; and / or, when a function is identified, judging whether to inline according to a code position of a call matched by reference relationship, and if yes, eliminating the function and replacing content of the function to a function call position; and / or, when it is identified that there is a preset local variable, a preset constant or a preset structure without reference relationship, eliminating code corresponding to the preset local variable, the preset constant or the preset structure; and / or, when a first preset keyword of the target code is identified, judging whether the preset keyword is in preset conditional branch code, and if yes, adding a temporary variable to move a value of the preset keyword out of the preset conditional branch; and / or, when a single-step assignment to a variable inside a structure is identified, replacing the single-step assignment with a preset swizzle operation; and / or, when an expression of a loop is identified, finding a second preset keyword inside the expression, replacing processing logic of the second preset keyword by defining an identifier, and when a return code is identified, replacing the return operation by defining an identifier and moving the return operation to a tail of a function.

[0016] Optionally, the first processing module is configured to: acquire file paths of the vertex file and the pixel file; locate the vertex file and the pixel file according to the file paths, and read code strings corresponding to the vertex file and the pixel file.

[0017] The third aspect of the embodiments of the present application provides an electronic device, including: a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor executes the program to implement the code file optimization method as described in the above embodiments.

[0018] The fourth aspect of the embodiments of the present application provides a computer readable storage medium, which stores a computer program, and the program is executed by a processor to implement the code file optimization method as described in the above embodiments.

[0019] Therefore, the present application has at least the following beneficial effects:

[0020] (1) The embodiment of the present application loads the vertex file and the pixel file of the obtained code to obtain corresponding code strings, then performs syntax processing, eliminates the code that does not meet the condition, obtains the fixed-point string of the vertex file and the pixel string of the pixel file, and matches them by using the reference relationship, eliminates the error string to obtain the eliminated vertex string and pixel string, and generates a new vertex file and a new pixel file respectively, eliminates the useless code and the error code, reduces the code occupation area, achieves the optimization purpose, and thus improves the running efficiency of the code.

[0021] (2) The embodiment of the present application eliminates or replaces the useless code that does not meet the condition by using different elimination strategies for different syntaxes, reduces the performance consumption of assignment and function call, achieves the optimization purpose, and thus improves the running efficiency of the code.

[0022] (3) The embodiment of the present application matches the global variable and the built-in variable of the code of the vertex string and the pixel string, generates the reference relationship if the matching succeeds, sends the exception information to the command line if the matching fails, judges whether the string meets the requirement or has an error by matching the reference relationship between the vertex string and the pixel string, and thus makes corresponding operation, achieves the optimization purpose, and thus improves the running efficiency of the code.

[0023] (4) The embodiment of the present application obtains the file path of the vertex file and the pixel file, positions to the file location according to the file path, obtains the code string corresponding to the vertex file and the pixel file from the file location, and thus improves the work efficiency and saves time.

[0024] The additional aspects and advantages of the present application will be partially given in the following description, partially will become obvious from the following description, or will be understood by practicing the present application. BRIEF DESCRIPTION OF DRAWINGS

[0025] The above and / or additional aspects and advantages of the present application will become apparent and more readily appreciated from the following description of the embodiments, taken in conjunction with the accompanying drawings, in which:

[0026] Figure 1 A flowchart of the code file optimization method according to the embodiment of the present application;

[0027] Figure 2 A processing flowchart of the code file optimization method according to the embodiment of the present application;

[0028] Figure 3 A block diagram of the code file optimization device according to the embodiment of the present application;

[0029] Figure 4This is a schematic diagram of the structure of an electronic device according to an embodiment of this application. Detailed Implementation

[0030] The embodiments of this application are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and intended to explain this application, and should not be construed as limiting this application.

[0031] The following description, with reference to the accompanying drawings, outlines a method, apparatus, electronic device, and storage medium for optimizing code files according to embodiments of this application. Addressing the problem mentioned in the background art of failing to remove useless or erroneous code, thus reducing work efficiency and code performance, this application provides a method for optimizing code files. In this method, a new shader code file is output by removing and improving existing shader code files, and then optimized during OpenGL program runtime. This solves the problems in related technologies, such as the inability to remove useless or erroneous code, leading to reduced work efficiency and code performance.

[0032] Specifically, Figure 1 This is a flowchart illustrating a code file optimization method provided in an embodiment of this application.

[0033] like Figure 1 As shown, the optimization method for this code file includes the following steps:

[0034] In step S101, the vertex file and pixel file of the target code are obtained.

[0035] It is understood that the code file and pixel file obtained in this application are used to prepare for subsequent parsing of the corresponding code strings in the file.

[0036] In step S102, the vertex file and pixel file are loaded respectively to obtain the code strings corresponding to the vertex file and pixel file. The syntax of the code string is parsed to obtain the syntax tree. Then, the useless code that does not meet the preset conditions in the syntax tree is removed using a preset culling strategy to obtain the vertex string of the vertex file and the pixel string of the pixel file.

[0037] The preset removal strategy can be a user-defined removal strategy, such as replacing the content defined by #define and then deleting #define from the code; or removing defined constants, etc. It can be adjusted according to the actual situation and is not specifically limited here.

[0038] The preset condition can be a condition set by a user in advance, for example, no error in syntax checking of the code string, which is not limited herein.

[0039] It can be understood that the embodiment of the present application loads the vertex file and the pixel file of the code and obtains the corresponding code string, and then performs syntax processing, eliminates the code that does not meet the condition, obtains the fixed-point string of the vertex file and the pixel string of the pixel file, and achieves the optimization purpose, thereby improving the running efficiency of the code.

[0040] In the embodiment of the present application, the vertex file and the pixel file are loaded to obtain the corresponding code string of the vertex file and the pixel file, including: obtaining the file path of the vertex file and the pixel file; locating the vertex file and the pixel file according to the file path, and reading the corresponding code string of the vertex file and the pixel file.

[0041] It can be understood that the embodiment of the present application obtains the file path of the vertex file and the pixel file, and locates the file position according to the file path, to obtain the corresponding code string of the vertex file and the pixel file, which facilitates the finding and obtaining of the required code string by loading the related file, improves the work efficiency, and saves time.

[0042] In the embodiment of the present application, the preset elimination strategy is used to eliminate the useless code in the syntax tree that does not meet the preset condition, to obtain the vertex string of the vertex file and the pixel string of the pixel file, including: when it is identified that there is a constant definition in the syntax tree, constant folding is performed; and / or, when it is identified that there is a preset equality check in the syntax tree, it is judged that the code corresponding to the preset equality check is invalid, and the code is eliminated; and / or, all leaf nodes of the syntax tree are scanned, a temporary variable is eliminated, and the expression of the syntax tree is replaced at the position where the temporary variable is used; and / or, when a function is identified, the code position of the call is matched according to the reference relationship, it is judged whether to inline, if yes, the function is eliminated, and the content of the function is replaced to the function call position; and / or, when it is identified that there is a preset local variable, a preset constant or a preset structure without reference relationship, the code corresponding to the preset local variable, the preset constant or the preset structure is eliminated; and / or, when a first preset keyword of the target code is identified, it is judged whether the preset keyword is in the preset condition branch code, if yes, a temporary variable is added, the preset keyword moves the value outside the preset condition branch; and / or, when a single-step assignment to a variable inside a structure is identified, the single-step assignment is replaced by a preset swizzle operation; and / or, when an expression of a loop is identified, a second preset keyword is found inside it, the processing logic of the second preset keyword is replaced by defining an identifier, and when a return code is identified, a return operation is replaced by defining an identifier, and the return operation is moved to the tail of the function.

[0043] The preset equality check can be an equality check set by a user in advance, for example, in a conditional judgment, the variables on the left and right of a relational operator can have a certain result, such as the left variable being always empty, or the left and right variables being always equal, and the like, which are not limited herein.

[0044] The preset local variable can be an internal variable, that is, a variable defined in a function or a compound statement, for example, taking a local variable without a reference relationship as an example, the local variable is just declared but not used, if the variable is not used after being declared, an error will occur during compilation, the warning is a set of warnings provided by a compiler for code that can cause a runtime error, which is not limited herein.

[0045] The preset constant can be a quantity that is not modified by a program during computer program running, for example, taking a constant without a reference relationship as an example, the constant is a quantity that is not modified and is continuously used, but cannot be referenced by an address, which is not limited herein.

[0046] The preset structure can be a data type constructed by combining different types of data into a whole, for example, at least one structure must be referenced in another structure in the form of a pointer, which is not limited herein.

[0047] The first preset keyword can be a keyword set by a user in advance, for example, the keyword is discard, which is not limited herein.

[0048] The preset conditional branch code can be code in a conditional branch, which is determined whether to execute according to a condition, which is not limited herein.

[0049] The second preset keyword can be a keyword set by a user in advance, for example, the keyword is continue, break, and the like, which is not limited herein.

[0050] It can be understood that different elimination strategies are used to eliminate the useless code that does not meet the condition according to different syntaxes, the performance consumption of assignment and function call is reduced, the optimization purpose is achieved, and the running efficiency of the code is improved.

[0051] In step S103, the reference relationship between the vertex string and the pixel string is matched, and the preset error in the vertex string and the pixel string is eliminated by using the reference relationship, to obtain an eliminated vertex string and an eliminated pixel string, and a new vertex file and a new pixel file are generated based on the eliminated vertex string and the eliminated pixel string, to achieve the optimization purpose.

[0052] The preset error can be an error set by a user in advance, for example, a code not to be executed, meaningless code, error code, and the like, which are not limited herein.

[0053] It can be understood that the embodiments of the present application match the fixed-point strings of the vertex files and the pixel strings of the pixel files by using the reference relationship, eliminate the error strings to obtain the eliminated vertex strings and pixel strings, and generate new vertex files and new pixel files, respectively, so as to eliminate the useless code and error code and achieve the optimization purpose, thereby improving the running efficiency of the code.

[0054] In the embodiments of the present application, the matching of the reference relationship between the vertex strings and the pixel strings includes: matching the global variables and the built-in variables of the target code of the vertex strings and the pixel strings; if the global variables and the built-in variables are matched, the reference relationship is generated based on the global variables and the built-in variables, otherwise, it is determined that the matching fails, and an exception information is sent to the command line.

[0055] It can be understood that the embodiments of the present application match the global variables of the vertex strings and the pixel strings and the built-in variables of the code, and if the matching succeeds, the reference relationship is generated, and if the matching fails, the exception information is sent to the command line. By matching the reference relationship between the vertex strings and the pixel strings, it is convenient to determine whether the strings meet the requirements or have errors, and then corresponding operations are made, so as to achieve the optimization purpose and improve the running efficiency of the code.

[0056] According to the optimization method of the code file provided in the embodiments of the present application, the vertex files and the pixel files of the obtained code are loaded to obtain corresponding code strings, and then syntax processing is performed to eliminate the code not meeting the conditions, so as to obtain the fixed-point strings of the vertex files and the pixel strings of the pixel files, and match the reference relationship, eliminate the error strings to obtain the eliminated vertex strings and pixel strings, and generate new vertex files and new pixel files, respectively, so as to eliminate the useless code and error code and achieve the optimization purpose, thereby improving the running efficiency of the code. Thus, the problem that the useless code or error code cannot be eliminated, resulting in large occupied area, low work efficiency, and low code performance, and the like, is solved.

[0057] The following will be described in detail Figure 2 The optimization method of the code file will be described in detail. In the present application, no UI interaction is performed, and therefore the getopt function is encapsulated to support the acceptance of necessary parameters in the command line, such as the input path and the output path of the shader file, and the like. The specific steps are as follows:

[0058] The first step is to input the shader file path to be pruned on the command line, and the file path generated after the pruning process is completed; in addition, two shader files need to be input at one time, and the type needs to be specified on the command line and it is indicated which one is the vertex shader and which one is the pixel shader.

[0059] The second step is that after the command line accepts the required parameters, the information is given to the ShaderLoader module, and the module will find the corresponding file according to the file path and read the file content in the form of a string.

[0060] The third step is that the ShaderLoader obtains the code string and then transmits it to the preprocessing module for processing. It first copies the string, and then processes some macro definitions identified, such as replacing the contents defined by #define, and then deleting #define from the code. Therefore, the actual pruning operation starts at this step.

[0061] The fourth step is that after the preprocessing module completes the processing, it transmits the code string to the parsing module for processing. The module mainly performs syntax analysis on the string, identifies which is the keyword content, which is the operator, which is the function, which is the variable, and constructs the calling relationship inside the entire code and forms a syntax tree, and also performs some simple syntax checking. If the parsing fails, the failure log will be printed on the command line.

[0062] The fifth step is that since the parsing module generates a syntax tree, the actual operation of pruning can be performed. The present embodiment performs the following operations:

[0063] (1) When a constant definition is identified, constant folding is performed, that is, the defined constant is pruned, and then the subsequent code uses the value of the constant to replace it.

[0064] (2) When an equality check is identified, that is, in conditional judgment, the variables on the left and right of the relational operator may have a certain result, such as the left variable is always empty, or the left and right variables are always equal, which will cause the code executed by the condition to have a branch that is invalid. Once this situation is checked, the invalid code will be pruned.

[0065] (3) All leaf nodes of the expression tree are scanned to prune temporary variables that can be pruned, and the expression is directly replaced at the location where the temporary variable is used. This operation is mainly to reduce the performance consumption of assignment.

[0066] (4) In the identification of the function, find the code location of the call according to the reference relationship, determine whether it can be inlined, if it can, remove the function, replace the function content to the function call location, the operation is mainly to reduce the performance consumption of function call.

[0067] (5) In the identification of the existence of no reference relationship of local variables, constants or structures, remove these codes, the part depends on the reference count implemented in the generation of syntax tree.

[0068] (6) In the identification of glsl keyword discard, determine whether it is in the conditional branch code, if it is, move the keyword outside the conditional branch by adding temporary variables.

[0069] (7) In the identification of single-step assignment to structure internal variables, replace the single-step assignment with swizzle operation.

[0070] (8) In the identification of the expression of the loop, find continue, break and other keywords inside it, replace the processing logic of these keywords in the form of defining identifiers; when identifying return, also move it to the tail of the function by this way.

[0071] The sixth step is to match the vertex string and the pixel string after the removal. In the modern GPU rendering pipeline, the operation of vertex shader is before the pixel shader, usually the vertex shader can be seen as the input source of the pixel shader, by comparison, form the reference relationship of two strings, the main operation is to match the global variable uniforms and glsl built-in variables, if the matching fails, the exception information is printed to the command line.

[0072] The seventh step is mainly to check the uniform after the matching. Since the removal operation in the fifth step is carried out on two strings respectively, and does not combine the two to remove, such as the part of the dead code removal in the fifth step can only be aimed at local reference, and this step can safely remove the global variables, functions or structures.

[0073] The eighth step, after the completion of the above operation, can generate the corresponding shader file according to the current string, after the file generation, the two files can be loaded into the OpenGL program to run, there is no difference in function, but the efficiency will be improved.

[0074] In summary, the embodiment of the present application optimizes the shader code file by removing and improving the shader code file, and then achieves the optimization effect when the OpenGL program runs, which can improve the execution efficiency of the shader, reduce the size of the shader code, remove the code that will not be executed, the defined useless variables, and the comments and other meaningless glsl codes for hardware, and also optimizes the glsl code to a certain extent, such as inlining the function, removing the function call, and reducing unnecessary overhead.

[0075] Secondly, the code file optimization device according to the embodiment of the present application is described with reference to the accompanying drawings.

[0076] Figure 3 is a block schematic diagram of the code file optimization device according to the embodiment of the present application.

[0077] As Figure 3 shown, the code file optimization device 10 includes an acquisition module 100, a first processing module 200, and a second processing module 300.

[0078] The acquisition module 100 is configured to acquire vertex files and pixel files of a target code; the first processing module 200 is configured to load the vertex files and the pixel files respectively to obtain code strings corresponding to the vertex files and the pixel files, parse the syntax of the code strings to obtain syntax trees, and remove useless codes in the syntax trees that do not meet preset conditions by using a preset removal strategy to obtain vertex strings of the vertex files and pixel strings of the pixel files; and the second processing module 300 is configured to match a reference relationship between the vertex strings and the pixel strings, remove preset errors in the vertex strings and the pixel strings by using the reference relationship to obtain removed vertex strings and pixel strings, and generate new vertex files and new pixel files based on the removed vertex strings and the pixel strings to achieve the optimization purpose.

[0079] In the embodiment of the present application, the first processing module 200 is configured to acquire file paths of the vertex files and the pixel files, locate the vertex files and the pixel files according to the file paths, and read code strings corresponding to the vertex files and the pixel files.

[0080] In the embodiment of the present application, the first processing module 200 is configured to: when it is identified that there is a constant definition in the syntax tree, performing constant folding; and / or, when it is identified that there is a preset equality check in the syntax tree, judging that the code corresponding to the preset equality check is invalid, and eliminating the code; and / or, scanning all leaf nodes of the syntax tree, eliminating temporary variables, and replacing the expressions of the syntax tree at positions where the temporary variables are used; and / or, when a function is identified, matching a code position of a call according to a reference relationship, judging whether to inline, if yes, eliminating the function, and replacing content of the function to the function call position; and / or, when it is identified that there is a preset local variable, a preset constant or a preset structure without a reference relationship, eliminating the code corresponding to the preset local variable, the preset constant or the preset structure; and / or, when a first preset keyword of target code is identified, judging whether the preset keyword is in a preset conditional branch code, if yes, adding a temporary variable, moving a value of the preset keyword outside the preset conditional branch; and / or, when a single-step assignment to a variable inside a structure is identified, replacing the single-step assignment with a preset swizzle operation; and / or, when an expression of a loop is identified, finding a second preset keyword inside the expression, and replacing processing logic of the second preset keyword by defining an identifier, and when a return code is identified, replacing a return operation by defining an identifier, and moving the return operation to a tail of the function.

[0081] In the embodiment of the present application, the second processing module 300 is configured to: matching global variables and built-in variables of the target code with the vertex string and the pixel string; if the global variables and the built-in variables are matched, generating a reference relationship based on the global variables and the built-in variables, otherwise, determining that the matching fails, and sending an exception information to a command line.

[0082] It should be noted that the foregoing explanation of the embodiment of the code file optimization method is also applicable to the code file optimization device of the embodiment, which will not be repeated here.

[0083] According to the code file optimization device provided in the embodiment of the present application, the vertex file and the pixel file of the obtained code are loaded to obtain corresponding code strings, and then syntax processing is performed to eliminate code that does not meet the conditions, to obtain a vertex string of the vertex file and a pixel string of the pixel file, and the vertex string and the pixel string are matched by using a reference relationship, and error strings are eliminated to obtain the eliminated vertex string and the eliminated pixel string, and new vertex file and new pixel file are generated, and useless code and error code are eliminated, so that the optimization purpose is achieved, and the running efficiency of the code is improved. Thus, the problem that useless code or error code cannot be eliminated, resulting in large occupied area, low work efficiency, and low code performance, and other problems are solved.

[0084] Figure 4A structural schematic diagram of an electronic device is provided in the embodiments of the present application. The electronic device can include

[0085] The memory 401, the processor 402 and the computer program stored in the memory 401 and executable on the processor 402.

[0086] The processor 402 implements the optimization method of the code file provided in the above embodiments when executing the program.

[0087] Further, the electronic device further includes

[0088] The communication interface 403 is used for communication between the memory 401 and the processor 402.

[0089] The memory 401 is used for storing the computer program executable on the processor 402.

[0090] The memory 401 can include a high-speed RAM (Random Access Memory) memory, and can also include a non-volatile memory, such as at least one disk memory.

[0091] If the memory 401, the processor 402 and the communication interface 403 are independently implemented, the communication interface 403, the memory 401 and the processor 402 can be connected to each other through a bus and complete communication between each other. The bus can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component) bus or an EISA (Extended Industry Standard Architecture) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For the convenience of representation, Figure 4 Only one thick line is used in the figure, but it does not mean that there is only one bus or only one type of bus.

[0092] Optionally, in specific implementation, if the memory 401, the processor 402 and the communication interface 403 are integrated on a chip, the memory 401, the processor 402 and the communication interface 403 can complete communication between each other through an internal interface.

[0093] The processor 402 can be a CPU (Central Processing Unit), or an ASIC (Application Specific Integrated Circuit), or one or more integrated circuits configured to implement the embodiments of the present application.

[0094] The embodiment of the present application further provides a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to implement the code file optimization method.

[0095] In the description of the specification, the description of the terms "one embodiment", "some embodiments", "an example", "a specific example", or "some examples" means that the specific features, structures, materials or characteristics described in connection with the embodiment or example are included in at least one embodiment or example of the present application. In the specification, the illustrative description of the above terms is not necessarily directed to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any appropriate manner in any one or N embodiments or examples. In addition, the person skilled in the art can combine and combine the different embodiments or examples described in the specification and the features of the different embodiments or examples without contradiction.

[0096] In addition, the terms "first", "second" are only for descriptive purposes and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined with "first", "second" can explicitly or implicitly include at least one of the features. In the description of the present application, the meaning of "N" is at least two, for example, two, three, etc., unless otherwise specifically limited.

[0097] Any process or method descriptions in flow charts or described elsewhere herein can be understood as representing code modules, segments, or portions of code that include one or more executable instructions for implementing the specified logical functions or processes, and the preferred embodiments of the present application also include additional implementation involving other processes or methods. It will be appreciated that the scope of the preferred embodiments of the present application encompasses also additional implementation in which the functions performed by the code modules, segments, or portions are carried out in a different order than the order shown or discussed, including use of a substantially simultaneous or an opposite order, as appropriate, and that the scope of the preferred embodiments of the present application encompasses additional implementation in which additional or different steps are carried out, as appropriate.

[0098] It should be understood that various parts of the present application can be implemented in hardware, software, firmware or a combination thereof. In the above-described embodiments, the N steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. As in another embodiment, if implemented in hardware, any of the following technologies known in the art or their combination can be used: discrete logic circuit with logic gate circuit for implementing logical functions on data signals, application specific integrated circuit with suitable combination of logic gate circuit, programmable gate array, field programmable gate array, etc.

[0099] Those skilled in the art can understand that all or part of the steps of the method carried out by the above-mentioned embodiments can be completed by programs instructing relevant hardware, and the programs can be stored in a computer readable storage medium. When the program is executed, it includes one of the steps of the method embodiment or a combination thereof.

[0100] Although the embodiments of the present application have been shown and described above, it should be understood that the above-mentioned embodiments are exemplary and cannot be understood as limiting the present application. Those skilled in the art can make changes, modifications, replacements and variations to the above-mentioned embodiments within the scope of the present application.

Claims

1. A method of optimizing a code file, characterized by, The method comprises the following steps: Obtaining a vertex file and a pixel file of target code; Loading the vertex file and the pixel file respectively to obtain code strings corresponding to the vertex file and the pixel file, parsing the syntax of the code strings to obtain a syntax tree, and removing useless code in the syntax tree that does not meet preset conditions by using a preset removal strategy to obtain a vertex string of the vertex file and a pixel string of the pixel file; Matching a reference relationship between the vertex string and the pixel string, and removing preset errors in the vertex string and the pixel string by using the reference relationship to obtain the vertex string and the pixel string after removal, and generating a new vertex file and a new pixel file based on the vertex string and the pixel string after removal respectively to achieve an optimization purpose.

2. The method of claim 1, wherein, The removing of the useless code in the syntax tree that does not meet the preset conditions by using the preset removal strategy to obtain the vertex string of the vertex file and the pixel string of the pixel file comprises: When it is identified that there is a constant definition in the syntax tree, constant folding is performed; And / or, when it is identified that there is a preset equality check in the syntax tree, it is judged that the code corresponding to the preset equality check is invalid, and the code is removed; And / or, all leaf nodes of the syntax tree are scanned, a temporary variable is removed, and an expression of the syntax tree is replaced at a position where the temporary variable is used; And / or, when a function is identified, a code position of a call is matched according to a reference relationship, it is judged whether to inline, if yes, the function is removed, and contents of the function are replaced to a function call position; And / or, when it is identified that there is a preset local variable, a preset constant or a preset structure without a reference relationship, the code corresponding to the preset local variable, the preset constant or the preset structure is removed; And / or, when a first preset keyword of the target code is identified, it is judged whether the preset keyword is in a preset condition branch code, if yes, a temporary variable is added, the preset keyword is moved out of the preset condition branch by a value of the preset keyword; And / or, when a single-step assignment to a variable inside a structure is identified, the single-step assignment is replaced by a preset swizzle operation; And / or, when an expression of a loop is identified, a second preset keyword is found inside the expression, a processing logic of the second preset keyword is replaced by defining an identifier, and when a return code is identified, a return operation is replaced by defining an identifier, and the return operation is moved to a tail of the function.

3. The method of claim 1, wherein, The matching of the reference relationship between the vertex string and the pixel string comprises: Matching global variables of the vertex string and the pixel string and built-in variables of the target code; If the global variables and the built-in variables are matched, the reference relationship is generated based on the global variables and the built-in variables, otherwise, it is determined that the matching fails, and an exception information is sent to a command line.

4. The method according to any one of claims 1 to 3, characterized in that, The loading of the vertex file and the pixel file to obtain the code strings corresponding to the vertex file and the pixel file comprises: Obtaining file paths of the vertex file and the pixel file; Locating the vertex file and the pixel file according to the file paths, and reading code strings corresponding to the vertex file and the pixel file.

5. An apparatus for optimizing a code file, characterized by, Comprise: An obtaining module, configured to obtain a vertex file and a pixel file of target code; A first processing module, configured to load the vertex file and the pixel file respectively to obtain code strings corresponding to the vertex file and the pixel file, parse the syntax of the code strings to obtain a syntax tree, and remove useless code in the syntax tree that does not meet a preset condition by using a preset elimination strategy, so as to obtain a vertex string of the vertex file and a pixel string of the pixel file; A second processing module, configured to match a reference relationship between the vertex string and the pixel string, remove a preset error in the vertex string and the pixel string by using the reference relationship to obtain the vertex string and the pixel string after elimination, and generate a new vertex file and a new pixel file based on the vertex string and the pixel string after elimination respectively, so as to achieve an optimization purpose.

6. The apparatus of claim 5, wherein, The first processing module is configured to: When it is identified that there is a constant definition in the syntax tree, perform constant folding; And / or, when it is identified that there is a preset equality check in the syntax tree, determine that code corresponding to the preset equality check is invalid, and remove the code; And / or, scan all leaf nodes of the syntax tree, remove a temporary variable, and replace an expression of the syntax tree at a position where the temporary variable is used; And / or, when a function is identified, match a code position of a call according to a reference relationship, determine whether to inline, if yes, remove the function, and replace content of the function to a function call position; And / or, when it is identified that there is a preset local variable, a preset constant or a preset structure without a reference relationship, remove code corresponding to the preset local variable, the preset constant or the preset structure; And / or, when a first preset keyword of the target code is identified, determine whether the preset keyword is in a preset condition branch code, if yes, add a temporary variable, move a value of the preset keyword out of the preset condition branch; And / or, when a single-step assignment to a variable inside a structure is identified, replace the single-step assignment with a preset swizzle operation; And / or, when an expression of a loop is identified, find a second preset keyword inside the expression, replace processing logic of the second preset keyword by defining an identifier, and when a return code is identified, replace a return operation by defining an identifier, and move the return operation to a tail of the function.

7. The apparatus of claim 5, wherein, The second processing module is configured to: Match a global variable of the vertex string and the pixel string and a built-in variable of the target code; If the global variable and the built-in variable are matched, generate the reference relationship based on the global variable and the built-in variable, otherwise, determine that the matching fails, and send exception information to a command line.

8. The apparatus of claim 5, wherein, The first processing module is configured to: Obtain file paths of the vertex file and the pixel file; Locating the vertex file and the pixel file according to the file path, and reading code strings corresponding to the vertex file and the pixel file.

9. An electronic device, comprising: Comprising: a memory, a processor, and a computer program stored on the memory and executable on the processor, the processor executing the program to implement the optimization method of the code file according to any one of claims 1-4.

10. A computer-readable storage medium having stored thereon a computer program, characterized in that, The program is executed by the processor for implementing the optimization method of the code file according to any one of claims 1-4.

Citation Information

Patent Citations

  • Shader simplifying method and device and image rendering method based on surface signal fitting

    CN104167015A

  • Compute graph optimization

    CN113256475A