Character string updating method and device
By dynamically updating strings in preset memory blocks, the memory overhead and garbage collection problems caused by traditional string object immutability are solved, and the efficiency and performance of string operations are improved.
Patent Information
- Application Number
- CN202411351744.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2024-12-20
AI Technical Summary
Traditional string objects are immutable after creation, resulting in the creation of new string instances when performing modification operations such as splicing, replacement, and case conversion, resulting in additional memory overhead and frequent garbage collection, resulting in a large amount of memory fragmentation.
By determining the string memory in the preset memory block and creating a string object in the string memory, parsing the string operation request, obtaining the initial string and string operation information, storing the initial string to the string object, and Update the string operation information to the target string, update the string dynamically to avoid frequent memory allocation and garbage collection.
Improve business performance, reduce the frequency of memory allocation and garbage collection, optimize the efficiency of string operation, and avoid the generation of memory fragmentation.
Smart Images

Figure CN119166547A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a string updating method and device. Background Art
[0002] In programming, a string is a series of zero or more characters. It is usually used to represent text data and is one of the most important data types. Processing strings is a basic requirement for many programs. However, traditional string objects are immutable after creation. Once a string is created, its content cannot be changed. This immutability brings advantages but also obvious disadvantages. When a string needs to be modified by concatenation, replacement, case conversion, etc., a new string instance will be created, which not only leads to additional memory overhead, but also increases the frequency of garbage collection and generates a large amount of memory fragmentation. Therefore, a more effective string update method is urgently needed to solve the above problems. Summary of the invention
[0003] In view of this, the embodiment of the present application provides a string updating method to solve the technical defects existing in the prior art. The embodiment of the present application also provides a string updating device, a computing device, a computer-readable storage medium, and a computer program product.
[0004] According to a first aspect of an embodiment of the present application, a method for updating a character string is provided, comprising: In response to a string operation request, determining a string memory in a preset memory block, and creating a string object in the string memory; Parsing the string operation request to obtain an initial string and string operation information; The initial string is stored in the string object, and the initial string is updated to a target string according to the string operation information.
[0005] Optionally, creating a string object in the string memory includes: Determine string layout information, and divide the string memory into initial string data memory and string dynamic memory according to the string layout information; The original string information corresponding to the string operation request is stored in the initial string data memory to obtain the string data memory; The string object is created based on the string data memory and the string dynamic memory.
[0006] Optionally, determining the string memory in a preset memory block in response to the string operation request includes: In response to the string operation request, determining a string scope corresponding to the string operation request; In the case where the string scope is associated with the target function, determining the preset memory block in the stack, and determining the string memory in the preset memory block; When the string scope is not associated with the target function and the string operation task is completed, the preset memory block is determined in the memory pool, and the string memory is determined in the preset memory block.
[0007] Optionally, after parsing the string operation request and obtaining the initial string and string operation information, the step further includes: In a case where it is determined based on the initial string and the string operation information that the string operation information does not satisfy a string operation memory condition, generating memory requirement information, and determining a target string memory in the preset memory block based on the memory requirement information; The target string memory is used as the string memory, and the step of creating a string object in the string memory is performed.
[0008] Optionally, after the initial character string is updated to a target character string according to the character string operation information, the method further includes: Execute the string operation task corresponding to the string operation request based on the target string; When the string scope is associated with a target function and the target function is executed, releasing the string memory associated with the stack; When the string scope is not associated with the target function and the string operation task is completed, the string memory associated with the memory pool is released.
[0009] Optionally, updating the initial character string to a target character string according to the character string operation information includes: Determining the string merging information based on the string operation information, and determining the merged string and the merged string included in the initial string; The merged character string and the merged character string are merged according to the character string merging information to obtain the target character string.
[0010] Optionally, updating the initial character string to a target character string according to the character string operation information includes: Determining the string modification information based on the string operation information, and determining the string to be modified contained in the initial string; The character string to be modified is modified according to the character string modification information to obtain the target character string.
[0011] According to a second aspect of an embodiment of the present application, a string updating device is provided, including: a determination module, configured to determine a string memory in a preset memory block in response to a string operation request, and create a string object in the string memory; A parsing module, configured to parse the string operation request to obtain an initial string and string operation information; The updating module is configured to store the initial string into the string object, and update the initial string into a target string according to the string operation information.
[0012] According to a third aspect of an embodiment of the present application, a computing device is provided, including: Memory and processor; The memory is used to store computer executable instructions, and the processor implements the steps of the string updating method when executing the computer executable instructions.
[0013] According to a fourth aspect of the embodiments of the present application, a computer-readable storage medium is provided, which stores computer-executable instructions, and when the instructions are executed by a processor, the steps of the string updating method are implemented.
[0014] According to a fifth aspect of the embodiments of the present application, a chip is provided, which stores a computer program, and the computer program implements the steps of the string updating method when executed by the chip.
[0015] According to a sixth aspect of the embodiments of this specification, a computer program product is provided, including a computer program or instructions, which implement the steps of the above-mentioned string updating method when executed by a processor.
[0016] The string update method provided in the present application determines the string memory in a preset memory block in response to a string operation request, and creates a string object in the string memory. Parse the string operation request to obtain the initial string and string operation information. Store the initial string in the string object, and update the initial string to the target string according to the string operation information. When there is a string update requirement, use the preset memory block to update the string, and dynamically update the string in the preset memory block, avoiding frequent memory allocation and garbage collection operations, and improving business performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 is a flow chart of a string updating method provided by an embodiment of the present application; Figure 2It is a processing flow chart of a string update method applied to C# string update provided by an embodiment of the present application; Figure 3 It is a schematic diagram of the memory layout of a string object of a string updating method provided in an embodiment of the present application; Figure 4 This is a string object value operation diagram of a string update method provided in an embodiment of the present application; Figure 5 is a structural diagram of a string updating device provided by an embodiment of the present application; Figure 6 It is a structural block diagram of a computing device provided in one embodiment of the present application. DETAILED DESCRIPTION
[0018] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0019] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms of "a", "said" and "the" used in one or more embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more associated listed items.
[0020] It should be understood that, although the terms first, second, etc. may be used to describe various information in one or more embodiments of the present application, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first.
[0021] First, the terms involved in one or more embodiments of the present invention are explained.
[0022] C#: An object-oriented programming language, C# is designed to maintain close ties with C and C++ while providing a more modern, secure, component-oriented programming environment. C# is widely used to develop Windows client applications, server-side applications, database applications, Web applications, Web services, mobile device applications, and games.
[0023] Stack: A data structure used to store local variables, function parameters, return addresses, and other information during program execution. It follows the LIFO (Last In First Out) principle, that is, the data pushed into the stack last is popped out first.
[0024] Memory Pool: A collection of pre-allocated memory blocks of equal or different sizes used to meet the memory requirements of a program when it is running. Using a memory pool can reduce the number of memory allocations and deallocations at the operating system level, thereby improving program performance.
[0025] Python: A widely used high-level programming language known for its concise and readable syntax, dynamic type system, and interpreted execution. Python supports multiple programming paradigms, including object-oriented, imperative, and functional programming.
[0026] JavaScript (JS): A lightweight interpreted programming language that is one of the cornerstones of web development and is used to implement complex interactivity and dynamic content in the browser. JavaScript is a multi-paradigm language that supports object-oriented, imperative, and functional programming styles.
[0027] In the present application, a string updating method is provided. The present application also relates to a string updating device, a computing device, a computer-readable storage medium, and a computer program product. The following embodiments will describe them in detail one by one.
[0028] Figure 1 A flowchart of a string updating method provided according to an embodiment of the present application is shown, which specifically includes the following steps: Step 102: In response to a string operation request, a string memory is determined in a preset memory block, and a string object is created in the string memory.
[0029] Specifically, a string operation request can be submitted for a string object in a programming language such as C#, Python, or JavaScript, and is used to perform modification operations such as concatenation and replacement on the string in the string object. The preset memory block is a large pre-allocated memory block used to complete string modification operations. String memory refers to a memory block selected from the preset memory block for processing string operations corresponding to string operation requests. String objects are used to carry and record strings corresponding to string operation requests.
[0030] Based on this, a preset memory block is determined. A string operation request submitted for a string is accepted, and in response to the string operation request, a string memory for processing the string is determined in the preset memory block, and a string object is created in the string memory, and after the string is stored in the string object, subsequent operations on the string are performed.
[0031] In actual applications, in programming languages such as C#, Python, and JavaScript, string objects are immutable after they are created. In C#, when any modification operation is performed on a string (such as concatenation, replacement, etc.), a new string object is created, while the original string object remains unchanged. This immutability helps achieve thread safety and allows strings to be shared in the constant pool, thereby saving memory.
[0032] Furthermore, considering that the memory size of the preset memory block is limited and there are concurrent string operation requests, in order to meet the concurrent string operation request requirements, string memory corresponding to the character memory space information needs to be allocated for each string operation request to avoid wasting storage resources due to allocating too much memory. The specific implementation is as follows: Determine character memory space information; and determine the string memory corresponding to the character memory space information in the preset memory block in response to the string operation request.
[0033] Specifically, the character memory space information is a preset memory size that can be allocated from a preset memory block at one time.
[0034] Based on this, character memory space information for limiting the single allocated memory size is determined. In response to the string operation request, a string memory corresponding to the character memory space information is determined in a preset memory block, and the string memory is allocated to the initial string for string operation.
[0035] For example, a string operation request may be submitted for an initial string in a programming language such as C#, Python, or JavaScript. By parsing the string operation request, the initial string "abc" that needs to be operated can be determined. When receiving the string operation request, it can be determined that the character memory space information is 16 characters, and then a string memory of 16 characters is determined in a preset memory block in response to the string operation request.
[0036] In summary, in response to a string operation request, a string memory corresponding to the character memory space information is determined in a preset memory block, thereby achieving reasonable memory allocation and avoiding waste of storage resources.
[0037] Furthermore, when determining the string memory, the string scope corresponding to the string operation request may be referred to, which is specifically implemented as follows: In response to the string operation request, a string scope corresponding to the string operation request is determined; when the string scope is associated with a target function, the preset memory block is determined in a stack, and the string memory is determined in the preset memory block; when the string scope is not associated with a target function and the string operation task is completed, the preset memory block is determined in a memory pool, and the string memory is determined in the preset memory block.
[0038] Specifically, the string scope refers to the location where the initial string corresponding to the string operation request is effectively accessed. For example, when a string variable is declared inside a method, its scope is inside the method. If the string is declared as a member variable (field) of a class, its scope will extend to the entire class. The target function is the executable function that contains the initial string.
[0039] Based on this, the string scope corresponding to the string operation request is determined in response to the string operation request. In the case where the string scope is associated with the target function, the string scope representing the initial string corresponding to the string operation is only in the target function. At this time, a preset memory block can be determined in the stack, and the string memory is determined in the preset memory block. In the case where the string scope is not associated with the target function, the string scope representing the initial string corresponding to the string operation is not in the target function, a preset memory block is determined in the memory pool, and the string memory is determined in the preset memory block.
[0040] Continuing with the above example, when allocating string memory for string operations, you can directly allocate memory on the stack or request memory allocation from the memory pool. Determine the string scope corresponding to the string operation request. When the string scope is associated with the target function, the string memory is directly allocated from the stack. When the target function returns, all the occupied stack memory will be reclaimed. When the string scope is not associated with the target function, the string memory can be directly allocated from the memory pool.
[0041] In summary, when the string scope is associated with the target function, the string memory is determined in the stack, and when the string scope is not associated with the target function, the string memory is determined in the memory pool, thereby realizing diversified allocation of string memory.
[0042] Step 104: Parse the string operation request to obtain an initial string and string operation information.
[0043] Specifically, after determining the string memory in the preset memory block in response to the string operation request and creating a string object in the string memory, the string operation request can be parsed to obtain the initial string and string operation information, wherein the initial string is the string that needs to be modified corresponding to the string operation request. The initial string can be an expression of characters. The string operation information is the operation description information related to the string operation, for example, the string operation information can be: converting the initial string ABC into lowercase letters abc, and merging the string cdef after the string abc.
[0044] Based on this, after determining the string memory in the preset memory block in response to the string operation request and creating a string object in the string memory, the string operation request is parsed to obtain the initial string to be operated on and the string operation information representing the operation description of the initial string, so that the initial string can be subsequently operated accordingly based on the string operation information.
[0045] Furthermore, considering that the string memory is used to store the initial string corresponding to the string operation request and to operate the initial string, it is necessary to create a string object capable of storing the string data corresponding to the string operation request, which is specifically implemented as follows: Determine string layout information, and divide the string memory into initial string data memory and string dynamic memory according to the string layout information; store original string information corresponding to the string operation request into the initial string data memory to obtain string data memory; create the string object based on the string data memory and the string dynamic memory.
[0046] Specifically, the string layout information represents the data storage format of the string data corresponding to the string operation request in the string memory, and is used to guide the memory division of the string memory and the storage of the string data. The initial string data memory is used to store the original string information of the string data corresponding to the string operation request, and the original string information includes at least one of the following: string object header data, string length, character text data, and terminator. The string dynamic memory is used to store and update the string data corresponding to the string operation request, and the string data is the initial string. The string dynamic memory is used to store and update the initial string. The update of the initial string includes but is not limited to the modification, replacement, merging, and uppercase and lowercase letter conversion of the initial string.
[0047] Based on this, the string layout information is determined, and the string memory is divided into an initial string data memory and a string dynamic memory for storing and updating the initial string according to the string layout information. The original string information corresponding to the string operation request is determined, and the original string information includes the string object header data, string length, character text data and terminator. The string object header data, string length, character text data and terminator are stored in the initial string data memory to obtain the string data memory. The arrangement format of the string object header data, string length, character text data and terminator is determined, and a string object is created based on the string data memory and the string dynamic memory according to the arrangement format.
[0048] Following the above example, after receiving a string operation request, determine the preset string layout information, and divide the string memory into two parts: initial string data memory and string dynamic memory according to the string layout information. Determine the string object header data, string length, character text data, and terminator corresponding to the string operation request, and store the string object header data, string length, character text data, and terminator in the string dynamic memory. The string dynamic memory is used to store and update the initial string abc. Create a string object based on the string data memory and string dynamic memory.
[0049] In summary, after receiving a string operation request, a string object is created based on the string data memory and the string dynamic memory. It is used to record the original string information, record and update the initial string, ensure the consistency of the string data, and realize the accurate update of the initial string.
[0050] Furthermore, the string memory is allocated based on the character memory space information. The string memory has a fixed size. When the initial string is operated, the size of the string memory cannot meet the operation requirements of the initial string, and the string memory needs to be reallocated. The specific implementation is as follows: In a case where it is determined based on the initial string and the string operation information that the string operation information does not satisfy the string operation memory condition, memory requirement information is generated, and a target string memory is determined in the preset memory block based on the memory requirement information; the target string memory is used as the string memory, and the step of creating a string object in the string memory is executed.
[0051] Specifically, the string operation memory condition is a memory usage condition for operating the initial string according to the string operation information, and the string operation memory condition represents the memory requirement information for operating the initial string. The memory requirement information refers to the memory size required for operating the initial string. The target string memory can meet the memory requirement of the initial string operation.
[0052] Based on this, when it is determined based on the initial string and the string operation information that the string operation information does not meet the string operation memory condition, it means that the string memory is insufficient and cannot support the string operation of the initial string, that is, when the initial string is operated according to the string operation information, the size of the initial string after the operation will exceed the range of the string memory. At this time, the memory requirement information is generated according to the memory size required for the operation of the initial string, and the target string memory is determined in the preset memory block based on the memory requirement information. Using the target string memory as the string memory, you can continue to create string objects in the string memory to support the operation of the initial string.
[0053] Using the above example, when the initial string is determined to be abc, the string operation information is to merge the string cdef after the initial string. According to the string operation information, the string memory size required after the string merger can be calculated to be 7. In the case where the first allocated string memory is 6, it is determined that the string memory is insufficient, that is, the string operation information does not meet the string operation memory condition. At this time, the string memory of 12 can be reallocated to facilitate subsequent string operations.
[0054] In summary, when it is determined based on the initial string and the string operation information that the string operation information does not meet the string operation memory condition, the target string memory is determined in the preset memory block based on the memory requirement information. The target string memory that meets the initial string operation requirement is allocated to ensure that the initial string operation is performed smoothly.
[0055] Step 106: Store the initial string into the string object, and update the initial string to a target string according to the string operation information.
[0056] Specifically, after parsing the string operation request and obtaining the initial string and string operation information, the initial string can be stored in the string object, and the initial string can be updated to the target string according to the string operation information, wherein the target string is the string obtained by performing the corresponding operation on the initial string based on the string operation information. The update of the initial string can be string merging, string insertion, string replacement or string interception.
[0057] Based on this, after parsing the string operation request and obtaining the initial string and string operation information, the initial string is stored in the string object, and the initial string is updated in the string object according to the string operation information to update the initial string to the target string.
[0058] In actual applications, after the initial string is updated based on the string operation information, a string rest may be added to the end of the updated initial string to generate a target string. The string rest indicates that the target string ends at the string rest, and the string rest is not recorded in the string length.
[0059] Furthermore, the operation on the initial string may be a string merging operation, which is specifically implemented as follows: The character string merging information is determined based on the character string operation information, and the merged character string and the merged character string contained in the initial character string are determined; and the merged character string and the merged character string are merged according to the character string merging information to obtain the target character string.
[0060] Specifically, the string merging information refers to the merging order of the strings; the merged string may be a string in the initial string waiting to be merged, and the merging string refers to a string to be merged into the merged string.
[0061] Based on this, the string merging information of the initial string is determined based on the string operation information. The merged string and the merged string contained in the initial string are determined. The merged string and the merged string are merged according to the string merging information to obtain a merged target string.
[0062] Using the above example, after determining that the string operation information is: merge string cdef after string abc, the string merge information can be determined as string abc first and string cdef later for merging. The initial string contains the merged string abc and the merged string cdef. According to the string merge information, the merged string abc and the merged string cdef are merged to obtain the target string abccdef.
[0063] In summary, the merged string and the merging string are merged according to the string merging information to obtain a merged target string, thereby ensuring the accuracy of the string merging.
[0064] Furthermore, the operation on the initial string can be a string modification operation, which is specifically implemented as follows: The character string modification information is determined based on the character string operation information, and the character string to be modified contained in the initial character string is determined; the character string to be modified is modified according to the character string modification information to obtain the target character string.
[0065] Specifically, the string modification information refers to the characters that need to be modified in the initial string and the modified characters. The string to be modified refers to the string that needs to be modified in the initial string. The string modification operation can be a string insertion operation, a string replacement operation or a string interception operation.
[0066] Based on this, the string modification information indicating the string modification method is determined based on the string operation information, the string to be modified contained in the initial string is determined, and the string to be modified is modified according to the string modification information to obtain a target string after the modification is completed.
[0067] Using the above example, after determining that the string operation information is: convert the initial string ABC to lowercase letters abc, it can be determined that the string ABC needs to be changed to lowercase letters abc. The initial string contains the string ABC to be modified. Modify the string ABC according to the string modification information to obtain the target string abc.
[0068] In summary, the string to be modified is modified according to the string modification information to obtain a modified target string, thereby ensuring the accuracy of the string modification.
[0069] Furthermore, considering that the allocated string memory is used for string operations on the initial string, after the string operation is completed, the string memory needs to be released. The specific implementation is as follows: Execute the string operation task corresponding to the string operation request based on the target string; when the string scope is associated with a target function and the target function is executed, release the string memory associated with the stack; when the string scope is not associated with a target function and the string operation task is executed, release the string memory associated with the memory pool.
[0070] Specifically, the string operation task may be a function execution or call task of the function where the target string is located, and the string operation task may also be a read and write task of the target string.
[0071] Based on this, the string operation task corresponding to the string operation request is executed based on the target string. During the execution of the string operation task, if the string scope is associated with the target function and the target function is executed, it means that the target string has been used up and the target function obtains a return value, at which time the string memory of the associated stack is released. If the string scope is not associated with the target function and the string operation task is executed, it means that the target string has been used up and the string memory of the associated memory pool is released.
[0072] In summary, when the target string is used up, the string memory is released for subsequent string operations to achieve string memory reuse.
[0073] The string update method provided in the present application determines the string memory in a preset memory block in response to a string operation request, and creates a string object in the string memory. Parse the string operation request to obtain the initial string and string operation information. Store the initial string in the string object, and update the initial string to the target string according to the string operation information. When there is a string update requirement, use the preset memory block to update the string, and dynamically update the string in the preset memory block, avoiding frequent memory allocation and garbage collection operations, and improving business performance.
[0074] The following combination Figure 2 Taking the application of the string update method provided by this application to the C# string update as an example, the string update method is further described. Figure 2 A processing flow chart of a string update method applied to C# string update provided in an embodiment of the present application is shown, which specifically includes the following steps.
[0075] Step 202: Apply for allocation of an initial memory block, and fill data in the initial memory block according to the memory layout of the string object to create a string object.
[0076] In C# programming, a string is a series of zero or more characters. It is usually used to represent text data and is one of the important data types. Processing strings is a basic requirement for many programs. However, traditional C# string objects are immutable after creation. Once a string is created, its content cannot be changed. This immutability brings certain advantages, such as thread safety and reducing errors, but it also causes some performance issues.
[0077] When you need to modify a string by concatenating, replacing, converting uppercase and lowercase characters, a new string instance is created instead of modifying the original string. This not only causes additional memory overhead, but also increases the frequency of garbage collection and generates a large amount of memory fragmentation.
[0078] In the process of game development, string operations are particularly frequent. For example, application scenarios such as logging, loading resource files, and processing user input all require a large number of string operations. In these cases, due to the immutability of strings, the system will constantly allocate and recycle memory, resulting in inefficient memory usage, which ultimately causes game runtime lags. This is unacceptable for games with high real-time requirements. In order to solve the performance problem of C# strings, a string update method applied to C# string updates is proposed.
[0079] First, apply for allocation of the initial memory block, and fill the data in the initial memory block according to the memory layout of the string object to create a string object.
[0080] The memory layout of a string object is as follows Figure 3 As shown in the figure, it specifically includes string object header data, string length, string text data, terminator, and pre-allocated string memory block. The pre-allocated string memory block is a string memory block used to dynamically adjust the string content, and the default capacity is 16 characters.
[0081] In actual applications, when allocating string memory, if the string scope is only in a certain function, you can directly allocate memory on the stack. When the function returns, all the stack memory occupied is reclaimed. You can also request memory allocation from the memory pool and return the memory when the string is finished.
[0082] Step 204: Parse the string operation request to obtain the initial string and string operation information.
[0083] The string operation request is parsed to determine the strings "abc" and "def" to be operated on, as well as string operation information: merging the strings "abc" and "def".
[0084] Step 206: Calculate the string length based on the initial string and the string operation information.
[0085] Calculate the length of the merged strings. The number of characters after the two strings are merged plus the terminator is 7, which does not exceed the memory range of 16, so there is no need to reallocate the string memory.
[0086] Step 208: Determine whether the string length meets the string operation memory condition; if not, execute step 210; if so, execute step 212.
[0087] After the two strings are combined, the number of characters including the terminator is 7, which does not exceed the memory range and does not need to be reallocated. Step 212 can be directly executed.
[0088] Step 210: release the initial memory block, generate memory requirement information according to the string length, apply for allocation of a target memory block based on the memory requirement information, use the target memory block as the initial memory block, and execute step 202.
[0089] If the number of characters after the two strings are combined plus the terminator is 17, it exceeds the memory range of 16 and the string memory needs to be reallocated.
[0090] Step 212: determine a string memory block in the initial memory block, copy the initial string to the string memory block, and update the initial string to the target string according to the string operation information.
[0091] Copy "abc" to the string memory block, and append "def" to the string memory block after "abc". The two strings are combined into "abcdef" in the string memory block, and a rest character '\0' is added to the end. The value of the member variable recording the string length in the string object is changed to 6 (the string length does not need to count the terminator).
[0092] Step 214: Execute the string operation task corresponding to the string operation request based on the target string, and release the string memory when the string operation task is completed.
[0093] After the string memory is used up, if it is memory allocated by the memory pool, the memory is returned. It should be noted that the completion of the string operation task indicates the end of the life cycle of the target string.
[0094] In practical applications, such as Figure 4 As shown in the figure, a dynamic string object is pre-allocated for the value operation of the string. The string object can be used to perform multiple value modification operations until the string modification is completed and the string can be read. The value modification operation of the string is achieved by dynamically modifying the underlying memory of the string. Since the underlying logic of the C# string determines the string content in the memory by the string length and terminator, this feature can be used to pre-allocate a large enough memory block, and then dynamically adjust the string content within its range, thereby avoiding frequent memory allocation and garbage collection operations, and the content can be directly used as a normal string after modification.
[0095] Corresponding to the above method embodiment, the present application also provides a string updating device embodiment, Figure 5 FIG. 1 shows a schematic diagram of a structure of a string updating device provided by an embodiment of the present application. Figure 5 As shown, the device comprises: A determination module 502 is configured to determine a string memory in a preset memory block in response to a string operation request, and create a string object in the string memory; The parsing module 504 is configured to parse the string operation request to obtain an initial string and string operation information; The updating module 506 is configured to store the initial string into the string object, and update the initial string into a target string according to the string operation information.
[0096] In an optional embodiment, the parsing module 504 is further configured to: Determine string layout information, and divide the string memory into initial string data memory and string dynamic memory according to the string layout information; The original string information corresponding to the string operation request is stored in the initial string data memory to obtain the string data memory; The string object is created based on the string data memory and the string dynamic memory.
[0097] In an optional embodiment, the determining module 502 is further configured to: In response to the string operation request, determining a string scope corresponding to the string operation request; In the case where the string scope is associated with the target function, determining the preset memory block in the stack, and determining the string memory in the preset memory block; When the string scope is not associated with the target function and the string operation task is completed, the preset memory block is determined in the memory pool, and the string memory is determined in the preset memory block.
[0098] In an optional embodiment, the parsing module 504 is further configured to: In a case where it is determined based on the initial string and the string operation information that the string operation information does not satisfy a string operation memory condition, generating memory requirement information, and determining a target string memory in the preset memory block based on the memory requirement information; The target string memory is used as the string memory, and the step of creating a string object in the string memory is performed.
[0099] In an optional embodiment, the updating module 506 is further configured to: Execute the string operation task corresponding to the string operation request based on the target string; When the string scope is associated with a target function and the target function is executed, releasing the string memory associated with the stack; When the string scope is not associated with the target function and the string operation task is completed, the string memory associated with the memory pool is released.
[0100] In an optional embodiment, the updating module 506 is further configured to: Determining the string merging information based on the string operation information, and determining the merged string and the merged string included in the initial string; The merged character string and the merged character string are merged according to the character string merging information to obtain the target character string.
[0101] In an optional embodiment, the updating module 506 is further configured to: Determining the string modification information based on the string operation information, and determining the string to be modified contained in the initial string; The character string to be modified is modified according to the character string modification information to obtain the target character string.
[0102] The string updating device provided by the present application determines the string memory in the preset memory block in response to the string operation request, and creates a string object in the string memory. The string operation request is parsed to obtain the initial string and the string operation information. The initial string is stored in the string object, and the initial string is updated to the target string according to the string operation information. When there is a need to update the string, the preset memory block is used to update the string, and the string is dynamically updated in the preset memory block, which avoids frequent memory allocation and garbage collection operations and improves business performance.
[0103] The above is a schematic scheme of a string update device of this embodiment. It should be noted that the technical solution of the string update device and the technical solution of the above-mentioned string update method belong to the same concept. For details not described in detail in the technical solution of the string update device, please refer to the description of the technical solution of the above-mentioned string update method. In addition, the various components in the device embodiment should be understood as functional modules that must be established to implement each step of the program flow or each step of the method, and each functional module is not an actual functional division or separation. The device claim defined by such a group of functional modules should be understood as a functional module architecture that mainly implements the solution through a computer program recorded in the specification, and should not be understood as a physical device that mainly implements the solution through hardware.
[0104] Figure 6 The block diagram of a computing device 600 according to an embodiment of the present application is shown. The components of the computing device 600 include but are not limited to a memory 610 and a processor 620. The processor 620 is connected to the memory 610 via a bus 630, and the database 650 is used to store data.
[0105] The computing device 600 also includes an access device 640 that enables the computing device 600 to communicate via one or more networks 660. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 640 may include one or more of any type of network interface (e.g., a network interface card (NIC)) that is wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and the like.
[0106] In one embodiment of the present application, the above components of the computing device 600 and Figure 6 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 6 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of the present application. Those skilled in the art may add or replace other components as needed.
[0107] The computing device 600 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or PC. The computing device 600 may also be a mobile or stationary server.
[0108] The processor 620 is used to execute computer executable instructions of the string updating method.
[0109] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical scheme of the above string update method belong to the same concept, and the details not described in detail in the technical scheme of the computing device can be referred to the description of the technical scheme of the above string update method.
[0110] An embodiment of the present application further provides a computer-readable storage medium storing computer instructions, which are used in a string updating method when executed by a processor.
[0111] The above is a schematic scheme of a computer-readable storage medium of this embodiment. It should be noted that the technical scheme of the storage medium and the technical scheme of the above-mentioned string update method belong to the same concept, and the details not described in detail in the technical scheme of the storage medium can be referred to the description of the technical scheme of the above-mentioned string update method.
[0112] An embodiment of the present application further provides a chip storing a computer program, which implements the steps of the string updating method when executed by the chip.
[0113] An embodiment of the present specification further provides a computer program product, including a computer program or instructions, which implements the steps of the above-mentioned character string updating method when executed by a processor.
[0114] The above is a schematic solution of a computer program product of this embodiment. It should be noted that the technical solution of the computer program product and the technical solution of the above-mentioned string update method belong to the same concept, and the details not described in detail in the technical solution of the computer program product can be referred to the description of the technical solution of the above-mentioned string update method.
[0115] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0116] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0117] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described action sequence, because according to the present application, 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 and modules involved are not necessarily required by the present application.
[0118] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0119] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the present application. The present application selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present application, so that those skilled in the art can understand and use the present application well. The present application is only limited by the claims and their full scope and equivalents.
Claims
1. A method for updating a character string, characterized in that: include: In response to a string operation request, determining a string memory in a preset memory block, and creating a string object in the string memory; Parsing the string operation request to obtain an initial string and string operation information; The initial string is stored in the string object, and the initial string is updated to a target string according to the string operation information.
2. The string updating method according to claim 1, characterized in that: The step of creating a string object in the string memory includes: Determine string layout information, and divide the string memory into initial string data memory and string dynamic memory according to the string layout information; The original string information corresponding to the string operation request is stored in the initial string data memory to obtain the string data memory; The string object is created based on the string data memory and the string dynamic memory.
3. The string updating method according to claim 1, characterized in that: The step of determining a string memory in a preset memory block in response to a string operation request comprises: In response to the string operation request, determining a string scope corresponding to the string operation request; In the case where the string scope is associated with the target function, determining the preset memory block in the stack, and determining the string memory in the preset memory block; When the string scope is not associated with the target function and the string operation task is completed, the preset memory block is determined in the memory pool, and the string memory is determined in the preset memory block.
4. The string updating method according to claim 1, characterized in that: After parsing the string operation request and obtaining the initial string and string operation information, the method further includes: In a case where it is determined based on the initial string and the string operation information that the string operation information does not satisfy a string operation memory condition, generating memory requirement information, and determining a target string memory in the preset memory block based on the memory requirement information; The target string memory is used as the string memory, and the step of creating a string object in the string memory is performed.
5. The character string updating method according to claim 3, characterized in that: After the initial string is updated to the target string according to the string operation information, the method further includes: Execute the string operation task corresponding to the string operation request based on the target string; When the string scope is associated with a target function and the target function is executed, releasing the string memory associated with the stack; When the string scope is not associated with the target function and the string operation task is completed, the string memory associated with the memory pool is released.
6. The character string updating method according to claim 1, characterized in that: The updating the initial character string to the target character string according to the character string operation information includes: Determining the string merging information based on the string operation information, and determining the merged string and the merged string included in the initial string; The merged character string and the merged character string are merged according to the character string merging information to obtain the target character string.
7. The character string updating method according to claim 1, characterized in that: The updating the initial character string to the target character string according to the character string operation information includes: Determining the string modification information based on the string operation information, and determining the string to be modified contained in the initial string; The character string to be modified is modified according to the character string modification information to obtain the target character string.
8. A string updating device, characterized in that: include: a determination module, configured to determine a string memory in a preset memory block in response to a string operation request, and create a string object in the string memory; A parsing module, configured to parse the string operation request to obtain an initial string and string operation information; The updating module is configured to store the initial string into the string object, and update the initial string into a target string according to the string operation information.
9. A computing device, characterized in that include: Memory and processor; The memory is used to store computer executable instructions, and the processor is used to execute the computer executable instructions to implement the steps of the string updating method according to any one of claims 1 to 7.
10. A computer-readable storage medium storing computer instructions, characterized in that: When the instruction is executed by the processor, the steps of the string updating method described in any one of claims 1 to 7 are implemented.
11. A computer program product comprising a computer program or instructions, characterized in that When the computer program or instruction is executed by a processor, the steps of the string updating method according to any one of claims 1 to 7 are implemented.
