Serialization method, deserialization method and related device
By directly storing and accessing object offset values in serialized data using address collections and mapping relationship tables in the serialization method, the problem of program crashes and slow access caused by recursive algorithms is solved, and efficient data access is achieved.
Patent Information
- Application Number
- CN202111605392.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-25
- Publication Date
- 2025-05-16
- Estimated Expiration
- 2041-12-25
AI Technical Summary
When using recursive algorithms, existing serialization methods are prone to crashes due to the program stack occupying too much space and the access speed is slow.
By obtaining the address set and mapping relationship tables used to store the serialized objects and the objects to be serialized, the absolute offset values of the objects are stored and accessed in the serialized data, avoiding the use of recursive algorithms.
It effectively avoids program crash problems caused by the depth of object reference, and improves the efficiency of data access, so that serialized data can be accessed efficiently without deserialization.
Smart Images

Figure CN114519051B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of computer data structures, and in particular to a serialization method, a deserialization method, and related devices. Background Art
[0002] Structured data refers to data stored in a unified format or data structure. Each data field has a clear meaning, and computers can access the content of structured data very quickly and efficiently. Serialization refers to the process of converting an object into a byte sequence, which is called object serialization; deserialization refers to the process of restoring a byte sequence to an object, which is called object deserialization. Serialized data is suitable for computer storage and network transmission, while deserialized objects are suitable for storage in computer memory for algorithm processing and access. In the field of deep learning, the trained model data is usually stored in a file through serialization, and then the file is read on another device, deserialized, and the model data structure is obtained for subsequent processing (such as forward reasoning).
[0003] At present, serialized data generally needs to be deserialized to obtain structured data before it can be efficiently accessed by the program. For example, Google's protobuf protocol provides serialization and deserialization functions and is widely used in model structures in the field of deep learning. Flatbuffer is another serialization framework provided by Google. One of its advantages is that serialized data can be accessed more efficiently without deserialization. However, when using recursive algorithms, it is easy to cause the program to crash due to the program stack taking up too much space, which is slightly slower than native structured object access. Therefore, a new serialization method is urgently needed to solve the above problems. Summary of the invention
[0004] The main technical problem solved by the present application is to provide a serialization method, a deserialization method and related devices, which can avoid being affected by the reference depth between objects when using a recursive algorithm.
[0005] To solve the above technical problems, a technical solution adopted by the present application is: to provide a serialization method, including: obtaining a first set, a second set and a first mapping relationship table; wherein the first set is used to store the first address of the serialized object, the second set is used to store the second address of the object to be serialized, and the first mapping relationship table is used to store the mapping relationship between the first address and the absolute offset value of the corresponding serialized object in the serialized data; for each second address in the second set, the object to be serialized corresponding to the second address is written into the serialized data formed by the serialized object, and the absolute offset value of the object to be serialized in the serialized data is written into the first mapping relationship table; in response to the existence of a corresponding reference object of the serialized object, a relative offset value between the absolute offset value of the serialized object in the serialized data and the absolute offset value of the corresponding reference object is obtained, and the relative offset value is written into the serialized data.
[0006] Among them, before the step of obtaining the relative offset value between the absolute offset value of the serialized object and the absolute offset value of the corresponding reference object in response to the existence of a corresponding reference object of the serialized object, and writing the relative offset value into the serialized data, the method includes: in response to the second set being empty, traversing the first addresses of all the serialized objects in the first set; for each of the serialized objects, obtaining the absolute offset value of the serialized object in the serialized data by using the first address and the first mapping relationship table, and traversing all the reference objects corresponding to the serialized object.
[0007] Among them, the step of obtaining the relative offset value between the absolute offset value of the serialized object and the corresponding absolute offset value of the reference object includes: obtaining the absolute offset value of the reference object in the serialized data from the first mapping relationship table; subtracting the absolute offset value of the reference object from the absolute offset value of the serialized object to obtain the relative offset value between the serialized object and the reference object.
[0008] Among them, after the step of writing the object to be serialized corresponding to the second address into the serialized data formed by the serialized object for each second address in the second set, and writing the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table, it also includes: in response to the absence of a corresponding reference object for the serialized object, setting the relative offset value at the corresponding position in the serialized data to a preset value.
[0009] Among them, before the step of writing the object to be serialized corresponding to the second address into the serialized data formed by the serialized object for each second address in the second set, and writing the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table, it includes: obtaining the memory values of all the objects to be serialized, and obtaining the second address of each object to be serialized in the serialized data according to the size order of the memory values.
[0010] To solve the above technical problems, another technical solution adopted by the present application is: providing a deserialization method, including: obtaining a third set, a fourth set and a second mapping relationship table; wherein the third set is used to store the absolute offset value of the deserialized object in the serialized data, the fourth set is used to store the absolute offset value of the object to be deserialized in the serialized data, and the second mapping relationship table is used to store the mapping relationship between the absolute offset value and the address of the corresponding deserialized object; for each of the absolute offset values in the fourth set, the object to be deserialized corresponding to the absolute offset value is written into the allocation space formed by the deserialized object, and the address of the object to be deserialized in the allocation space is written into the second mapping relationship table; in response to the relative offset value between the deserialized object and the corresponding referenced object being a non-preset value, the third address of the referenced object is obtained, and the third address is written into the pointer of the corresponding deserialized object.
[0011] Among them, before the step of obtaining the third address of the reference object in response to the relative offset value between the deserialized object and the corresponding reference object being a non-preset value, and writing the third address into the pointer of the corresponding deserialized object, it includes: in response to the fourth set being empty, traversing the absolute offset values of all the deserialized objects in the third set in the serialized data; for each of the deserialized objects, using the absolute offset value and the second mapping relationship table to obtain the fourth address of the deserialized object in the allocated space, and traversing all the reference objects corresponding to the deserialized objects.
[0012] Among them, the step of obtaining the third address of the referenced object includes: for each of the referenced objects, obtaining from the serialized data the relative offset value between the deserialized object and the referenced object, and obtaining from the second mapping table according to the fourth address the absolute offset value of the deserialized object corresponding to the referenced object in the serialized data; using the sum of the relative offset value and the absolute offset value of the deserialized object in the serialized data as the absolute offset value of the referenced object in the serialized data; and obtaining the third address corresponding to the referenced object using the absolute offset value of the referenced object in the serialized data and the second mapping relationship table.
[0013] Among them, after the step of writing the object to be deserialized corresponding to the absolute offset value into the allocated space formed by the deserialized object for each absolute offset value in the fourth set, and writing the address of the object to be deserialized in the allocated space into the second mapping relationship table, it also includes: in response to the relative offset value being a preset value, setting the corresponding pointer of the deserialized object in the allocated space to null.
[0014] To solve the above technical problems, another technical solution adopted in the present application is: to provide an electronic device, comprising a memory and a processor coupled to each other, wherein the memory stores program instructions, and the processor is used to execute the program instructions to implement the serialization method or deserialization method mentioned in any of the above embodiments.
[0015] To solve the above technical problems, another technical solution adopted in the present application is: providing a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program is used to implement the serialization method or deserialization method mentioned in any of the above embodiments.
[0016] Different from the prior art, the beneficial effect of the present application is that the serialization method provided by the present application includes: obtaining a first set for storing the first address of the serialized object, a second set for storing the second address of the object to be serialized, and a first mapping relationship table for storing the mapping relationship between the first address and the absolute offset value of the corresponding serialized object in the serialized data; for each second address in the second set, writing the object to be serialized corresponding to the second address into the serialized data formed by the serialized object, and writing the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table; in response to the existence of a corresponding reference object of the serialized object, obtaining the relative offset value between the absolute offset value of the serialized object in the serialized data and the absolute offset value of the corresponding reference object, and writing the relative offset value into the serialized data. Through this design method, it is possible to avoid being affected by the reference depth between objects when using a recursive algorithm, and avoid the problem of program crash caused by the program stack occupying too much space. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. Among them:
[0018] Figure 1 It is a schematic diagram of the storage of data structures in memory;
[0019] Figure 2 is with Figure 1 The corresponding result diagram after serialization;
[0020] Figure 3 It is the overall flow chart of the serialization and deserialization algorithm provided by this application;
[0021] Figure 4 It is a flowchart of an implementation method of the serialization method of the present application;
[0022] Figure 5 yes Figure 4 A schematic diagram of a flow chart of an implementation method before step S3;
[0023] Figure 6 yes Figure 4 A schematic diagram of a flow chart of an implementation method corresponding to step S4;
[0024] Figure 7 It is a flowchart of an implementation method of the deserialization method of the present application;
[0025] Figure 8 yes Figure 7 A schematic diagram of a flow chart of an implementation method before step S32;
[0026] Fig. 9 yes Figure 7 A schematic diagram of a flow chart of an implementation method corresponding to step S34;
[0027] Fig.10 It is a structural diagram of an implementation method of a serialization system and a deserialization system of the present application;
[0028] Fig.11 It is a schematic diagram of a framework of an implementation method of an electronic device of the present application;
[0029] Fig.12 It is a schematic diagram of a framework of an implementation scheme of a computer-readable storage medium of the present application. DETAILED DESCRIPTION
[0030] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0031] Please also read Figure 1 and Figure 2 , Figure 1 It is a schematic diagram of the storage of data structures in memory. Figure 2 is with Figure 1 The corresponding result diagram after serialization. Figure 1 As shown, the members of one of the objects in the data structure (for example, object A, object B, object C, object D, object E) are divided into two types, namely basic data types and references to other objects. For example, the reference object of object A is object B. Basic data types are data that occupy a fixed size of memory, and the corresponding data space is directly allocated in the object to store its value, such as integers and floating-point numbers of various widths. For references to other data, pointers are generally used in memory to represent them, and the referenced object can be accessed through the pointer. If the referenced object does not exist, the value of the pointer is empty (NULL). Because the size of the space occupied by a pointer is also fixed, the size of the storage space of the entire object is also fixed. However, it is not allowed for an object (parent object) to directly contain another object (child object). In this case, the child object should be made an independent object, and a reference to the child object should be added to the parent object. As shown Figure 1 and Figure 2As shown, the storage format of serialized data is to store all members of an object after serialization (including basic data types and references to other objects) continuously. When the starting address of the object is obtained, the access program can access a specified member through a fixed offset. For the referenced object, the offset value is stored in the serialized data, where the offset value is calculated as follows: offset value = starting address of the referenced object - starting address of the referenced object. Through this offset value, the address of the referenced object can be obtained very conveniently. Specifically, when the offset value is zero, it is a reference of an object to itself. Generally, the starting position of the object is required to be 4-byte aligned. If the offset value is not a multiple of 4, then the offset value is not a legal offset value. We can use any such value to indicate that the referenced object does not exist, and this application is not limited here. Preferably, in this embodiment, the offset value can be set to 1 or -1 to indicate that the referenced object does not exist. Specifically, in this embodiment, when the offset value is a positive number, it indicates a reference to the subsequent object in the serialized data; when the offset value is a negative number, it indicates a reference to the previous object in the serialized data. In current technology, only FlatBuffer uses offsets to reference other objects, and the offset value is a positive number, which can only be referenced in one direction, and does not support reverse references or self-references. This design breaks through the structural limitations of FlatBuffer serialized data, no longer requires data to have a tree structure, and does not need to define the top-level object data type, so that cross-references, circular references, and self-references between objects are allowed, and basically any structured data can be serialized. Because in the serialized data, the reference to the subsequent objects, the previous objects, and the object itself in the serialized data is achieved through the relative offset values of positive numbers, negative numbers, and zero, and the non-existence of the referenced object is indicated by a specific offset value (such as 1 or -1).
[0032] See also Figure 3 , Figure 3 This is the overall flow chart of the serialization and deserialization algorithm provided by this application. Figure 3 As shown, the algorithm process is divided into two parts: the first part allocates space for serialized or deserialized objects, completes the processing of basic data types, and establishes the mapping relationship between objects before and after processing; the second part combines the mapping relationship between objects before and after processing and the object reference relationship before processing to generate the object reference relationship after processing, that is, to complete the mutual conversion between pointers and relative offsets.
[0033] The following will explain the serialization method and deserialization method in detail.
[0034] The serialization process is that the input of the serialization is an object in memory, and the output is the serialized data. The serialized data contains the members of this object and all objects directly referenced and indirectly referenced by it, and the starting position of the serialized data stores the object specified by the input. For the referenced objects, the order of serialization is not guaranteed. In the serialization process, the object before serialization is represented by its address; the object after serialization is represented by the absolute offset of its storage location in the serialized data. The serialized object can represent both the object before serialization, that is, the object to be serialized, and the object after serialization. The address is used to represent the object before serialization, and the absolute offset value is used to represent the object after serialization.
[0035] In this embodiment, the structured data such as Java object data in the heap memory can be obtained first, and then the structured data such as Java object data can be stored in a disk file or transferred to other network nodes through the serialization method provided by this application. Figure 4 , Figure 4 This is a flow chart of an implementation method of the serialization method of the present application. The above serialization method includes:
[0036] S1: Obtain a first set, a second set, and a first mapping relationship table.
[0037] Specifically, the first set Set1 is used to store the first address of the serialized object, the second set Set2 is used to store the second address of the object to be serialized, and the first mapping table Map1 is used to store the mapping relationship between the first address and the absolute offset value of the corresponding serialized object in the serialized data. In this embodiment, the initial state of the first set Set1 is empty, the initial state of the second set Set2 is to contain only the object specified by the input, and the initial state of the first mapping table Map1 is empty.
[0038] S2: For each second address in the second set, write the object to be serialized corresponding to the second address into the serialized data formed by the serialized object, and write the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table.
[0039] Specifically, when an object to be serialized is taken out from the second set Set2, the object to be serialized is deleted from Set2 at the same time, so as to avoid the problem of program crash caused by the program stack occupying too much space when using a recursive algorithm.
[0040] Preferably, in this embodiment, before step S2, the process includes: obtaining the memory values of all objects to be serialized, and obtaining the second address of each object to be serialized in the serialized data according to the order of the memory values. Specifically, the memory values of all objects to be serialized are calculated, and space is allocated to each object to be serialized in the serialized data according to the order of the memory values.
[0041] Specifically, in this embodiment, for all reference objects of the object to be serialized, it is determined one by one whether the reference object exists in the first set Set1 or the second set Set2. If the reference object does not exist in either the first set Set1 or the second set Set2, the reference object is added to the second set Set2. If the reference object only exists in the first set Set1, it means that the reference object has been serialized and does not need to be added to the second set Set2. In this embodiment, all serialized objects first enter the second set Set2, and are transferred to the first set Set1 after serialization. The objects in the first set Set1 only increase and never decrease. Regardless of whether the reference object exists in the first set Set1 or the second set Set2, it means that the reference object has been referenced elsewhere and is an object that is repeatedly referenced (or referenced multiple times). At this time, there is no need to make any modifications to the first set Set1 or the second set Set2.
[0042] S3: Determine whether the serialized object has a corresponding reference object.
[0043] See also Figure 5 , Figure 5 yes Figure 4 Schematic diagram of a flow chart of an implementation method before step S3. Specifically, before step S3 includes:
[0044] S10: Determine whether the second set is empty.
[0045] Specifically, the purpose of determining whether the second set Set2 is empty is to determine whether all the objects to be serialized in the second set Set2 are included in the first set Set1.
[0046] S11: If yes, traverse the first addresses of all serialized objects in the first set.
[0047] Specifically, if the second set Set2 is empty, it means that all the objects to be serialized in the second set Set2 are entered into the first set Set1 and converted into serialized objects. At this time, all serialized objects in the first set Set1 and the first addresses of all serialized objects are traversed.
[0048] S12: For each serialized object, use the first address and the first mapping relationship table to obtain the absolute offset value of the serialized object in the serialized data, and traverse all reference objects corresponding to the serialized object.
[0049] Specifically, for each serialized object, the absolute offset value of the serialized object corresponding to the first address in the serialized data is obtained by querying from the first mapping relationship table Map1, and all reference objects corresponding to the serialized object are traversed.
[0050] S13: Otherwise, return to step S2.
[0051] Specifically, if the second set Set2 is not empty, it means that not all the objects to be serialized in the second set Set2 have entered the first set Set1 and been converted into serialized objects. At this time, the process returns to the step of writing the object to be serialized corresponding to the second address into the serialized data formed by the serialized object for each second address in the second set, and writing the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table, to ensure that all the objects to be serialized in the second set Set2 enter the first set Set1 and are converted into serialized objects.
[0052] S4: If yes, obtain the relative offset value between the absolute offset value of the serialized object in the serialized data and the absolute offset value of the corresponding referenced object, and write the relative offset value into the serialized data.
[0053] Specifically, in this embodiment, please refer to Figure 6 , Figure 6 yes Figure 4 Specifically, if the serialized object has a corresponding reference object, that is, if the pointer corresponding to the reference object is not null, the step of obtaining the relative offset value between the absolute offset value of the serialized object and the absolute offset value of the corresponding reference object in step S4 includes:
[0054] S20: Obtaining the absolute offset value of the referenced object in the serialized data from the first mapping relationship table.
[0055] Specifically, the absolute offset value of the reference object corresponding to the serialized object is obtained by querying the first mapping relationship table Map1.
[0056] S21: Subtract the absolute offset value of the referenced object from the absolute offset value of the serialized object to obtain a relative offset value between the serialized object and the referenced object.
[0057] Specifically, the relative offset value between the serialized object and the referenced object = the absolute offset value of the referenced object - the absolute offset value of the serialized object. After the relative offset values of the two are calculated, the relative offset value is written to a specific position in the serialized data, and finally the serialized data is returned.
[0058] In this way, the serialized objects can achieve efficient data access without deserialization, and the relative offset value is used to realize the reference to other objects, which can easily calculate the position of the referenced object in the serialized data.
[0059] S5: Otherwise, the relative offset value at the corresponding position in the serialized data is set to a preset value.
[0060] Specifically, in this embodiment, after step S2, the following step is further performed: in response to the absence of a corresponding reference object for the serialized object, that is, in response to the pointer corresponding to the reference object being null, the relative offset value at the corresponding position in the serialized data is set to a preset value. Specifically, if there is no absolute offset value of the reference object corresponding to the first address in the first mapping relationship table Map1, the relative offset value at the corresponding position in the serialized data is set to a preset value. In this embodiment, the preset value may be 1 or -1, etc., and this application does not limit this.
[0061] With this design, serialized objects and objects to be serialized are stored in collections in the serialization method. By adding and deleting collection elements, the influence of the reference depth between objects when using recursive algorithms can be avoided, and the problem of program crashes caused by the program stack taking up too much space can be avoided.
[0062] The deserialization process is the reverse process of serialization. The input is the serialized data (that is, the starting address of the serialized object), and the output return value is the pointer of the first object in the serialized data after deserialization. After deserialization, the reference relationship between objects is changed from offset value to pointer reference. In the deserialization process, the object before deserialization (that is, the object obtained after serialization) is represented by its absolute offset value in the serialized data, and the object after deserialization is represented by its address in memory. The deserialized object can represent both the object before deserialization (that is, the object to be deserialized) and the object after deserialization. The address is used to represent the object after deserialization, and the absolute offset value is used to represent the object before deserialization.
[0063] In this embodiment, object data in disk files, object data on network nodes, etc. are obtained, and the objects to be deserialized in the above object data are deserialized to restore the object data into a Java object model. Figure 7 , Figure 7This is a flow chart of an implementation method of the deserialization method of the present application. The above deserialization method includes:
[0064] S30: Obtain the third set, the fourth set and the second mapping relationship table.
[0065] Specifically, the third set is used to store the absolute offset value of the deserialized object in the serialized data, the fourth set is used to store the absolute offset value of the object to be deserialized in the serialized data, and the second mapping relationship table is used to store the mapping relationship between the absolute offset value and the address of the corresponding deserialized object. In this embodiment, the initial state of the third set Set3 is empty, the initial state of the fourth set Set4 is to contain an offset value of zero, and the initial state of the second mapping relationship table Map2 is empty.
[0066] S31: For each absolute offset value in the fourth set, write the object to be deserialized corresponding to the absolute offset value into the allocation space formed by the deserialized object, and write the address of the object to be deserialized in the allocation space into the second mapping relationship table.
[0067] Specifically, when an object to be deserialized is taken out from the fourth set Set4, the object to be deserialized is deleted from Set4 at the same time, so as to avoid the problem of program crash caused by the program stack occupying too much space when using a recursive algorithm.
[0068] Preferably, in this embodiment, before step S31, the process includes: allocating space in the memory space for each object to be deserialized. The basis for the allocation can be set according to actual conditions, and this application does not limit this.
[0069] Specifically, in this embodiment, for all referenced objects in the deserialized object, the relative offset value is read from the deserialized object. If the relative offset value is a preset value, such as 1 or -1, etc., indicating that the referenced object of the deserialized object is empty, no processing is performed; if the relative offset value is not a preset value, indicating that the deserialized object has a referenced object, the absolute offset value of the referenced object is obtained by calculation, and the calculation formula is absolute offset value of the referenced object = absolute offset value of the deserialized object + relative offset value. Then, it is determined whether the absolute offset value of the referenced object exists in the third set Set3 or the fourth set Set4; if not, the absolute offset value of the referenced object is added to the fourth set Set4.
[0070] S32: Determine whether the relative offset value between the deserialized object and the corresponding reference object is a preset value.
[0071] See also Figure 8 , Figure 8 yes Figure 7Schematic diagram of the flow chart of an implementation method before step S32.
[0072] Specifically, before step S32, the following steps are included:
[0073] S320: Determine whether the fourth set is empty.
[0074] Specifically, the purpose of judging whether the fourth set Set4 is empty is to judge whether all the objects to be deserialized in the fourth set Set4 are entered into the third set Set3.
[0075] S321: If yes, traverse the absolute offset values of all deserialized objects in the third set in the serialized data.
[0076] Specifically, if the fourth set Set4 is empty, it means that the objects to be deserialized in the fourth set Set4 are all entered into the third set Set3 and converted into deserialized objects. At this time, the absolute offset values of all deserialized objects in the third set Set3 in the serialized data are traversed.
[0077] S322: For each deserialized object, obtain the fourth address of the deserialized object in the allocated space using the absolute offset value and the second mapping relationship table, and traverse all reference objects corresponding to the deserialized object.
[0078] Specifically, for each deserialized object, the fourth address of the deserialized object in the allocated space corresponding to the absolute offset value is obtained from the second mapping relationship table Map2, and all reference objects corresponding to the serialized object are traversed.
[0079] S324: Otherwise, return to step S31.
[0080] Specifically, if the fourth set Set4 is not empty, it means that not all objects to be deserialized in the fourth set Set4 have entered the third set Set3, and the process returns to the step of, for each absolute offset value in the fourth set, writing the object to be deserialized corresponding to the absolute offset value into the allocated space formed by the deserialized object, and writing the address of the object to be deserialized in the allocated space into the second mapping relationship table.
[0081] S33: If so, set the corresponding pointer of the deserialized object in the allocated space to null.
[0082] Specifically, if the relative offset value is a preset value (eg, 1 or -1, etc.), the pointer corresponding to the deserialized object in the allocated space is set to null.
[0083] S34: Otherwise, obtain the third address of the referenced object, and write the third address into the pointer of the corresponding deserialized object.
[0084] Specifically, if the relative offset value is not a preset value (eg, 1 or -1, etc.), it indicates that the deserialized object has a reference object, and the third address of the reference object is obtained, and the third address is written into the pointer of the corresponding deserialized object.
[0085] With this design, deserialized objects and objects to be deserialized are stored in collections in the deserialization method. By adding and deleting collection elements, the influence of reference depth between objects when using recursive algorithms can be avoided, and the problem of program crashes caused by the program stack taking up too much space can be avoided.
[0086] Specifically, in this embodiment, please refer to Fig. 9 , Fig. 9 yes Figure 7 Specifically, the step of obtaining the third address of the referenced object in step S34 includes:
[0087] S340: For each reference object, obtain the relative offset value between the deserialized object and the reference object from the serialized data, and obtain the absolute offset value of the deserialized object corresponding to the reference object in the serialized data from the second mapping table according to the fourth address.
[0088] S341: The sum of the relative offset value and the absolute offset value of the deserialized object in the serialized data is used as the absolute offset value of the referenced object in the serialized data.
[0089] Specifically, the calculation formula is: the absolute offset value of the referenced object in the serialized data = the relative offset value + the absolute offset value of the deserialized object in the serialized data.
[0090] S342: Obtain a third address corresponding to the referenced object using the absolute offset value of the referenced object in the serialized data and the second mapping relationship table.
[0091] Specifically, after obtaining the absolute offset value of the referenced object in the serialized data in step S341, the third address of the referenced object in the allocated space corresponding to the absolute offset value of the referenced object in the serialized data is obtained from the second mapping relationship table Map1, and then the step of writing the third address into the pointer of the corresponding deserialized object is entered, and finally the address of the first deserialized object in the memory is returned.
[0092] This design approach can avoid being affected by the depth of references between objects when using recursive algorithms, and can also prevent the program from crashing due to the program stack taking up too much space.
[0093] See also Fig.10 , Fig.10 Schematic diagram of the structure of an implementation of the serialization system and deserialization system of the present application. Fig.10 As shown in a, the serialization system specifically includes:
[0094] The first acquisition module 10 is used to acquire a first set, a second set and a first mapping relationship table; wherein the first set is used to store the first address of the serialized object, the second set is used to store the second address of the object to be serialized, and the first mapping relationship table is used to store the mapping relationship between the first address and the absolute offset value of the corresponding serialized object in the serialized data.
[0095] The first writing module 12 is coupled to the first obtaining module 10, and is used to write the object to be serialized corresponding to the second address into the serialized data formed by the serialized object for each second address in the second set, and write the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table.
[0096] The offset value module 14 is coupled to the first writing module 12, and is used to obtain a relative offset value between the absolute offset value of the serialized object in the serialized data and the absolute offset value of the corresponding reference object in response to the absolute offset value of the reference object of the serialized object in the first mapping relationship table. In addition, the first writing module 12 is also used to write the relative offset value into the serialized data.
[0097] Specifically, Fig.10 As shown in b, the deserialization system specifically includes:
[0098] The second acquisition module 20 is used to obtain the third set, the fourth set and the second mapping relationship table; wherein the third set is used to store the absolute offset value of the deserialized object in the serialized data, the fourth set is used to store the absolute offset value of the object to be deserialized in the serialized data, and the second mapping relationship table is used to store the mapping relationship between the absolute offset value and the address of the corresponding deserialized object.
[0099] The second writing module 22 is coupled to the second acquisition module 20, and is used to write the object to be deserialized corresponding to the absolute offset value into the allocated space formed by the deserialized object for each absolute offset value in the fourth set, and write the address of the object to be deserialized in the allocated space into the second mapping relationship table.
[0100] The address module 24 is coupled to the second writing module 22, and is used to obtain the third address of the reference object in response to the relative offset value between the deserialized object and the corresponding reference object being a non-preset value. In addition, the second writing module 22 is also used to write the third address into the pointer of the corresponding deserialized object.
[0101] See also Fig.11, Fig.11 1 is a schematic diagram of a framework of an embodiment of an electronic device of the present application. The electronic device includes a memory 30 and a processor 32 coupled to each other. Specifically, in this embodiment, the memory 30 stores program instructions, and the processor 32 is used to execute the program instructions to implement the serialization method or deserialization method mentioned in any of the above embodiments.
[0102] Specifically, the processor 32 may also be referred to as a CPU (Central Processing Unit). The processor 32 may be an integrated circuit chip having the ability to process signals. The processor 32 may also be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. A general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. In addition, the processor 32 may be implemented by a plurality of integrated circuit chips.
[0103] See also Fig.12 , Fig.12 It is a schematic diagram of a framework of an embodiment of a computer-readable storage medium of the present application. The computer-readable storage medium 40 stores a computer program 400, which can be read by a computer, and the computer program 400 can be executed by a processor to implement the serialization method or deserialization method mentioned in any of the above embodiments. Among them, the computer program 400 can be stored in the above-mentioned computer-readable storage medium 40 in the form of a software product, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The computer-readable storage medium 40 with a storage function can be a U disk, a mobile hard disk, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), a disk or an optical disk, etc., which can store program codes, or terminal devices such as computers, servers, mobile phones, and tablets.
[0104] In summary, different from the prior art, the serialization method provided by the present application includes: obtaining a first set for storing the first address of the serialized object, a second set for storing the second address of the object to be serialized, and a first mapping relationship table for storing the mapping relationship between the first address and the absolute offset value of the corresponding serialized object in the serialized data; for each second address in the second set, writing the object to be serialized corresponding to the second address into the serialized data formed by the serialized object, and writing the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table; in response to the existence of a corresponding reference object of the serialized object, obtaining the relative offset value between the absolute offset value of the serialized object in the serialized data and the absolute offset value of the corresponding reference object, and writing the relative offset value into the serialized data. Through this design method, it is possible to avoid being affected by the reference depth between objects when using a recursive algorithm, and avoid the problem of program crash caused by the program stack occupying too much space.
[0105] The above description is only an implementation method of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly used in other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A serialization method, characterized in that: include: Obtain a first set, a second set, and a first mapping relationship table; wherein the first set is used to store a first address of a serialized object, the second set is used to store a second address of an object to be serialized, and the first mapping relationship table is used to store a mapping relationship between the first address and an absolute offset value of the corresponding serialized object in serialized data; For each second address in the second set, write the object to be serialized corresponding to the second address into the serialized data formed by the serialized object, and write the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table; In response to the existence of a corresponding reference object for the serialized object, obtaining a relative offset value between an absolute offset value of the serialized object in the serialized data and an absolute offset value of the corresponding reference object, and writing the relative offset value into the serialized data; Wherein, in response to the existence of a corresponding reference object for the serialized object, obtaining a relative offset value between an absolute offset value of the serialized object and an absolute offset value of the corresponding reference object, and before writing the relative offset value into the serialized data, the step includes: In response to the second set being empty, traversing the first addresses of all the serialized objects in the first set; For each of the serialized objects, the absolute offset value of the serialized object in the serialized data is obtained by using the first address and the first mapping relationship table, and all reference objects corresponding to the serialized object are traversed.
2. The serialization method according to claim 1, characterized in that: The step of obtaining a relative offset value between the absolute offset value of the serialized object and the corresponding absolute offset value of the referenced object comprises: Obtaining the absolute offset value of the referenced object in the serialized data from the first mapping relationship table; The absolute offset value of the reference object and the absolute offset value of the serialized object are subtracted to obtain a relative offset value between the serialized object and the reference object.
3. The serialization method according to claim 1, characterized in that: After the step of writing the object to be serialized corresponding to the second address into the serialized data formed by the serialized object for each second address in the second set, and writing the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table, the method further includes: In response to the serialized object not having a corresponding reference object, the relative offset value at the corresponding position in the serialized data is set to a preset value.
4. The serialization method according to claim 1, characterized in that: Before the step of writing, for each second address in the second set, the object to be serialized corresponding to the second address into the serialized data formed by the serialized object, and writing the absolute offset value of the object to be serialized in the serialized data into the first mapping relationship table, the method includes: The memory values of all the objects to be serialized are obtained, and the second address of each object to be serialized in the serialized data is obtained in order of the sizes of the memory values.
5. A deserialization method, characterized in that: include: Obtain a third set, a fourth set, and a second mapping relationship table; wherein the third set is used to store the absolute offset value of the deserialized object in the serialized data, the fourth set is used to store the absolute offset value of the object to be deserialized in the serialized data, and the second mapping relationship table is used to store the mapping relationship between the absolute offset value and the address of the corresponding deserialized object; For each of the absolute offset values in the fourth set, write the object to be deserialized corresponding to the absolute offset value into the allocation space formed by the deserialized object, and write the address of the object to be deserialized in the allocation space into the second mapping relationship table; In response to a relative offset value between the deserialized object and the corresponding reference object being a non-preset value, obtaining a third address of the reference object, and writing the third address into a pointer of the corresponding deserialized object; Wherein, in response to the relative offset value between the deserialized object and the corresponding reference object being a non-preset value, before the step of obtaining the third address of the reference object and writing the third address into the pointer of the corresponding deserialized object, the step includes: In response to the fourth set being empty, traversing the absolute offset values of all the deserialized objects in the third set in the serialized data; For each of the deserialized objects, the fourth address of the deserialized object in the allocated space is obtained using the absolute offset value and the second mapping relationship table, and all reference objects corresponding to the deserialized object are traversed.
6. The deserialization method according to claim 5, characterized in that: The step of obtaining the third address of the reference object comprises: For each of the referenced objects, obtain a relative offset value between the deserialized object and the referenced object from the serialized data, and obtain an absolute offset value of the deserialized object corresponding to the referenced object in the serialized data from the second mapping relationship table according to the fourth address; Taking the sum of the relative offset value and the absolute offset value of the deserialized object in the serialized data as the absolute offset value of the referenced object in the serialized data; The third address corresponding to the reference object is obtained by using the absolute offset value of the reference object in the serialized data and the second mapping relationship table.
7. The deserialization method according to claim 5, characterized in that: After the step of writing the object to be deserialized corresponding to the absolute offset value into the allocation space formed by the deserialized object for each absolute offset value in the fourth set, and writing the address of the object to be deserialized in the allocation space into the second mapping relationship table, the method further includes: In response to the relative offset value being a preset value, the corresponding pointer of the deserialized object in the allocated space is set to null.
8. An electronic device, characterized in that: It comprises a memory and a processor coupled to each other, wherein the memory stores program instructions, and the processor is used to execute the program instructions to implement the serialization method according to any one of claims 1 to 4 or the deserialization method according to any one of claims 5 to 7.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it is used to implement the serialization method described in any one of claims 1 to 4 or the deserialization method described in any one of claims 5 to 7.
Citation Information
Patent Citations
Data processing method and device
CN111506628A
Methods for In-Place Access of Serialized Data
US20150293962A1