A method and device for optimizing game scripts

By generating and optimizing abstract syntax trees, the problem of insufficient automation optimization in game scripts is solved, and comprehensive performance improvement and user experience improvement is achieved.

CN114159799BActive Publication Date: 2025-06-24NETEASE (HANGZHOU) NETWORK CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111478644.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-06
Publication Date
2025-06-24
Estimated Expiration
2041-12-06

AI Technical Summary

Technical Problem

The existing technology relies on manual analysis and third-party tools in game script optimization, and cannot achieve comprehensive automation optimization, resulting in excessive memory and CPU performance consumption during game operation and lag and other problems.

Method used

By obtaining game scripts and generating abstract syntax trees, optimized processing includes deleting specified log statements, adding specified assignment nodes, deleting code comments, formatting local variable naming, replacing variable references as constants, and deleting unused modules, etc., generating optimized game scripts.

Benefits of technology

It realizes comprehensive optimization of game scripts, reduces memory usage and performance consumption, improves user experience, and avoids the separate optimization needs for serious bottlenecks in performance consumption.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114159799B_ABST
    Figure CN114159799B_ABST
Patent Text Reader

Abstract

Embodiments of the present invention provide a method and apparatus for optimizing a game script. The method includes: obtaining a first game script and generating an abstract syntax tree for the first game script; optimizing the abstract syntax tree; and generating a second game script according to the optimized abstract syntax tree. In the embodiments of the present invention, by obtaining a first game script, generating an abstract syntax tree for the first game script, optimizing the abstract syntax tree, and generating a second game script according to the optimized abstract syntax tree, it is possible to comprehensively optimize all game codes, rather than being limited to the bottleneck parts with serious performance consumption. Therefore, there is no need for manual analysis of the bottleneck parts in the game script, and the game can be comprehensively accelerated to improve the user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of games, and particularly to a method and apparatus for optimizing game scripts. Background Art

[0002] As the online time of a game increases, the code logic of the game script becomes more and more numerous and complex, resulting in more and more memory and CPU performance being occupied during the game operation, and phenomena such as lag occur.

[0003] The current approach is to analyze the real data obtained during the game operation with the help of a third-party tool, and find the bottleneck parts with relatively serious performance consumption for optimization.

[0004] However, this requires that the third-party tool can correctly capture the game operation data, and it is necessary to manually analyze the bottleneck parts with relatively serious performance consumption from the operation data for optimization, and it is optimized by reconstructing the logic code. These dependencies result in the optimization method using the third-party tool not being automated enough and unable to comprehensively optimize the game script. Summary of the Invention

[0005] In view of the above problems, a method and apparatus for optimizing game scripts are proposed to overcome the above problems or at least partially solve the above problems, including:

[0006] A method for optimizing a game script, the method including:

[0007] Obtain a first game script, and generate an abstract syntax tree for the first game script;

[0008] Optimize the abstract syntax tree;

[0009] Generate a second game script according to the optimized abstract syntax tree.

[0010] Optionally, the optimizing the abstract syntax tree includes:

[0011] Determine function call nodes according to the abstract syntax tree;

[0012] Judge whether the function call nodes correspond to specified log statements;

[0013] When the function call nodes correspond to specified log statements, delete the specified log statements corresponding to the function call nodes in the abstract syntax tree.

[0014] Optionally, the judging whether the function call nodes correspond to specified log statements includes:

[0015] Determine whether the global object called by the function call node is an object for accessing logs;

[0016] In the case where the global object called by the function call node is an object for accessing logs, determine whether the log management sub-object of the global object is a specified log management sub-object;

[0017] In the case where the log management sub-object of the global object is a specified log management sub-object, determine whether the log output mode of the function call node is a specified log output mode;

[0018] In the case where the log output mode of the function call node is a specified log output mode, determine that the function call node corresponds to a specified log statement.

[0019] Optionally, the optimization of the abstract syntax tree includes:

[0020] According to the abstract syntax tree, determine multiple classes and the inheritance relationships between the multiple classes;

[0021] According to the inheritance relationships between the multiple classes, determine whether the current class is multi-inherited by other classes;

[0022] In the case where the current class is not multi-inherited by other classes, add a specified assignment node for the current class in the abstract syntax tree according to the attribute set and function set of the current class.

[0023] Optionally, the adding a specified assignment node for the current class in the abstract syntax tree according to the attribute set and function set of the current class includes:

[0024] Obtain the attribute set and function set of the base class of the current class;

[0025] Determine whether the current class multi-inherits other classes;

[0026] Generate an information list according to the attribute set and function set of the current class, the attribute set and function set of the base class of the current class, and the judgment result;

[0027] Add a specified assignment node for the current class in the abstract syntax tree according to the information list.

[0028] Optionally, the optimization of the abstract syntax tree includes:

[0029] According to the abstract syntax tree, determine the nodes containing code blocks;

[0030] From the nodes containing code blocks, determine the sub-nodes containing strings;

[0031] Delete the string in the child node containing the string.

[0032] Optionally, the optimization of the abstract syntax tree includes:

[0033] Determine the child nodes of the function body according to the abstract syntax tree;

[0034] When the child node of the function body is a local variable, determine whether the child node of the function body corresponds to an assigned variable;

[0035] When the child node of the function body corresponds to an assigned variable, update the name information of the child node of the function body.

[0036] Optionally, it further includes:

[0037] Store the mapping relationship of the name information before and after the update;

[0038] According to the mapping relationship, replace the name information referring to the child node of the function body before the update with the name information referring to the child node of the function body after the update.

[0039] Optionally, the optimization of the abstract syntax tree includes:

[0040] Determine the variable assignment node according to the abstract syntax tree;

[0041] When the value corresponding to the variable assignment node is a constant, update the reference to the variable assignment node to a reference to the constant.

[0042] Optionally, the optimization of the abstract syntax tree includes:

[0043] Determine the declaration node according to the abstract syntax tree;

[0044] Determine the declared module according to the declaration node;

[0045] Delete the unused modules from the declared modules.

[0046] Optionally, the deleting the unused modules from the declared modules includes:

[0047] Determine the identifier node according to the abstract syntax tree;

[0048] Determine the modules other than the module corresponding to the identifier node as the unused modules from the declared modules, and delete the unused modules.

[0049] A device for optimizing game scripts, the device includes:

[0050] The first game script module is used to obtain a first game script and generate an abstract syntax tree for the first game script;

[0051] The abstract syntax tree optimization module is used to optimize the abstract syntax tree;

[0052] The second game script generation module is used to generate a second game script according to the optimized abstract syntax tree.

[0053] An electronic device includes a processor, a memory, and a computer program stored on the memory and capable of running on the processor. When the computer program is executed by the processor, the method for optimizing one or more game scripts as described above is implemented.

[0054] A computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the method for optimizing one or more game scripts as described above is implemented.

[0055] The embodiments of the present invention have the following advantages:

[0056] In the embodiments of the present invention, by obtaining a first game script, generating an abstract syntax tree for the first game script, optimizing the abstract syntax tree, and generating a second game script according to the optimized abstract syntax tree, it is possible to comprehensively optimize all game codes, rather than being limited to the bottleneck parts with serious performance consumption. There is no need for manual analysis of the bottleneck parts in the game script, and the game can be comprehensively accelerated to improve the user experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0057] In order to more clearly illustrate the technical solutions of the present invention, the drawings required for the description of the present invention will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0058] Figure 1 is a flowchart of the steps of a method for optimizing a game script provided by an embodiment of the present invention;

[0059] Figure 2a is a flowchart of the steps of another method for optimizing a game script provided by an embodiment of the present invention;

[0060] Figure 2b is a schematic diagram of deleting a specified log statement in an abstract syntax tree provided by an embodiment of the present invention;

[0061] Figure 3aIt is a flowchart of steps of another method for optimizing game scripts provided by an embodiment of the present invention;

[0062] Figure 3b It is a schematic diagram of adding a specified assignment node in an abstract syntax tree provided by an embodiment of the present invention;

[0063] Figure 4a It is a flowchart of steps of another method for optimizing game scripts provided by an embodiment of the present invention;

[0064] Figure 4b It is a schematic diagram of deleting code comments in an abstract syntax tree provided by an embodiment of the present invention;

[0065] Figure 5a It is a flowchart of steps of another method for optimizing game scripts provided by an embodiment of the present invention;

[0066] Figure 5b It is a schematic diagram of formatting local variable names in an abstract syntax tree provided by an embodiment of the present invention;

[0067] Figure 6a It is a flowchart of steps of another method for optimizing game scripts provided by an embodiment of the present invention;

[0068] Figure 6b It is a schematic diagram of replacing variable references with constants in an abstract syntax tree provided by an embodiment of the present invention;

[0069] Figure 7a It is a flowchart of steps of another method for optimizing game scripts provided by an embodiment of the present invention;

[0070] Figure 7b It is a schematic diagram of deleting useless code modules in an abstract syntax tree provided by an embodiment of the present invention;

[0071] Figure 8 It is a structural block diagram of a device for optimizing game scripts provided by an embodiment of the present invention. Detailed implementation manners

[0072] To make the above objects, features, and advantages of the present invention more obvious and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific implementation manners. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of them. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.

[0073] Refer to Figure 1, showing a flowchart of the steps of a method for optimizing a game script provided by an embodiment of the present invention, which may specifically include the following steps:

[0074] Step 101, obtain a first game script and generate an abstract syntax tree for the first game script;

[0075] Among them, the game script may include source code. As an example, the game script may be a game script written in the Python language. The abstract syntax tree may represent the source code structure of the game script in a tree-like form.

[0076] In a specific implementation, the source code of the first game script may be obtained, and then, based on the source code of the first game script, an abstract syntax tree may be generated. The abstract syntax tree may have multiple nodes, and the multiple nodes correspond to the structure of the source code of the first game script. For example, the source code of the first game script may include function calls, variable assignments, and their specific objects. Correspondingly, the generated abstract syntax tree may include function call nodes, variable assignment nodes, and sub-nodes corresponding to specific objects.

[0077] The nodes of the abstract syntax tree may be scanned to obtain the attribute information of the nodes, and then, based on the attribute information of the nodes, the type of the nodes may be determined, such as determining a function call node, a string node, or a variable assignment node.

[0078] As an example, the nodes of the abstract syntax tree may be scanned, and the nodes of type ast.Call may be determined as function call nodes.

[0079] Step 102, optimize the abstract syntax tree;

[0080] In a specific implementation, the nodes of the abstract syntax tree may be scanned to determine the type of the nodes, and then, corresponding optimization may be performed according to the type of the nodes. For example, some specific nodes (such as the nodes corresponding to code comments) may be deleted or replaced.

[0081] Step 103, generate a second game script according to the optimized abstract syntax tree.

[0082] The abstract syntax tree is generated according to the source code of the first game script. By optimizing the abstract syntax tree, the optimization of the first game script can be achieved. After optimizing the abstract syntax tree, a second game script for the optimized abstract syntax tree may be generated, so that the second game script can be run during the game operation.

[0083] In an embodiment of the present invention, by obtaining a first game script, generating an abstract syntax tree for the first game script, optimizing the abstract syntax tree, and generating a second game script based on the optimized abstract syntax tree, it is possible to comprehensively optimize all game codes, rather than being limited to the bottleneck parts with serious performance consumption. It is not necessary to manually analyze the bottleneck parts in the game script, and the game can be comprehensively accelerated to improve the user experience.

[0084] In practical applications, in order to facilitate the testing of game scripts and be able to see real-time running information on the console during runtime, debugging statements can be added during coding, such as log statements.

[0085] During the running process of the game script, the debugging statement will call the corresponding function and print out the log. In the official package of the game script, this may not be required.

[0086] Refer to Figure 2a , which shows the step flowchart of another method for optimizing game scripts provided by an embodiment of the present invention, and specifically may include the following steps:

[0087] Step 201, obtain a first game script and generate an abstract syntax tree for the first game script;

[0088] Step 202, determine function call nodes according to the abstract syntax tree;

[0089] The source code of the first game script may include function call statements. Correspondingly, the abstract syntax tree generated according to the first game script may include function call nodes.

[0090] In a specific implementation, the nodes of the abstract syntax tree can be traversed to determine function call nodes. As an example, nodes of the ast.Call type can be determined as function call nodes.

[0091] Step 203, determine whether the function call node corresponds to a specified log statement;

[0092] Among them, the specified log statement can be a statement that uses a debugging statement to print out the log.

[0093] As an example, the specified log statement can be a statement that uses a log statement to print out the log.

[0094] In the first game script, the corresponding debugging statement can be called through the specified log statement to print out the log. In the abstract syntax tree generated according to the first game script, the node corresponding to the specified log statement can be of the function call node type.

[0095] To achieve optimization, the nodes of the abstract syntax tree can be traversed to determine whether a function call node corresponds to a specified log statement. In the case where it is determined that the function call node corresponds to the specified log statement, the function call node can be deleted. For example, an empty statement "pass" can be used to replace the function call node corresponding to the specified log statement in the abstract syntax tree, thereby deleting the specified log statement, the function call to the specified log statement, and the formatting operation of the printed output log, and further optimizing the abstract syntax tree can be achieved.

[0096] In an embodiment of the present invention, step 203 may include the following sub-steps:

[0097] Sub-step S11, determining whether the global object called by the function call node is an object for accessing logs;

[0098] Among them, the object for accessing logs can be a debugging statement, which can be understood as an object for printing logs, that is, using the debugging statement to print logs.

[0099] As an example, the object for accessing logs can be the G object, which in the abstract syntax tree can be a node of string type. For example, the string "G". In the abstract syntax tree, the node corresponding to the called global object can be a child node of the function call node.

[0100] In practical applications, the corresponding debugging statement can be called through the specified log statement to print the output log. To achieve optimization, it can be determined whether the object called by the function call node is a global object. In the case where it is determined that the called object is a global object, it is further determined whether the called global object is an object for accessing logs.

[0101] Specifically, in the abstract syntax tree, the function call node can be accessed, and the node whose call object is a global object can be determined from its child nodes. Further, it can be determined whether the node whose call object is a global object corresponds to an object for accessing logs.

[0102] Sub-step S12, in the case where the global object called by the function call node is an object for accessing logs, determining whether the log management sub-object of the global object is a specified log management sub-object;

[0103] Among them, the specified log management sub-object can be an object for printing the output log among the objects for accessing logs. Such as the logger object.

[0104] The specified log management sub-object has a corresponding node in the abstract syntax tree, corresponding to the child node of the function call node.

[0105] In a specific implementation, when the global object called by the determination function call node is an object for accessing logs, it is further determined whether the sub-object for log management is a specified log management sub-object, such as the logger object. In the abstract syntax tree, the node corresponding to the specified log management sub-object can be a node of string type, such as the string "logger". When it is determined that the sub-object for log management is not the specified log management sub-object, the current function call node can be ignored.

[0106] Sub-step S13: When the log management sub-object of the global object is the specified log management sub-object, determine whether the log output mode of the function call node is the specified log output mode.

[0107] Among them, the specified log output mode can include one or more of info, debug, warning, and error.

[0108] In a specific implementation, when it is determined that the calling object of the function call node is the global object and the log management sub-object of the global object is an object for printing and outputting logs, it can be further determined whether the log output mode of the function call node is the specified log output mode. Furthermore, it can be determined whether the calling function node corresponds to the specified log statement according to whether the log output mode is the specified log output mode.

[0109] Sub-step S14: When the log output mode of the function call node is the specified log output mode, determine whether the function call node corresponds to the specified log statement.

[0110] Specifically, when it is determined that the log output mode of the function call node is the specified log output mode, such as the output being "debug", it can be determined that the function call node corresponds to a statement that uses the debug statement to print and output logs.

[0111] Step 204: When the function call node corresponds to the specified log statement, delete the specified log statement corresponding to the function call node in the abstract syntax tree.

[0112] When it is confirmed that the function call node corresponds to a statement that uses the debug statement to print and output logs, it can be considered to delete it. For example, the function call node can be replaced with an empty statement "pass" to complete the deletion of the corresponding specified log statement, and the optimization of the abstract syntax tree can be achieved to obtain the optimized abstract syntax tree.

[0113] Step 205: Generate the second game script according to the optimized abstract syntax tree.

[0114] For a better understanding of the above optimization process, the following is combined with the appendix Figure 2bA schematic diagram of deleting a specified log statement in an abstract syntax tree is described by way of example.

[0115] First, function call nodes in the abstract syntax tree can be traversed to identify one or more function call nodes.

[0116] In an actual game script, the log function can be called for debugging and the output log can be printed.

[0117] Furthermore, for a single function call node, it can be determined whether it is a node corresponding to a debugging statement. For example, whether it is a log node. It can be understood that for each function call node, it is determined whether it corresponds to a call to the log function. If it is determined that the function call node corresponds to a debugging statement, it indicates that it may be a statement that uses the debugging statement to print the output log.

[0118] Furthermore, for this function call node, it can be determined whether its global call object func.value.value is an object used to access the log. For example, whether it is the string G. If the judgment result is no, it indicates that the current function call node does not correspond to a statement that uses the debugging statement to print the output log, and it can be ignored. If the judgment result is yes, it indicates that the current function call node may correspond to a statement that uses the debugging statement to print the output log.

[0119] Furthermore, for this function call node, it can be determined whether its log management object func.value.attr of the global object is an object used to print the output log. For example, whether it is a logger object. If the judgment result is no, it indicates that the current function call node does not correspond to a statement that uses the debugging statement to print the output log, and it can be ignored. If the judgment result is yes, it indicates that the current function call node may correspond to a statement that uses the debugging statement to print the output log.

[0120] Furthermore, for this function call node, it can be determined whether its log output method func.attr property is info / debug / warning / error. If the judgment result is yes, it can be determined that the function call node corresponds to a statement that uses the debugging statement to print the output log, and it can be considered for deletion. For example, replace it with a pass statement. If the judgment result is no, it can be determined that the function call node does not correspond to a statement that uses the debugging statement to print the output log, and it can be ignored.

[0121] In an embodiment of the present invention, by obtaining a first game script, generating an abstract syntax tree for the first game script, determining function call nodes according to the abstract syntax tree, determining whether the function call nodes correspond to specified log statements, and in the case where the function call nodes correspond to the specified log statements, deleting the specified log statements corresponding to the function call nodes in the abstract syntax tree to obtain an optimized abstract syntax tree, and generating a second game script according to the optimized abstract syntax tree, it is possible to comprehensively optimize all game codes, rather than being limited to the bottleneck parts with serious performance consumption. There is no need for manual analysis of the bottleneck parts in the game script, and the game can be comprehensively accelerated to improve the user experience.

[0122] Moreover, statements that print and output logs using debug statements can be removed, so that the official package of the game script does not include statements that print and output logs using debug statements. When the second game script is run, function calls and formatting operations brought about by printing and outputting logs can be reduced.

[0123] In a game script, every time a class creates an instance (an object of the class), a dictionary dict will be allocated for the instance to store its attributes. Accessing the attributes of the instance can be achieved by accessing its corresponding dictionary dict.

[0124] Due to the data structure characteristics of the dictionary dict itself, it needs to occupy memory. When there are more instances of the class or more attributes of the instance, the memory consumption will become more serious. This may affect the running of the game script, manifested as poor performance, which is an optimization that can be considered.

[0125] Refer to Figure 3a , which shows a flowchart of the steps of another method for optimizing a game script provided by an embodiment of the present invention, and specifically may include the following steps:

[0126] Step 301, obtain a first game script and generate an abstract syntax tree for the first game script;

[0127] Step 302, determine multiple classes and the inheritance relationships between the multiple classes according to the abstract syntax tree;

[0128] In a specific implementation, according to the abstract syntax tree generated from the first game script, the code structure in the first game script can be presented in the form of nodes in the abstract syntax tree, and the classes in the source code are also presented in the abstract syntax tree. By scanning the nodes of the abstract syntax tree, the inheritance relationships between multiple classes can be determined.

[0129] As an example, all nodes in the abstract syntax tree can be scanned to identify function declaration nodes among them. For example, nodes of the ast.ClassDef type can be identified as function declaration nodes. Subsequently, the function declaration nodes can be accessed, and based on the keywords in the function declaration nodes, multiple classes and the inheritance relationships between multiple classes can be determined.

[0130] Step 303: According to the inheritance relationships between the multiple classes, determine whether the current class is multiply inherited by other classes;

[0131] Among them, being multiply inherited by other classes means that the subclasses inheriting the current class also inherit other classes. It can be understood that the current class is the base class of the multiply inherited class. Among them, the multiply inherited class can be a class with multiple base classes.

[0132] As an example, if class b that inherits current class a also inherits class c, then current class a is the base class of multiply inherited class b, and it can be considered that current class a is multiply inherited by other classes.

[0133] In specific implementation, according to the inheritance relationships between multiple classes, for each class, it can be determined whether it (hereinafter referred to as the current class) is multiply inherited by other classes.

[0134] Step 304: In the case where the current class is not multiply inherited by other classes, according to the attribute set and function set of the current class, add specified assignment nodes for the current class in the abstract syntax tree;

[0135] Among them, the specified assignment nodes can correspond to the list of attribute information of the object of the current class. It can be used to quickly access the attributes of the object of the current class. Such as the slots list.

[0136] To achieve optimization, an information list (such as the slots list) for the objects of the class can be created for the class, and the attributes common to all declared instances (objects of the class) are included in this information list. It can be understood as public attributes.

[0137] When creating an instance of a class, new attributes can be added to the instance. It can be understood that the attributes of the instance may change dynamically. To support the dynamic change of instance attributes, corresponding storage spaces can be added to the information list for the instance for each instance, for storing the private attributes of the corresponding instance in addition to the public attributes.

[0138] As an example, by default, the attributes of an instance of a class are stored in its dictionary dict, and the dictionary dict of the instance can be added to the information list (such as the slots list) for the instance of the class.

[0139] After creating an information list for storing the attributes of an instance of a class, the information list belongs to the class rather than the instance. When creating an instance of a class, it may not be necessary to allocate a dictionary (dict) for each instance. Accessing the attributes of an instance can be achieved by accessing this information list. Thus, memory can be saved and access can be accelerated.

[0140] In a specific implementation, when it is determined that the current class is not multi-inherited by other classes (i.e., the subclasses of the current class are not multi-inherited classes), the current class can be accessed to obtain the attributes of the instance, obtain the attribute set of the current class, and according to the attribute set, generate an information list of the current class (such as a slots list), and add a specified assignment node corresponding to the information list of the current class to the current class in the abstract syntax tree.

[0141] As an example, the first game script can be a Python script, and the specified assignment node can be a node corresponding to the slots list. Such as a slots assignment node. When it is determined that the current class is not multi-inherited by other classes, a slots list can be generated according to the attributes of the instance of the current class.

[0142] Specifically, due to the inherent characteristics of some languages, the ability to distinguish types is weak, and it may not be able to accurately directly identify attributes and functions. Such as the Python language. In an example, a.b = c cannot accurately distinguish whether b is a function or an attribute.

[0143] In practical applications, the current class can be accessed to directly obtain the attributes and functions of the instance, obtain the attribute set and function set of the current class. At this time, the function set is included in the attribute set, and the function set can be subtracted from the attribute set to obtain the final attribute set. Furthermore, an information list (such as a slots list) can be generated according to the final attribute set. Thus, a specified assignment node can be generated using this information list (such as a slots list) and added to the current class (class) in the abstract syntax tree.

[0144] It should be noted that there is an inheritance relationship between classes, and subclasses can inherit the attributes of the base class. To save memory, the part inherited from the base class can be deleted from the information list of the current class, and when it is necessary to access it, it can be achieved along the inheritance relationship.

[0145] In the case where a class is multi-inherited by other classes (i.e., the class that inherits the current class also inherits other classes), if the above-mentioned information list is generated for this class, due to the non-uniqueness of the inheritance relationship, it may not be possible to determine the information list of the corresponding base class along the inheritance relationship from the class that inherits it.

[0146] As an example, for class a, class b, class c that inherits class a and class b, when it is necessary to access the information list of the base class of class c, it is impossible to determine whether to access the information list of class a or the information list of class c.

[0147] In a specific implementation, when it is determined that the current class is multiply inherited by other classes, the current class can be accessed to obtain the set of attributes and functions for its instance, and the set of attributes and functions of the current class can be obtained and cached for convenient direct access.

[0148] In an embodiment of the present invention, step 304 may include the following sub-steps:

[0149] Sub-step S21, obtaining the set of attributes and functions of the base class of the current class;

[0150] Sub-step S22, determining whether the current class is multiply inherited by other classes;

[0151] Sub-step S23, generating an information list according to the set of attributes and functions of the current class, the set of attributes and functions of the base class of the current class, and the judgment result;

[0152] Among them, the set of attributes of the base class may be a set of attributes for an instance of the base class. The same applies to the function set. The information list can be used to quickly access class attributes. As an example, the first game script may be a python script, and the information list may be a slots list.

[0153] If the current class is not multiply inherited by other classes, it means that the subclass of the current class is a single-inheritance class, that is, the subclass that inherits the current class only inherits the current class. This means that the current class may be a single-inheritance class (i.e., only inheriting one base class) or a multiple-inheritance class (i.e., inheriting multiple base classes).

[0154] To achieve optimization, the judgment result of determining whether the current class is multiply inherited by other classes includes the cases of multiply inheriting other classes (i.e., multiple-inheritance class) and not multiply inheriting other classes (i.e., single-inheritance class). According to different judgment results, different methods can be adopted to generate an information list according to the set of attributes and functions of the current class and the set of attributes and functions of the base class of the current class.

[0155] Specifically, if the judgment result is that the current class is multiply inherited by other classes, the set of attributes of the current class plus the set of attributes of the base class can be used, and then the functions of the current class and the functions of the base class are subtracted. In this way, the attributes of the base class can be directly obtained from the information list of the current class without accessing according to the inheritance relationship. If the judgment result is that the current class is not multiply inherited by other classes, it can be determined that the current class is a single-inheritance class, and the set of attributes of the current class itself minus the set of attributes of the base class, its own functions and the functions of the base class can be used to obtain the final set of attributes of the current class. Furthermore, an information list (such as a slots list) can be generated according to the final set of attributes of the current class.

[0156] Note that the property set of the current class includes not only the public properties among the instances of the current class, but also the private properties of each instance. When generating the information list, for the private properties of each instance, an instance tag can be added to them, and then they can be stored in the storage space corresponding to each instance in the information list for storing its private properties (such as the dictionary dict corresponding to each instance in the information list).

[0157] Sub-step S24, according to the information list, add a specified assignment node to the current class in the abstract syntax tree.

[0158] In a specific implementation, after generating the information list for the current class, for use during the running of the game script, in the abstract syntax, a specified assignment node can be generated according to the information list, and the specified assignment node can be added to the current class. When accessing the properties of an instance of the current class, it can be directly implemented through the information list corresponding to the specified assignment node.

[0159] Step 305, generate a second game script according to the optimized abstract syntax tree.

[0160] To better understand the embodiments of the present invention, the following will be combined with the attached Figure 3b Schematic diagram of adding a specified assignment node in the abstract syntax tree as shown for exemplary illustration.

[0161] First, the inheritance information between multiple classes can be obtained through scanning, and the inheritance relationship between multiple classes can be obtained from it. Then, each class (the current class and the base class) can be accessed separately to obtain the property set and function set of each class (the properties and functions for the instances of the class).

[0162] Furthermore, according to the inheritance relationship between multiple classes obtained previously, it can be determined whether the current class is multi-inherited, that is, whether the class inheriting the current class also inherits other classes. If the determination result is yes, the property set and function set of the current class, as well as the property set and function set of the base class inherited by the current class, can be cached without generating a slots list.

[0163] In the case where it is determined that the current class is not multi-inherited, it can be further determined whether the current class multi-inherits other classes, that is, whether the base class of the current class is multiple.

[0164] If the determination result is yes, the final property set of the current class can be obtained by adding the property set of the current class and the property set of the base class, and subtracting the function set of the current class and the function set of the base class. In this way, the properties of the base class can be directly accessed in the slots list of the current class.

[0165] In the case where the determination result is "no", it can be considered that the current class only inherits one base class. The final attribute set of the current class can be obtained by subtracting the attribute set, function set of the base class, and the function set of the current class from the attribute set of the current class.

[0166] Finally, based on the final attribute set of the current class, a slots list can be generated, and corresponding slots assignment nodes can be generated according to the slots list. Add the slots assignment nodes for the current class in the abstract syntax tree. When running the game script, the attributes of the instance of the current class can be accessed by accessing the slots list corresponding to the slots assignment nodes.

[0167] In the embodiment of the present invention, by obtaining the first game script, generating an abstract syntax tree for the first game script, determining multiple classes and the inheritance relationship between the multiple classes according to the abstract syntax tree, judging whether the current class is multi-inherited by other classes according to the inheritance relationship between the multiple classes, and in the case where the current class is not multi-inherited by other classes, adding specified assignment nodes for the current class in the abstract syntax tree according to the attribute set and function set of the current class, and generating a second game script according to the optimized abstract syntax tree, it is possible to comprehensively optimize all game codes, rather than being limited to the bottleneck parts with serious performance consumption, and it is not necessary to manually analyze the bottleneck parts in the game script, so as to comprehensively accelerate the game and improve the user experience. Further, when the second game script runs, the attributes of the instance of the class can be quickly accessed by accessing the information list corresponding to the specified assignment nodes, and the memory occupancy is greatly reduced.

[0168] During the game script development process, code comments can be added to improve code readability. However, during the game script running, code comments do not play a positive role and may occupy memory, so it can be considered to optimize them.

[0169] Refer to Figure 4a , which shows the step flowchart of another game script optimization method provided by an embodiment of the present invention, and specifically may include the following steps:

[0170] Step 401, obtain the first game script and generate an abstract syntax tree for the first game script;

[0171] Step 402, determine the nodes containing code blocks according to the abstract syntax tree;

[0172] The nodes of the abstract syntax tree can be scanned to determine the nodes containing code blocks from them.

[0173] As an example, all nodes can be accessed to obtain their attributes. If a node has an attribute named (with the name) body / orelse / finalbody and the corresponding value is of list type, then it can be determined that the node is a node containing a code block.

[0174] Step 403: From the nodes containing code blocks, determine the child nodes containing strings;

[0175] In a game script, a code comment can be a string and is added after the corresponding code. Correspondingly, in an abstract syntax tree, a code comment can be a node of string type and is a child node corresponding to a node containing a code block.

[0176] To achieve optimization, after determining the nodes containing code blocks, the child nodes of the nodes containing code blocks can be traversed to determine the child nodes containing strings among them, and these child nodes can be regarded as the corresponding code comments. It can be understood that the child nodes under the nodes containing code blocks can be traversed, and the child nodes of string type among them can be determined as the nodes corresponding to the code comments.

[0177] Step 404: Delete the strings in the child nodes containing strings;

[0178] During the running of a game script, code comments may not be necessary and may occupy a certain amount of memory and consume performance. To achieve optimization, after determining the nodes corresponding to code comments in the abstract syntax tree, these nodes can be deleted.

[0179] As an example, an empty statement pass can be used to replace the nodes corresponding to code comments.

[0180] Step 405: Generate a second game script according to the optimized abstract syntax tree.

[0181] To better understand the embodiments of the present invention, the following is an exemplary illustration in conjunction with Figure 4b the schematic diagram of deleting code comments in the abstract syntax tree as shown.

[0182] First, after converting the first game script into an abstract syntax tree, the code nodes (all nodes) can be accessed. Then, for each node, it can be determined whether it contains a code block, that is, to determine the nodes corresponding to the code blocks from all nodes. Then, for the nodes containing code blocks, their child nodes can be traversed to determine whether the node (child node) is a node of string type. If so, it means that the child node corresponds to a code comment, and it can be replaced with an empty statement pass, so that there are no code comments in the second game script, and memory occupancy can be reduced during runtime.

[0183] In an embodiment of the present invention, by obtaining a first game script, generating an abstract syntax tree for the first game script, determining nodes containing code blocks according to the abstract syntax tree, determining child nodes containing strings from the nodes containing code blocks, deleting the strings in the child nodes containing strings, and generating a second game script according to the optimized abstract syntax tree, it is possible to comprehensively optimize all game codes, rather than being limited to the bottleneck parts with serious performance consumption. It is not necessary to manually analyze the bottleneck parts in the game script, and the game can be accelerated comprehensively to improve the user experience. Further, by deleting the code comments in the game script, a part of the memory occupancy can be reduced during the running of the game script, the code can be optimized, and the performance consumption can be reduced.

[0184] Due to the characteristics of some code languages, the same string is retained in memory once. When creating the same string later, a new space is not allocated, but the address of the string is assigned to the newly created variable. For example, the string interning mechanism (Interned mechanism) in Python.

[0185] In the game script, the more different local variables there are, the more local variable names are retained in memory, resulting in excessive memory occupancy and affecting performance.

[0186] Referring to Figure 5a , a flowchart of steps of another method for optimizing a game script provided by an embodiment of the present invention is shown, which may specifically include the following steps:

[0187] Step 501, obtain a first game script and generate an abstract syntax tree for the first game script;

[0188] Step 502, determine the child nodes of the function body according to the abstract syntax tree;

[0189] Among them, the function body can be the whole composed of all the codes of a function function in the game script, which can include a declaration part and a statement part (execution part).

[0190] To achieve optimization, the nodes of the abstract syntax tree can be traversed to determine the nodes corresponding to the function body from them, and then the child nodes of the function body can be determined according to the nodes corresponding to the function body.

[0191] As an example, under the nodes corresponding to the function body, the child nodes of the ast.FunctionDef type can be determined as the child nodes of the function body.

[0192] Step 503, when the child node of the function body is a local variable, determine whether the child node of the function body corresponds to an assigned variable;

[0193] Among them, the child nodes of the function body are local variables, which can be the corresponding declared variables, that is, a new local variable is declared, or they can be the corresponding references to the declared local variables.

[0194] Considering that global variables and function parameters are globally unique, optimization can be performed only on the names of local variables, without optimizing function parameters and global variables. To achieve the above optimization, it is necessary to distinguish local variables from global variables and function parameters to avoid optimizing global variables and function parameters.

[0195] For the child nodes of the function body, it can be determined whether they correspond to local variables. Furthermore, in the case where it is determined that they correspond to local variables, it can be further determined whether they correspond to assigned variables, that is, it can be determined whether they correspond to declared local variables.

[0196] In an embodiment of the present invention, step 503 may include the following sub-steps:

[0197] Sub-step S51, for the child nodes of the function body, determine whether the assignment corresponds to a global variable;

[0198] Among them, the assignment to a global variable can be that a variable is assigned to a global variable, that is, it is determined whether the variable in the child node of the current function body is assigned to a global variable.

[0199] As an example, a node of the ast.Global type can be determined to correspond to a global variable.

[0200] In addition, there is a possible situation: a variable is directly used without being assigned. For this type of variable, optimization may not be performed.

[0201] Sub-step S52, in the case where the child nodes of the function body do not correspond to an assignment to a global variable, determine whether the child nodes of the function body correspond to assigned variables.

[0202] In the case where it is determined that the assignment of the current node is not a global variable, it can be further determined whether the current node corresponds to an assigned variable, that is, it can be determined whether the current node corresponds to a declared variable. Since the object targeted is the child node of the function body, if it corresponds to a declared variable, it can be explained that the declared variable is a local variable.

[0203] Specifically, it can be determined whether the current node corresponds to an assigned variable according to whether the current node is an assignment node, whether the corresponding variable is referenced earlier, and whether it is a function parameter.

[0204] In the case where the current node is an assignment node, the corresponding variable is not a function parameter, and it is referenced earlier, it indicates that the variable corresponding to the reference in the current node is not the variable declared in the current node.

[0205] When the current node is an assignment node, the corresponding variable is not a function parameter, and it has not been referenced previously, it indicates that the current node corresponds to a newly declared variable, and it can be determined that the current node corresponds to the assigned variable.

[0206] Of course, when the variable in the current node is a function parameter, it can be ignored and no processing is done. Note that when the current node is not an assignment node, it can correspond to a reference variable.

[0207] Step 504, when the child node of the function body corresponds to an assigned variable, update the name information of the child node of the function body;

[0208] When it is determined that the child node of the function body corresponds to a declared variable, the name of the child node of the function body can be updated, that is, the name of the declared local variable is formatted. In this way, the names of multiple variables declared in different or the same functions are formatted into a unified format, which can reduce memory occupancy.

[0209] As an example, three local variables are declared: a_1, b_1, c_1, occupying three portions of memory. After optimizing the names of the local variables, the name information of these three local variables is formatted as var_1, only occupying one portion of memory. When there are multiple local variables in the game script, it can greatly reduce the memory occupancy.

[0210] Step 505, store the mapping relationship of the name information before and after the update;

[0211] Note that in the game script, to reference a local variable, it can be achieved through the name of the local variable.

[0212] After formatting the names of the declared local variables, the names of the local variables used when referencing the local variables can be further updated synchronously to maintain the reference relationship.

[0213] In the specific implementation, a mapping relationship can be established for the name information of the local variable before and after the update and stored, which can be used to synchronously update the name information during the process of referencing the local variable.

[0214] Step 506, according to the mapping relationship, replace the name information before the update of the child node of the function body being referenced with the name information after the update of the child node of the function body being referenced;

[0215] After establishing a mapping relationship for the name information of the local variable before and after the update, the identifier in the node corresponding to the referenced local variable (the name information of the local variable before the update) can be replaced with the updated name information to maintain the reference relationship.

[0216] In an embodiment of the present invention, step 503 may include the following sub-steps:

[0217] Sub-step S53, when the child node of the function body does not correspond to an assignment variable, determine whether the name information of the child node of the function body is in the mapping relationship.

[0218] Sub-step S54, when the name information of the child node of the function body is in the mapping relationship, update the name information of the child node of the function body with the updated name information according to the mapping relationship.

[0219] It is determined that the current node corresponds to an assignment variable when the current node is an assignment node, the corresponding variable is not a function parameter, and it has not been referenced previously. Then, if it is determined that the current node does not correspond to an assignment variable, it means that the current node may not be an assignment node, or the corresponding variable may be a function parameter, or the corresponding variable has been referenced previously.

[0220] It should be noted that in a game script, a reference is implemented through an identifier (the name information of a local variable).

[0221] When the child node of the function body does not correspond to an assignment variable, it can be further determined whether the current node corresponds to an identifier node.

[0222] When it is determined that the current node is an identifier node, it can be determined whether the current node corresponds to a reference function parameter or a global variable that previously assigned a value to a variable according to the identifier of the current node, that is, whether it corresponds to the global variable in sub-step S51.

[0223] When it is determined that the current node does not correspond to a reference function parameter and does not correspond to a global variable that previously assigned a value to a variable, it can be further determined whether the variable referenced by the current node is in the mapping relationship, that is, whether the identifier of the current node is the name information in the mapping relationship, otherwise it can be ignored and no processing is performed.

[0224] When it is determined that the variable referenced by the current node is in the mapping relationship, the identifier in the current node can be replaced with the updated name information according to the mapping relationship.

[0225] Step 507, generate a second game script according to the optimized abstract syntax tree.

[0226] For a better understanding of the embodiments of the present invention, the following is an exemplary description in conjunction with the Figure 5b schematic diagram showing the formatting of local variable naming in the abstract syntax tree.

[0227] It is possible to preset and maintain a local variable identifier stack for generating name information by incrementing, a mapping relationship stack for establishing and storing name information before and after updates, a function parameter set stack for recording function parameters, and a global variable set stack for recording global variables.

[0228] Among them, the set stack can be such that the elements in the stack are sets.

[0229] During the process of implementing optimization, in the abstract syntax tree, function declaration nodes can be determined, and then function parameters can be added to the top set of the function parameter set stack according to the function declaration.

[0230] Furthermore, in the abstract syntax tree, the function body can be traversed to determine the child nodes of the function body.

[0231] Furthermore, for the child nodes of the function body, it can be determined whether they are assigned to global variables, that is, in the case where the current node is determined to be an assignment node, it is further determined whether it is assigned to a global variable.

[0232] In the case where it is determined that the current node is assigned to a global variable, the variable in the current node can be added to the top set of the global variable set stack.

[0233] In the case where it is determined that the current node is not assigned to a global variable, it indicates that the current node may not be an assignment node, or may correspond to an assigned variable (i.e., corresponding to declaring a local variable), or may correspond to a reference variable. For the above possible situations, it can be further determined whether the current node is an assignment node for a new variable. Specifically, in the case where it is determined to be an assignment node, it is determined whether the corresponding variable is referenced first.

[0234] In the case where it is determined that the current node is an assignment node for a new variable, it indicates that a variable is declared in the function, and this variable is a local variable. A new name information can be taken from the local variable identifier stack to replace the name information of the variable in the current node, and a mapping relationship is established between the name information before and after the update in the mapping relationship stack.

[0235] In the case where it is determined that the current node is not an assignment node for a new variable, it indicates that it may correspond to a reference variable. For the above possible situations, it can be further traversed through the global traversal set stack and the function parameter set stack according to the identifier in the current node to determine whether the identifier of the current node is in the function parameter set stack or the global variable set stack.

[0236] In the case where it is determined that the identifier of the current node is not in the global traversal set stack and the function parameter set stack, the mapping relationship stack can be traversed according to the identifier of the current node. Otherwise, it is ignored.

[0237] In the case where the same identifier can be found in the mapping relationship stack, it indicates that the current node refers to a local variable. According to the mapping relationship stack, the identifier in the current node can be replaced with the updated name information.

[0238] In the case where the same identifier cannot be found in the mapping relationship stack, it indicates that the current node refers to a global variable and this global variable is not in the global variable set stack. To reduce the computational load, the identifier corresponding to the current node can be added to the global variable set stack, so that before traversing the mapping relationship stack, it can be determined to ignore it based on the identifier.

[0239] In the embodiments of the present invention, by obtaining a first game script and generating an abstract syntax tree for the first game script, according to the abstract syntax tree, determining the child nodes of the function body, in the case where the child nodes of the function body are local variables, determining whether the child nodes of the function body correspond to assigned variables, in the case where the child nodes of the function body correspond to assigned variables, updating the name information of the child nodes of the function body, storing the mapping relationship of the name information before and after the update, according to the mapping relationship, replacing the name information before the update of the child nodes referring to the function body with the name information after the update of the child nodes referring to the function body, and generating a second game script according to the optimized abstract syntax tree, it is possible to achieve comprehensive optimization for all game codes, rather than being limited to the bottleneck parts with serious performance consumption. It is not necessary to manually analyze the bottleneck parts in the game script, and the game can be comprehensively accelerated to improve the user experience. Further, by uniformly formatting the local naming, a certain number of strings can be reduced, and during the running of the game script, the memory occupation can be reduced.

[0240] In game scripts, the situation where one module refers to variables in other modules is often involved, which is achieved by referring to variable names. This makes it necessary to execute the import operation (import operation) of the referenced module and the access operation of the variable name when the game script runs, which increases the consumption and affects the performance.

[0241] Refer to Figure 6a , which shows the step flowchart of another method for optimizing game scripts provided by an embodiment of the present invention, and specifically may include the following steps:

[0242] Step 601, obtain a first game script and generate an abstract syntax tree for the first game script;

[0243] Step 602, determine variable assignment nodes according to the abstract syntax tree;

[0244] It is possible to traverse the abstract syntax tree nodes and determine variable assignment nodes from them, that is, the nodes corresponding to variable assignments.

[0245] Step 603, when the value corresponding to the variable assignment node is a constant, update the reference to the variable assignment node to a reference to the constant.

[0246] After determining the variable assignment node, it is possible to further determine whether the value corresponding to the variable assignment node is a constant, that is, to determine whether the assignment of the variable is a constant.

[0247] For each variable assignment node, when it is determined that the assignment of the variable is a constant, it is possible to further determine the node that references the variable assignment node, that is, to further determine the variable reference node that references the variable.

[0248] Furthermore, it is possible to update the reference to the variable assignment node to a reference to the corresponding constant, that is, to replace the variable referenced in the variable reference node with the constant assigned to the variable during assignment.

[0249] In this way, it is possible to directly obtain the constant corresponding to the variable without using the variable name, reducing the access operations to the variable name. At the same time, it is also possible to reduce the import operations of the module.

[0250] In an embodiment of the present invention, step 603 may include the following sub-steps:

[0251] Sub-step S61, for each variable assignment node, when the corresponding variable meets the constant standard, cache the variable.

[0252] The variable assignment node can be used to assign a value to a variable. When the assignment of the variable is a constant, it can be considered that the variable meets the constant standard, and the variable can be cached for convenient subsequent use, avoiding multiple scans of the abstract syntax tree.

[0253] In one example, ordinary numerical values and strings can be used as constants.

[0254] As an example, if variable a is assigned the value 1 and the assignment 1 is a constant, it can be determined that variable a meets the constant standard, and it can be regarded as a constant and cached.

[0255] Sub-step S62, when the corresponding variable does not meet the constant standard, determine whether the current variable assignment node is used to modify a cached variable.

[0256] In practical applications, some variables are assigned as constants in one variable assignment node and are cached as constants. In other variable assignment nodes, the variable can be modified. If a variable is assigned a constant and then modified, the variable can be modified and is not a true constant.

[0257] For optimization, variables that can be modified can be deleted from the cache.

[0258] Sub-step S63: Determine variable reference nodes according to the abstract syntax tree;

[0259] Sub-step S64: When the variable reference nodes reference cached variables, replace the variables referenced by the variable reference nodes with the corresponding assignments of the cached variables.

[0260] Specifically, all nodes of the abstract syntax tree can be traversed to determine variable reference nodes from them, that is, to determine the nodes that reference variables, and then it can be judged whether the variables referenced by the variable reference nodes are variables in the cache.

[0261] When judging whether the variables referenced by the variable reference nodes are variables in the cache, the corresponding assignments of the variables can be used to replace the referenced variables in the variable reference nodes, which can enable the direct use of constants without accessing through variable names.

[0262] Step 604: Generate a second game script according to the optimized abstract syntax tree.

[0263] To better understand the embodiments of the present invention, the following combines the Figure 6b Schematic diagram of constant replacement for variable references in the abstract syntax tree shown below for exemplary illustration.

[0264] First, the nodes in the abstract syntax tree can be traversed for the first time. During this process, for each node, it can be judged whether it is a variable assignment node. If it is determined that it is not a variable assignment node, the node can be ignored without any processing.

[0265] If it is determined that the current node is a variable assignment node, it can be further judged whether the corresponding variable meets the constant standard. If it is determined that the variable corresponding to the current node meets the constant standard, the variable can be regarded as a constant and cached.

[0266] If it is determined that the variable corresponding to the current node does not meet the constant standard, it can be further judged whether the variable assignment node modifies a cached variable. If it is determined that it modifies a cached variable, it means that the variable corresponding to the current node (that is, the cached variable) is not a real constant, and the variable can be removed from the cache. If it is determined that the current node does not modify a cached variable, it can be ignored without any processing. In this way, the cached variables can be regarded as real constants and will not be modified.

[0267] Furthermore, a second traversal can be performed on the nodes of the abstract syntax tree. During this process, for each node, it can be determined whether it is a variable reference node. When it is determined that the current node is a variable reference node, it can be further determined whether the variable referenced by the current node is a cached variable. If it is determined to be the case, the assignment corresponding to the cached variable can be used to replace the variable referenced by the current node, so that the variable does not need to be referenced by its name, and the assignment of the referenced variable can be directly used, reducing the access operation to the variable name and also reducing the import operation of the module.

[0268] In an embodiment of the present invention, by obtaining a first game script and generating an abstract syntax tree for the first game script, and determining a variable assignment node according to the abstract syntax tree, when the value corresponding to the variable assignment node is a constant, updating the reference to the variable assignment node to reference the constant, and generating a second game script according to the optimized abstract syntax tree, comprehensive optimization of all game codes can be achieved, rather than being limited to the bottleneck parts with serious performance consumption. It is not necessary to manually analyze the bottleneck parts in the game script, and the game can be accelerated comprehensively to improve the user experience.

[0269] In a game script, an import operation (import operation) is required to use a module. Sometimes, a module that is not used is also imported. For example, there are modules that do not need to be used among multiple modules imported in a batch processing manner.

[0270] During the running process of the game script, the modules that have been imported but not used are useless. These unused modules will occupy memory and affect performance, which is an optimization that can be considered.

[0271] Refer to Figure 7a , which shows a flowchart of steps of another method for optimizing a game script provided by an embodiment of the present invention, and specifically may include the following steps:

[0272] Step 701, obtain a first game script and generate an abstract syntax tree for the first game script;

[0273] Step 702, determine a declaration node according to the abstract syntax tree;

[0274] Among them, the declaration node can be a module import node for importing a module, and can be used to declare the imported module.

[0275] As an example, the declaration node can be a node of the import type.

[0276] In a specific implementation, nodes of the abstract syntax tree can be scanned to determine nodes corresponding to function bodies therefrom, and then, based on the nodes of the function bodies, declaration nodes for module imports can be determined, that is, module import nodes can be determined.

[0277] Step 703: Determine the declared modules according to the declaration nodes.

[0278] To achieve optimization, the declared modules can be determined according to the declaration nodes, that is, the imported modules can be determined according to the module import nodes.

[0279] Step 704: Delete unused modules from the declared modules.

[0280] During the process of determining module import nodes based on the abstract syntax tree, it can be determined whether each node is a module import node. If it is determined not to be, it can be determined whether it is an identifier node for using a module.

[0281] Using a module can be achieved through an identifier corresponding to the module. In the abstract syntax tree, using a module can correspond to an identifier node.

[0282] To achieve optimization, the modules used in the imported modules can be determined at the identifier node, and then the unused modules can be determined. Further, the unused modules can be deleted from the declared modules.

[0283] In an embodiment of the present invention, step 704 may include the following sub-steps:

[0284] Sub-step S71: Determine identifier nodes according to the abstract syntax tree.

[0285] Sub-step S72: Determine, from the declared modules, the modules other than the modules corresponding to the identifier nodes as unused modules, and delete the unused modules.

[0286] Step 705: Generate a second game script according to the optimized abstract syntax tree.

[0287] To better understand the embodiments of the present invention, the following can be combined with Figure 7b The schematic diagram of deleting unused modules in the abstract syntax tree shown below for exemplary illustration.

[0288] It can be preset to maintain a stack of imported module sets to record the imported modules, and a stack of used modules to record the used modules.

[0289] In the process of implementing optimization, a first pass traversal can be performed on the nodes of the abstract syntax tree. Among them, for each node, it can be determined whether it is a module import node, that is, whether it is a declaration node for a module.

[0290] In the case where it is determined that the current node is a module import node, the imported module (the identifier of the module) can be added to the top set of the import module set stack. In the case where it is determined that the current node is not a module import node, it can be further determined whether it is a module identifier node, that is, whether it is a node for using a module. If it is for using a module, it has the identifier of the corresponding module.

[0291] In the case where it is determined that it is a module identifier node, the import module set stack and the used module set stack can be traversed from the top to the bottom according to the corresponding identifier.

[0292] During the traversal, when the identifier of the current node is in the used module set stack, it means that the corresponding module has been used, and the current node can be ignored without any processing.

[0293] When the identifier of the current node is not in the used module set stack and is in the import module set stack, it means that the module corresponding to the identifier has been used. The module corresponding to the identifier can be deleted from the import module set stack and added to the used module set stack. After traversing, the remaining modules in the import module set stack are the imported and unused ones.

[0294] To achieve optimization, a second pass traversal can be performed on the nodes of the abstract syntax tree, and then for each node, it can be determined whether it is a module import node. In the case where it is determined that the current node is a module import node, it can be further determined whether the module imported by the current node is in the import module set stack. In the case where it is determined that it is, the current node can be deleted to complete the elimination of useless modules.

[0295] In the embodiment of the present invention, by obtaining the first game script, generating an abstract syntax tree for the first game script, determining declaration nodes according to the abstract syntax tree, determining the declared modules according to the declaration nodes, deleting the unused modules from the declared modules, and generating a second game script according to the optimized abstract syntax tree, it is possible to achieve comprehensive optimization for all game codes, rather than being limited to the bottleneck parts with serious performance consumption. There is no need for manual analysis of the bottleneck parts in the game script, and the game can be comprehensively accelerated to improve the user experience.

[0296] It should be noted that, for method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should be aware that the embodiments of the present invention are not limited by the described action sequences, because according to the embodiments of the present invention, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily essential for the embodiments of the present invention.

[0297] Referring to Figure 8 , a schematic structural diagram of a device for optimizing a game script provided by an embodiment of the present invention is shown, which may specifically include the following modules:

[0298] The first game script module 801 is used to obtain a first game script and generate an abstract syntax tree for the first game script;

[0299] The abstract syntax tree optimization module 802 is used to optimize the abstract syntax tree;

[0300] The second game script generation module 803 is used to generate a second game script according to the optimized abstract syntax tree.

[0301] In an embodiment of the present invention, the abstract syntax tree optimization module 802 may include:

[0302] The function call node determination sub-module is used to determine function call nodes according to the abstract syntax tree;

[0303] The specified log statement judgment sub-module is used to judge whether the function call nodes correspond to specified log statements;

[0304] The specified log statement deletion sub-module is used to delete the specified log statements corresponding to the function call nodes in the abstract syntax tree when the function call nodes correspond to specified log statements.

[0305] In an embodiment of the present invention, the specified log statement judgment sub-module may include:

[0306] The access log object judgment unit is used to judge whether the global object called by the function call node is an object for accessing logs;

[0307] The specified log management sub-object judgment unit is used to judge whether the log management sub-object of the global object is a specified log management sub-object when the global object called by the function call node is an object for accessing logs;

[0308] A specified log output mode determination unit, configured to determine whether the log output mode of the function call node is a specified log output mode when the log management sub-object of the global object is a specified log management sub-object;

[0309] A specified log statement determination unit, configured to determine the specified log statement corresponding to the function call node when the log output mode of the function call node is a specified log output mode.

[0310] In an embodiment of the present invention, the abstract syntax tree optimization module 802 may include:

[0311] A class inheritance relationship sub-module, configured to determine multiple classes and the inheritance relationship between the multiple classes according to the abstract syntax tree;

[0312] A multi-inheritance determination sub-module by other classes, configured to determine whether the current class is multi-inherited by other classes according to the inheritance relationship between the multiple classes;

[0313] A specified assignment node addition sub-module, configured to add a specified assignment node for the current class in the abstract syntax tree according to the attribute set and function set of the current class when the current class is not multi-inherited by other classes.

[0314] In an embodiment of the present invention, the specified assignment node addition sub-module may include:

[0315] A base class attribute set and function set acquisition unit, configured to acquire the attribute set and function set of the base class of the current class;

[0316] A multi-inheritance of other classes unit, configured to determine whether the current class multi-inherits other classes;

[0317] An information list generation unit, configured to generate an information list according to the attribute set and function set of the current class, the attribute set and function set of the base class of the current class, and the determination result;

[0318] A specified assignment node addition unit, configured to add a specified assignment node for the current class in the abstract syntax tree according to the information list.

[0319] In an embodiment of the present invention, the abstract syntax tree optimization module 802 may include:

[0320] A node determination sub-module for a code block-containing node, configured to determine a node containing a code block according to the abstract syntax tree;

[0321] A sub-node determination sub-module for a string-containing node, configured to determine a sub-node containing a string from the node containing a code block;

[0322] A string sub-deletion module, which is used to delete the string in the sub-node containing the string.

[0323] In an embodiment of the present invention, the abstract syntax tree optimization module 802 may include:

[0324] A sub-node determination sub-module of the function body, which is used to determine the sub-nodes of the function body according to the abstract syntax tree;

[0325] A sub-node corresponding assignment variable sub-module, which is used to determine whether the sub-node of the function body corresponds to an assignment variable when the sub-node of the function body is a local variable;

[0326] A name information update sub-module, which is used to update the name information of the sub-node of the function body when the sub-node of the function body corresponds to an assignment variable.

[0327] In an embodiment of the present invention, the abstract syntax tree optimization module 802 may further include:

[0328] A mapping relationship storage sub-module, which is used to store the mapping relationship of the name information before and after the update;

[0329] A replace name information sub-module, which is used to replace the name information before the update of the sub-node of the function body with the name information after the update of the sub-node of the function body according to the mapping relationship.

[0330] In an embodiment of the present invention, the abstract syntax tree optimization module 802 may include:

[0331] A variable assignment node determination sub-module, which is used to determine the variable assignment node according to the abstract syntax tree;

[0332] A variable assignment node update sub-module, which is used to update the reference to the variable assignment node to the reference to the constant when the value corresponding to the variable assignment node is a constant.

[0333] In an embodiment of the present invention, the abstract syntax tree optimization module 802 may include:

[0334] A declaration node determination sub-module, which is used to determine the declaration node according to the abstract syntax tree;

[0335] A declared module determination sub-module, which is used to determine the declared module according to the declaration node;

[0336] An unused module deletion sub-module, which is used to delete the unused modules from the declared modules.

[0337] In an embodiment of the present invention, the unused module deletion sub-module may include:

[0338] An identifier node determination unit for determining an identifier node according to the abstract syntax tree;

[0339] An unused module deletion unit for determining, from the declared modules, modules other than the module corresponding to the identifier node as unused modules and deleting the unused modules.

[0340] An embodiment of the present invention further provides an electronic device, which may include a processor, a memory, and a computer program stored on the memory and capable of running on the processor. When the computer program is executed by the processor, the method for optimizing the game script as described above is implemented.

[0341] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for optimizing the game script as described above is implemented.

[0342] For the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple. For the relevant parts, refer to the partial description of the method embodiment.

[0343] Each embodiment in this specification is described in a progressive manner. Each embodiment focuses on the differences from other embodiments. For the same or similar parts among the embodiments, reference may be made to each other.

[0344] Those skilled in the art should understand that the embodiments of the present invention may be provided as a method, a device, or a computer program product. Therefore, the embodiments of the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memories, CD-ROMs, optical memories, etc.) containing computer-usable program codes.

[0345] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the method, terminal device (system), and computer program product according to the embodiments of the present invention. It should be understood that each flow and / or block in the flowchart and / or block diagram can be implemented by computer program instructions, and the combination of the flows and / or blocks in the flowchart and / or block diagram can also be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal devices generate a device for implementing the specified functions in Figure 1 one or more flows or multiple flows and / or blocks Figure 1 one or more blocks or multiple blocks.

[0346] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to operate in a particular manner, such that the instructions stored in the computer-readable memory produce a manufacture including an instruction device that implements the functions specified in one or more of the processes and / or blocks Figure 1 of one or more of the processes and / or blocks Figure 1 specified in one or more of the blocks or blocks.

[0347] These computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process, whereby the instructions executed on the computer or other programmable apparatus provide steps for implementing the functions specified in one or more of the processes and / or blocks Figure 1 of one or more of the processes and / or blocks Figure 1 specified in one or more of the blocks or blocks.

[0348] Although the preferred embodiments of the embodiments of the present invention have been described, those skilled in the art can make additional changes and modifications once they learn the basic creative concept. Therefore, the appended claims are intended to be construed to include the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.

[0349] Finally, it should also be noted that in this document, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variation thereof is intended to cover a non-exclusive inclusion, such that a process, method, article or apparatus comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or apparatus. Without further limitation, an element defined by the phrase "comprising an..." does not exclude the presence of additional identical elements in the process, method, article or apparatus comprising the element.

[0350] The above has introduced in detail the method and apparatus for optimizing game scripts, electronic devices, and storage media provided. Specific examples are used in this article to elaborate on the principles and implementation manners of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manners and application scopes. In summary, the content of this specification should not be construed as a limitation to the present invention.

Claims

1. A method for optimizing game scripts, characterized in that, The method includes: Obtain a first game script and generate an abstract syntax tree for the first game script; Optimize the abstract syntax tree, including: Determine function call nodes according to the abstract syntax tree; Judge whether the function call nodes correspond to specified log statements, including: judging whether the global object called by the function call nodes is an object for accessing logs; in the case where the global object called by the function call nodes is an object for accessing logs, judging whether the log management sub-object of the global object is a specified log management sub-object; in the case where the log management sub-object of the global object is a specified log management sub-object, judging whether the log output mode of the function call nodes is a specified log output mode; in the case where the log output mode of the function call nodes is a specified log output mode, judging whether the function call nodes correspond to specified log statements; In the case where the function call nodes correspond to specified log statements, delete the specified log statements corresponding to the function call nodes in the abstract syntax tree; Generate a second game script according to the optimized abstract syntax tree.

2. The method according to claim 1, wherein The optimizing the abstract syntax tree includes: Determine multiple classes and the inheritance relationships between the multiple classes according to the abstract syntax tree; Judge whether the current class is multiply inherited by other classes according to the inheritance relationships between the multiple classes; In the case where the current class is not multiply inherited by other classes, add specified assignment nodes for the current class in the abstract syntax tree according to the attribute set and function set of the current class.

3. The method according to claim 2, wherein The adding specified assignment nodes for the current class in the abstract syntax tree according to the attribute set and function set of the current class includes: Obtain the attribute set and function set of the base class of the current class; Judge whether the current class multiply inherits other classes; Generate an information list according to the attribute set and function set of the current class, the attribute set and function set of the base class of the current class, and the judgment result; Add specified assignment nodes for the current class in the abstract syntax tree according to the information list.

4. The method according to claim 1, wherein The optimizing the abstract syntax tree includes: Determine nodes containing code blocks according to the abstract syntax tree; Determine child nodes containing strings from the nodes containing code blocks; Delete the strings in the child nodes containing strings.

5. The method according to claim 1, wherein The optimizing the abstract syntax tree includes: Determine child nodes of a function body according to the abstract syntax tree; In the case where the child nodes of the function body are local variables, judge whether the child nodes of the function body correspond to assignment variables; In the case where the child nodes of the function body correspond to assignment variables, update the name information of the child nodes of the function body.

6. The method according to claim 5, wherein It also includes: Store the mapping relationship of the name information before and after the update; According to the mapping relationship, replace the name information before the update of the child nodes of the function body with the name information after the update of the child nodes of the function body.

7. The method according to claim 1, wherein The optimizing the abstract syntax tree includes: Determine variable assignment nodes according to the abstract syntax tree; When the value corresponding to the variable assignment node is a constant, update the reference to the variable assignment node to reference the constant.

8. The method according to claim 1, characterized in that, The optimization of the abstract syntax tree includes: Determine declaration nodes according to the abstract syntax tree; Determine the declared modules according to the declaration nodes; Delete unused modules from the declared modules.

9. The method according to claim 8, characterized in that The step of deleting unused modules from the declared modules includes: Determine identifier nodes according to the abstract syntax tree; Determine, from the declared modules, the modules other than the module corresponding to the identifier node as unused modules, and delete the unused modules.

10. An apparatus for optimizing game scripts, characterized in that, The device includes: A first game script module for obtaining a first game script and generating an abstract syntax tree for the first game script; An abstract syntax tree optimization module for optimizing the abstract syntax tree, including: A function call node determination sub-module for determining function call nodes according to the abstract syntax tree; A specified log statement judgment sub-module for judging whether the function call nodes correspond to specified log statements, including; An access log object judgment unit for judging whether the global object called by the function call node is an object for accessing logs; A specified log management sub-object judgment unit for judging whether the log management sub-object of the global object is a specified log management sub-object when the global object called by the function call node is an object for accessing logs; A specified log output method judgment unit for judging whether the log output method of the function call node is a specified log output method when the log management sub-object of the global object is a specified log management sub-object; A specified log statement judgment unit for judging whether the function call node corresponds to a specified log statement when the log output method of the function call node is a specified log output method; A specified log statement deletion sub-module for deleting the specified log statement corresponding to the function call node from the abstract syntax tree when the function call node corresponds to a specified log statement; A second game script generation module for generating a second game script according to the optimized abstract syntax tree.

11. An electronic device, characterized in that, It includes a processor, a memory, and a computer program stored on the memory and capable of running on the processor. When the computer program is executed by the processor, it implements the game script optimization method according to any one of claims 1 to 9.

12. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium. When the computer program is executed by the processor, it implements the game script optimization method according to any one of claims 1 to 9.

Citation Information

Patent Citations

  • Code processing method and device thereof

    CN111249736A