Program running method and device, and computer readable storage medium

CN113360215BActive Publication Date: 2026-09-25TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202010146970.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-03-05
Publication Date
2026-09-25
Estimated Expiration
2040-03-05

AI Technical Summary

Technical Problem

[0004]但是,目前的优化方式均是从代码加载速度和启动速度的角度优化,并不 能让单个物理页尽可能的加载更多的当前或下一条待执行的函数,因此无法进 一步提高程序的启动速度和加载速度

Benefits of technology

[0020]本申请实施例具有以下有益效果:由于在运行待运行程序之前,确定待运 行程序对应的函数集合中的每一函数在进行函数调用时对应的频次;并根据每 一函数的所述频次,确定出用于加载所述函数集合中的函数且具有连续内存的 页内存,这样,可以使得调用频次较高的函数,能够被加载到同一页内存或近 邻的页内存中而降低程序运行过程中寄存器的指令跳转幅度,从而显著降低程序的启动内存,提高程序运行速率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113360215B_ABST
    Figure CN113360215B_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a program running method and device and a computer readable storage medium, wherein the method comprises: obtaining a function set corresponding to a to-be-run program; determining a frequency corresponding to each function in the function set when the function is called; determining a page memory for loading functions in the function set and having continuous memory according to the frequency of each function; loading the functions in the function set into the page memory in sequence; and calling the functions in the page memory in sequence according to the address of the page memory to realize running the to-be-run program. Through the present application, functions with high calling frequency can be loaded into the same page memory or adjacent page memory to reduce the instruction jump range of the register in the program running process, thereby significantly reducing the starting memory of the program and improving the program running rate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of Internet technology, and includes, but is not limited to, a program execution method, apparatus, and computer-readable storage medium. Background Technology

[0002] As product requirements increase and the stacked features become more complex, the overall size of the application grows larger. However, the increasing number of features leads to more and more user experience and performance issues, among which startup speed and loading speed are the most directly affected by the user.

[0003] Currently, optimization of startup and loading speeds is typically achieved by reducing unnecessary code, lazy loading, and utilizing multithreading. Alternatively, binary reordering can be used to compactly arrange all function code executed sequentially during startup into a sequential binary, thereby significantly reducing the instruction jump range of registers.

[0004] However, current optimization methods all focus on improving code loading and startup speed, and cannot enable a single physical page to load as many current or next functions to be executed as possible. Therefore, they cannot further improve the startup and loading speed of the program. Summary of the Invention

[0005] This application provides a program running method, apparatus, and computer-readable storage medium that can optimize program memory without modifying program code, significantly reducing program startup memory and improving program startup and loading speed.

[0006] The technical solution of this application embodiment is implemented as follows:

[0007] This application provides a program execution method, including:

[0008] Get the set of functions corresponding to the program to be run;

[0009] Determine the frequency of each function in the function set when it is called;

[0010] Based on the frequency of each function, determine the page memory used to load the functions in the function set and have contiguous memory;

[0011] The functions in the function set are loaded into the page memory in sequence;

[0012] The functions in the page memory are called sequentially according to the address of the page memory to run the program to be run.

[0013] This application provides a program running device, including:

[0014] The acquisition module is used to obtain the set of functions corresponding to the program to be run.

[0015] The first determining module is used to determine the frequency of each function in the function set when it is called.

[0016] The second determining module is used to determine, based on the frequency of each function, a page memory with contiguous memory for loading functions in the function set;

[0017] A loading module is used to load the functions in the function set into the page memory in sequence;

[0018] The calling module is used to sequentially call the functions in the page memory according to the address of the page memory, so as to run the program to be run.

[0019] This application provides a computer-readable storage medium storing executable instructions for implementing the above-described method when executed by a processor.

[0020] The embodiments of this application have the following beneficial effects: Before running the program to be run, the frequency of each function in the function set corresponding to the program to be run is determined; and based on the frequency of each function, a page memory with contiguous memory is determined for loading the functions in the function set. In this way, functions with higher call frequency can be loaded into the same page memory or adjacent page memory, thereby reducing the instruction jump amplitude of registers during program execution, thus significantly reducing the program's startup memory and improving the program's running speed. Attached Figure Description

[0021] Figure 1 This is a schematic diagram of binary code text in related technologies;

[0022] Figure 2A This is an optional architecture diagram of the program running system 10 provided in this application embodiment;

[0023] Figure 2B This is an optional structural diagram of the program running system 10 provided in this application embodiment applied to a blockchain system;

[0024] Figure 2C This is an optional schematic diagram of the block structure provided in an embodiment of this application;

[0025] Figure 3 This is a schematic diagram of the server structure provided in an embodiment of this application;

[0026] Figure 4This is an optional flowchart illustrating the program execution method provided in the embodiments of this application;

[0027] Figure 5 This is an optional flowchart illustrating the program execution method provided in the embodiments of this application;

[0028] Figure 6 This is an optional flowchart illustrating the program execution method provided in the embodiments of this application;

[0029] Figure 7 This is an optional flowchart illustrating the program execution method provided in the embodiments of this application;

[0030] Figure 8 This is an optional flowchart illustrating the process of obtaining the symbolic list of functions provided in an embodiment of this application.

[0031] Figure 9 This is a rearranged binary memory layout diagram provided in the embodiments of this application. Detailed Implementation

[0032] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0033] In the following description, references to "some embodiments" refer to a subset of all possible embodiments. However, it is understood that "some embodiments" can be the same or different subsets of all possible embodiments and can be combined with each other without conflict. Unless otherwise defined, all technical and scientific terms used in the embodiments of this application have the same meaning as commonly understood by one of ordinary skill in the art to which the embodiments of this application pertain. The terminology used in the embodiments of this application is for the purpose of describing the embodiments of this application only and is not intended to limit the application.

[0034] To better understand the program execution method provided in the embodiments of this application, the method for optimizing program startup speed in related technologies will first be explained:

[0035] In related technologies, optimization of startup and loading speed is usually achieved by reducing unnecessary code, lazy loading, and using multithreading. Alternatively, binary reordering can be used to compactly arrange all function codes executed sequentially during startup in a sequential binary, thereby significantly reducing the instruction jump range of registers.

[0036] Methods such as reducing unnecessary code, lazy loading, and utilizing multithreading mainly focus on reducing the main thread's workload, making it difficult to achieve significant improvements.

[0037] The purpose of binary reordering is to group function code together, specifically the most frequently executed or critically executed code, into a compact code text. After binary reordering, the high-frequency or critical code is arranged more compactly, which is more conducive to optimizing the startup phase, as well as the speed and memory usage during foreground / background switching or function call phases.

[0038] For rearranged binary code text, if all the code executed sequentially during startup is arranged in the order of execution, the overall frequency and number of page faults will be significantly reduced. For example... Figure 1 The diagram illustrates binary code text in related technologies. If there is a sequential call process for functions A, B, C, and D, then since functions A through D are not located in the same memory page, for example... Figure 1 Function A is in page 76, function B is in page 10, function C is in page 2, and function D is in page 23. Therefore, Figure 1 The call process requires four page faults, all occurring on non-adjacent pages. These four page faults result in four page breaks and four instances of physical page memory usage. If the program has many such call problems, it will frequently cause foreground / background switching or function call fragmentation, leading to increased physical page memory usage and reduced program speed.

[0039] Therefore, current optimization methods all focus on improving code loading and startup speed, and cannot enable a single physical page to load as many current or next functions to be executed as possible. As a result, they cannot further improve the startup and loading speed of the program, and thus cannot effectively improve the program's running speed.

[0040] To address the problems existing in related technologies, this application provides a program execution method. The server obtains a set of functions corresponding to the program to be executed; determines the frequency of function calls for each function in the set; determines page memory with contiguous memory for loading functions from the set based on the frequency of each function; loads the functions from the set into the page memory sequentially; and calls the functions in the page memory sequentially according to their addresses to execute the program. Thus, by determining the frequency of function calls for each function in the set before execution, and determining the page memory with contiguous memory for loading functions from the set based on the frequency of each function, functions with higher call frequencies can be loaded into the same or adjacent page memory, reducing the instruction jump amplitude of registers during program execution, thereby significantly reducing the program's startup memory and improving program execution speed.

[0041] The following describes exemplary applications of the program running device provided in the embodiments of this application. The program running device provided in the embodiments of this application can be implemented as various types of terminals such as laptops, tablets, desktop computers, and mobile devices (e.g., mobile phones, portable music players, personal digital assistants, dedicated messaging devices, portable gaming devices), or as a server. The following will describe exemplary applications when the program running device is implemented as a server.

[0042] See Figure 2A , Figure 2A This is an optional architecture diagram of the program execution system 10 provided in this application embodiment. To support any application, the program execution system 10 includes at least one terminal ( Figure 2A The diagram shows a first terminal 100-1 and a second terminal 100-2. The terminals are connected to a server 300 via a network 200. Each terminal can run an application. The application can exit normally or abnormally while running in the foreground. The user interface (UI) of the application can be displayed on the current interface 110-1 of the first terminal 100-1, and the UI of the application can also be displayed on the current interface 110-2 of the second terminal 100-2.

[0043] Taking the first terminal 100-1 as an example, when a user starts a program on the first terminal 100-1, or when the program is installed on the first terminal 100-1, the server 300 obtains the function set corresponding to the program from the first terminal 100-1; determines the frequency of each function call in the function set; and, based on the frequency of each function, determines a page memory 300-1 with contiguous memory for loading the functions in the function set. The functions in the function set are then loaded sequentially into the page memory 300-1. The page memory 300-1 can be located on the server 300. When the program on the first terminal 100-1 is started, the first terminal 100-1, through the network 200, sequentially calls the functions in the page memory 300-1 according to their addresses to run the program.

[0044] The program execution system 10 involved in this application embodiment can also be a distributed system 201 of a blockchain system, see [link to relevant documentation]. Figure 2B , Figure 2B This is an optional structural diagram of the program execution system 10 provided in this application embodiment applied to a blockchain system. The distributed system 201 can be a distributed node system formed by multiple nodes 202 (any form of computing device in the network, such as a server or user terminal) and clients 203. The nodes form a peer-to-peer (P2P) network. The P2P protocol is an application layer protocol running on top of the Transmission Control Protocol (TCP). In the distributed system, any machine, such as a server or terminal, can join and become a node. A node includes a hardware layer, a middleware layer, an operating system layer, and an application layer.

[0045] See Figure 2B The functions of each node in the blockchain system shown include:

[0046] 1) Routing: A basic function of nodes used to support communication between nodes.

[0047] In addition to routing capabilities, nodes can also have the following functions:

[0048] 2) Applications are deployed in the blockchain to implement specific business needs. They record data related to the implementation of functions to form record data, carry digital signatures in the record data to indicate the source of the task data, and send the record data to other nodes in the blockchain system. When other nodes successfully verify the source and integrity of the record data, they add the record data to a temporary block.

[0049] For example, the business logic implemented by the application includes:

[0050] 2.1) A wallet is used to provide the function of conducting electronic currency transactions, including initiating transactions (i.e., sending the transaction record of the current transaction to other nodes in the blockchain system; after other nodes successfully verify the transaction, they store the transaction record data in the temporary block of the blockchain as a response to acknowledge the validity of the transaction; of course, the wallet also supports querying the remaining electronic currency in the electronic currency address.

[0051] 2.2) Shared ledger, used to provide functions such as storage, query and modification of ledger data. It sends the record data of the operation on the ledger data to other nodes in the blockchain system. After the other nodes verify the validity, as a response to acknowledge the validity of the ledger data, they store the record data in a temporary block. They can also send confirmation to the node that initiated the operation.

[0052] 2.3) Smart contracts are computerized protocols that can execute the terms of a contract. They are implemented through code deployed on a shared ledger that executes when certain conditions are met. Based on actual business needs, the code is used to complete automated transactions, such as querying the logistics status of goods purchased by a buyer and transferring the buyer's electronic money to the merchant's address after the buyer signs for the goods. Of course, smart contracts are not limited to executing contracts for transactions; they can also execute contracts for processing received information.

[0053] 3) A blockchain consists of a series of blocks that are sequentially generated. Once a new block is added to the blockchain, it will not be removed. The blocks record the data submitted by the nodes in the blockchain system.

[0054] 4) Consensus is a process in a blockchain network used to reach an agreement on transactions in a block among multiple involved nodes. The agreed-upon block is appended to the end of the blockchain. Mechanisms for achieving consensus include Proof of Work (PoW), Proof of Stake (PoS), Delegated Proof-of-Stake (DPoS), and Proof of Elapsed Time (PoET).

[0055] See Figure 2C , Figure 2CThis is an optional schematic diagram of the block structure provided in this application embodiment. Each block includes the hash value of the transaction records stored in this block (the hash value of this block) and the hash value of the previous block. The blocks are connected through their hash values ​​to form a blockchain. Additionally, the block may include information such as a timestamp when it was generated. A blockchain is essentially a decentralized database, a chain of data blocks linked together using cryptographic methods. Each data block contains relevant information used to verify the validity of the information (anti-counterfeiting) and to generate the next block.

[0056] See Figure 3 , Figure 3 This is a schematic diagram of the structure of the server 300 provided in the embodiments of this application. Figure 3 The server 300 shown includes at least one processor 310, memory 350, at least one network interface 320, and a user interface 330. The various components in the server 300 are coupled together via a bus system 340. It is understood that the bus system 340 is used to implement communication between these components. In addition to a data bus, the bus system 340 also includes a power bus, a control bus, and a status signal bus. However, for clarity, ... Figure 3 The general labeled all buses as Bus System 340.

[0057] The processor 310 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor or any conventional processor.

[0058] User interface 330 includes one or more output devices 331 that enable the presentation of media content, including one or more speakers and / or one or more visual displays. User interface 330 also includes one or more input devices 332, including user interface components that facilitate user input, such as a keyboard, mouse, microphone, touch screen display, camera, other input buttons and controls.

[0059] Memory 350 may be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid-state memory, hard disk drives, optical disk drives, etc. Memory 350 may optionally include one or more storage devices physically located remote from processor 310. Memory 350 may include volatile memory or non-volatile memory, or both. Non-volatile memory may be read-only memory (ROM), and volatile memory may be random access memory (RAM). The memory 350 described in this application embodiment is intended to include any suitable type of memory. In some embodiments, memory 350 is capable of storing data to support various operations, examples of which include programs, modules, and data structures, or subsets or supersets thereof, as illustrated below.

[0060] Operating system 351 includes system programs for handling various basic system services and performing hardware-related tasks, such as the framework layer, core library layer, driver layer, etc., for implementing various basic business functions and handling hardware-based tasks;

[0061] The network communication module 352 is used to reach other computing devices via one or more (wired or wireless) network interfaces 320, exemplary network interfaces 320 including: Bluetooth, WiFi, and Universal Serial Bus (USB), etc.

[0062] The input processing module 353 is used to detect and translate one or more user inputs or interactions from one or more input devices 332.

[0063] In some embodiments, the apparatus provided in this application can be implemented in software. Figure 3 A program execution device 354 stored in memory 350 is shown. This program execution device 354 can be a program execution device in server 300, and can be software in the form of programs and plug-ins, including the following software modules: acquisition module 3541, first determination module 3542, second determination module 3543, loading module 3544, and calling module 3545. These modules are logically related and can therefore be arbitrarily combined or further divided according to the functions they implement. The functions of each module will be described below.

[0064] In other embodiments, the apparatus provided in this application can be implemented in hardware. As an example, the apparatus provided in this application can be a processor in the form of a hardware decoding processor, which is programmed to execute the program execution method provided in this application. For example, the processor in the form of a hardware decoding processor can be one or more application-specific integrated circuits (ASICs), DSPs, programmable logic devices (PLDs), complex programmable logic devices (CPLDs), field-programmable gate arrays (FPGAs), or other electronic components.

[0065] The following will describe the program execution method provided in this application embodiment with reference to the exemplary application and implementation of the server 300 provided in the embodiments of this application. See also Figure 4 , Figure 4 This is an optional flowchart illustrating the program execution method provided in the embodiments of this application, which will be combined with... Figure 4 The steps shown are explained.

[0066] Step S401: Obtain the set of functions corresponding to the program to be run.

[0067] Here, the function set includes at least two functions, and the program to be run corresponds to multiple functions. When running the program, the program runs by calling each function sequentially. In this embodiment, the function set corresponding to the program to be run can be obtained when the program to be run is installed on the terminal.

[0068] Step S402: Determine the frequency of each function in the function set when it is called.

[0069] Here, each function may be called multiple times during its execution, and the frequency of each function call may be the same or different. In this embodiment, the frequency of each function being called and the frequency of other functions being called can be determined by sequentially recording the function call process.

[0070] Step S403: Based on the frequency of each function, determine the page memory with contiguous memory for loading functions in the function set.

[0071] Here, the page memory used for loading functions is physical memory. This page memory can be the memory in the server used for loading program functions. Each page memory has a certain size and can load a certain number of functions. In this embodiment, the determined page memory is contiguous, and the page memory allows functions in the function set to be arranged closely together, thereby minimizing cross-page function calls during function calls.

[0072] In this embodiment of the application, the determined page memory can be one page or multiple pages. When the page memory includes multiple pages, the multiple page memories are contiguous page memories.

[0073] Step S404: Load the functions in the function set into the page memory one by one.

[0074] Here, after determining the page memory, each function in the function set is loaded into the page memory, so that the functions in the function set are closely arranged in the page memory.

[0075] Step S405: Call the functions in the page memory sequentially according to the page memory address to run the program to be run.

[0076] Here, when the program to be run is executed, the functions in the page memory are called sequentially according to the addresses of the page memory to realize the execution of the program.

[0077] The program execution method provided in this application determines the frequency of function calls for each function in the function set corresponding to the program before execution; and determines the page memory with contiguous memory for loading functions in the function set based on the frequency of each function. This allows functions with high call frequency to be loaded into the same page memory or adjacent page memory, reducing the instruction jump amplitude of registers during program execution, thereby significantly reducing the program's startup memory and improving the program execution speed.

[0078] Figure 5 Figure 5 is an optional flowchart illustrating a program execution method provided in an embodiment of this application. The method includes the following steps:

[0079] Step S501: Obtain the set of functions corresponding to the program to be run.

[0080] Step S502: Determine the frequency of each function in the function set when it is called.

[0081] It should be noted that steps S501 and S502 are the same as steps S401 and S402 described above.

[0082] Step S503: Sort the functions in the function set according to the frequency of each function to form a function sequence.

[0083] Here, the functions in the function set can be sorted in descending order of frequency of function calls to form a function sequence; alternatively, they can be sorted in ascending order of frequency of function calls to form a function sequence; or they can be sorted according to other specific rules to form a function sequence. This application does not limit the sorting method used in its embodiments.

[0084] In some embodiments, to ensure that frequently used functions are grouped into specific pages of memory, functions with a particular frequency can be arranged at specific positions in a function sequence based on their frequency. For example, the function sequence may include a first half and a second half, where functions are arranged in ascending order of frequency in the first half and in descending order of frequency in the second half.

[0085] Step S504: Adjust the page memory used for loading functions according to the function sequence to form page memory with contiguous memory.

[0086] Here, adjusting page memory means loading functions sequentially and tightly into page memory according to the order of functions in the function sequence, so that the page memory for loading functions is fully and effectively utilized, and the phenomenon of random loading of functions in page memory no longer occurs.

[0087] Step S505: Load the functions into the page memory in the order they are arranged in the function sequence.

[0088] Here, functions are loaded into page memory sequentially according to their order in the function sequence. This loading is done by placing functions at consecutive addresses within the page memory, ensuring that the functions are tightly packed in the page memory and thus minimizing the amount of page memory used.

[0089] Step S506: Call the functions in the page memory sequentially according to the page memory address to run the program to be run.

[0090] The program execution method provided in this application sorts the functions in the function set according to the frequency of each function to form a function sequence. In this way, functions with high call frequency or specific call frequency can be arranged in specific positions in the function sequence or closely arranged together. Since the functions with high frequency are called more often or call other functions more frequently, it can ensure that the functions are loaded into the page memory. When the program calls functions later, it can greatly reduce cross-page calls when calling functions, thereby reducing the number of page faults and improving the program execution speed.

[0091] In some embodiments, the frequency mentioned above includes the first call frequency and the first called frequency of the function; based on Figure 5 , Figure 6 This is an optional flowchart illustrating the program execution method provided in an embodiment of this application, such as... Figure 6 As shown, step S503 can be achieved through the following steps:

[0092] Step S601: The function whose first call frequency is greater than the first threshold and whose first called frequency is greater than the second threshold is determined as a high-frequency function.

[0093] Here, a high-frequency function refers to a function that has a high frequency of calls and is called by others. The first and second thresholds can be determined based on the actual function call relationships.

[0094] Step S602: Sort the high-frequency functions according to their calling order in the program to be run, forming a high-frequency function sequence.

[0095] Here, after selecting the high-frequency functions, it is necessary to sort them. This can be done by sorting them according to the order in which they are called in the program to be run, thus forming a high-frequency function sequence.

[0096] Step S603: Arrange the functions other than the high-frequency functions into the head or tail of the high-frequency function sequence according to their corresponding calling order in the program to be run, to form a function sequence.

[0097] Here, after sorting the high-frequency functions, the other functions are then sorted. In implementation, these other functions can be arranged before or after the high-frequency function sequence according to the order in which they are called in the program to be executed, forming the final function sequence. This ensures that the high-frequency functions are located in the middle of the entire function sequence, minimizing instruction jumps between them when they call or are called by other functions. This minimizes call distances, further reducing the number of page faults and minimizing inter-page jumps during cross-page calls, thus improving program execution speed.

[0098] In some embodiments, each function corresponds to a calling function pair and a called function pair. A calling function pair refers to a function pair formed by the function and another function that has a calling relationship with itself. For example, if function A calls function B, and function A is called by function C, then for function A, the calling function pair includes A and B, which can be represented as (A->B), and the called function pair includes A and C, which can be represented as (A<-C).

[0099] Based on the above relationship between calling function pairs and called function pairs, this application embodiment further provides a program execution method, please continue to refer to... Figure 6 Step S503 can also be achieved through the following steps:

[0100] Step S604: Determine the calling function pair and the called function pair corresponding to each function.

[0101] Here, the above frequencies include: the second call frequency corresponding to the calling function pair, and the second call frequency corresponding to the called function pair.

[0102] Step S605: Determine the first weight of the function pair based on the second call frequency.

[0103] Here, when the second call frequency is high, the corresponding first weight is high, and when the second call frequency is low, the corresponding first weight is low.

[0104] Step S606: Determine the second weight of the called function pair based on the second called frequency.

[0105] Here, when the second call frequency is high, the corresponding second weight is high; when the second call frequency is low, the corresponding second weight is low.

[0106] Step S607: Sort the functions in the function set according to the first weight and the second weight to form a function sequence.

[0107] In some embodiments, step S607 can be implemented by the following steps:

[0108] Step S6071: For each function, the first weight and the second weight are weighted and summed according to a first preset ratio to obtain the comprehensive weight of the corresponding function.

[0109] Here, the weights of the first and second weights are determined according to the importance of each function's calling and called pairs in the entire program to be run. Then, the first and second weights are weighted and summed based on their respective weights to obtain the comprehensive weight of the function.

[0110] Step S6072: Sort the functions in the function set according to the order of comprehensive weight from high to low to form a function sequence.

[0111] Figure 7 Figure 7 shows an optional flowchart of the program execution method provided in this application embodiment. Step S6072 can be implemented through the following steps:

[0112] Step S701: Merge the two functions with the highest overall weight in the function set to obtain function nodes.

[0113] Here, the two functions with the highest overall weights can be merged sequentially according to the decreasing overall weight to form function nodes.

[0114] Step S702: Determine the overall weight of the function nodes.

[0115] In some embodiments, step S702 can be implemented by the following steps:

[0116] Step S7021: Determine the function in the function set that is located before and adjacent to the function node as the first function, and the function that is located after and adjacent to the function node as the second function.

[0117] Step S7022: The sum of the weights of the functions from the first function to the function node is determined as the first weight sum.

[0118] Step S7023: Determine the sum of the weights of the functions in the function node to the second function as the second weight sum.

[0119] Step S7024: The first weight sum and the second weight sum are weighted and summed according to a second preset ratio to obtain the comprehensive weight of the function node.

[0120] Steps S7021 to S7024 above involve weighted summation of the weights of the two adjacent functions and the function node to obtain the comprehensive weight of the function node.

[0121] Step S703: The function node is used as a function in the function set to replace the two functions in the function set that are being merged.

[0122] Here, once the function nodes are determined, they are placed into the function set. For example, the function set {A, B, C} includes three functions A, B, and C, where functions A and B form the function node (A, B). Therefore, the function node (A, B) replaces functions A and B in the function set {A, B, C}, forming the function set {(A, B), C}.

[0123] Step S704: Repeat the merging process until all functions in the function set are sorted to form a function sequence.

[0124] Here, after merging the two functions with the highest overall weight, since the overall weight of the function node has been determined, the function node is treated as a new function in the function set. This new function node, along with the other unmerged functions, forms a new function set, and the merging process is repeated on this new set. In other words, the function merging process is executed again within the new function set until all functions in the set are sorted, forming a function sequence.

[0125] In some embodiments, before running the program to be run, a linked list of function symbols corresponding to the program to be run can be obtained first, and then a set of functions can be obtained based on the obtained linked list of function symbols, and the program running method of this application embodiment can be implemented. Figure 4 , Figure 8 This is an optional flowchart illustrating the process of obtaining the symbolic linked list of functions provided in an embodiment of this application, such as... Figure 8 As shown, the method includes the following steps:

[0126] Step S801: Obtain the function symbol of each function called by the program during runtime by using either compiler instrumentation or instrumentation during program runtime.

[0127] Step S802: Record the function symbols in the order in which the functions are called to form a linked list of function symbols.

[0128] Here, the function symbol list includes the function symbol of each function called by the program to be run.

[0129] Please continue to refer to Figure 8In some embodiments, the function symbol list can also be obtained through the following steps:

[0130] Step S803: Determine the set of services corresponding to the program to be run. Each service in the set of services corresponds to a code segment, and each code segment includes at least one function.

[0131] The service set includes at least the following services: startup service, front-end / back-end switching service, high-frequency service, and core service.

[0132] Step S804: Sort the corresponding code segments according to the order in which the business functions in the business set are implemented to obtain a code segment sequence.

[0133] Here, each code segment includes at least one function, and each code segment is used to implement one of the business functions in the above set of business functions.

[0134] In some embodiments, step S804 can also be implemented through the following steps:

[0135] Step S8041: Determine the order in which the startup service, the foreground / background switching service, the high-frequency service, and the core service are implemented to obtain a service order list.

[0136] Step S8042: Sort the code segments corresponding to the business order list according to the business order list to form the code segment sequence.

[0137] For example, the business set includes startup business, front-end / back-end switching business, high-frequency business A and B, and core business. The business order list is: startup business, high-frequency business A, core business, high-frequency business B, and front-end / back-end switching business. Then, according to the order of the businesses in the business order list, the corresponding code segments are sorted to form the following code segment sequence: startup business code segment, high-frequency business A code segment, core business code segment, high-frequency business B code segment, and front-end / back-end switching business code segment.

[0138] Step S805: Sort the function symbols of the functions in each code segment in the code segment sequence to form a linked list of function symbols.

[0139] Here, since each code segment includes at least one function, the function symbols within each code segment are sorted to form a final linked list of function symbols, which includes the function symbol of each function in the program to be run.

[0140] Correspondingly, step S401 can be achieved through the following steps:

[0141] Step S806: Obtain the set of functions corresponding to the program to be run based on the function symbol linked list.

[0142] Step S402 can be achieved through the following steps:

[0143] Step S807: Determine the frequency of the corresponding function based on the number of times the function symbol of each function appears in the function symbol chain list.

[0144] Here, the function symbol linked list can be traversed. When a function symbol of a certain function appears, the frequency of that function symbol is incremented by one, until the entire function symbol linked list has been traversed. In this embodiment, the frequency of a function symbol appearing in the function symbol linked list can be determined as the frequency of the corresponding function. For example, when function symbol K appears 10 times in the function symbol linked list, the frequency of function K1 corresponding to function symbol K is 10.

[0145] The program execution method provided in this application obtains the function symbol of each function called during the execution of the program to be run, forming a function symbol linked list. In the subsequent sorting process, the frequency of functions is determined based on the function symbol linked list, and the functions are sorted based on the determined frequency. Since the function symbol linked list contains every called function and is formed according to the order of function calls, the frequency of each function can be accurately determined. This allows for a reasonable and orderly arrangement of functions in the program to be run, making the functions loaded into memory pages more compact. This further reduces the number of page faults and minimizes the inter-page jump during cross-page calls, thereby improving the program execution speed.

[0146] The following will describe an exemplary application of the embodiments of this application in a real-world application scenario.

[0147] This application provides a program execution method that arranges the program startup, foreground / background switching, and critical core or high-frequency execution code in a compact order through code segment rearrangement, thereby reducing the physical memory required when loading or restoring code segments and thus reducing overall memory usage.

[0148] In this embodiment, code segment (binary) rearrangement is applied to optimize the memory usage of mobile applications. Combining the system's runtime characteristics and the technical features of storing data in memory-mapped files (mmap), the method specifically optimizes application startup memory, foreground / background switching, and high-performance code execution memory. This method can significantly reduce application startup memory and also effectively reduce the physical memory consumed by high-performance code execution, thereby providing better support for application stability.

[0149] The solution in this application is based on the limitations of mmap loading code segments and physical page memory size. On the operating system, application execution is based on the dynamic linker (dyld) bootloader. During the dyld bootloader process, mmap is used to load the corresponding code segment data into physical memory. During program execution, register values ​​are continuously adjusted to jump to different virtual addresses. When the physical memory for the corresponding virtual address does not exist, a page fault is triggered, and the Memory Management Unit (MMU) loads the data into physical memory and then establishes the mapping relationship between virtual addresses and physical memory.

[0150] By reducing the number of page faults, the corresponding physical page memory usage is reduced, allowing more code to be executed in a single physical page, thereby reducing the resident memory usage of code segments at runtime. Through appropriate adjustments to code segment memory layout, such as arranging code in execution order at startup, optimal code segment memory usage can be achieved during application startup.

[0151] Figure 9 This is a rearranged binary memory layout diagram provided in the embodiments of this application, such as... Figure 9 As shown, functions A, B, C, and D are arranged compactly in execution order, and preferably on the same page or adjacent pages. This is because if functions A, B, C, and D were arranged randomly, they might occupy four separate memory pages, resulting in a total memory usage of at least four pages. However, by arranging them in order... Figure 9 If the memory is arranged in a compact order, then in the optimal case it only requires one page of memory, resulting in a significant improvement.

[0152] In this embodiment, a single page fault triggers a loading of physical page memory. Due to hardware limitations, the size of a physical page varies across different processor architectures. For example, on some processors, the physical page size is 4KB, so a single page fault will trigger a loading of 4KB of consecutive memory; on other processors, the physical page size is 16KB, so a single page fault will trigger a loading of 16KB of consecutive memory. This application fully utilizes the 4KB or 16KB of physical memory loaded during a single page fault to execute the same code, including function calls, but with a lower total number of page faults and a lower required physical page memory.

[0153] The method in this application embodiment includes the following steps:

[0154] Step S11: Based on compiler instrumentation, save and record the function symbols executed at runtime, including function symbols of object-oriented programming languages ​​extended C (Objective-C), C, and C++, in the order of execution, forming a symbol record, and add it to a singly linked list.

[0155] Step S12: Each symbol record includes the currently called function symbol (denoted as callee) and the function symbol of the upper-level caller (denoted as caller), and records the number of calls from caller to callee; when a call from caller to callee is triggered once, the weight count is incremented by 1, and so on.

[0156] Step S13: A weighted call graph is formed based on steps S11 and S12. This weighted call graph is reorganized and sorted to minimize the overall average movement distance required to access the registers sequentially, thereby obtaining a new ordered sequence.

[0157] In this embodiment of the application, step S13 can be implemented through the following steps:

[0158] Step S131: For the weighted function call directed graph obtained in steps S11 and S12, merge the two nodes with the highest weights in sequence according to the weight decreasing strategy.

[0159] For example, the weight from function A to function B is 50, the weight from function A to function C is 20, the weight from function B to function C is 15, and the weight from function C to function D is 40. Following a decreasing weight strategy, we first merge the nodes from function A to function B, resulting in a new set of merged nodes (function A, function B). The weight of each node from (function A, function B) to its original neighboring node is the sum of its weights to that neighboring node. At this point, three nodes remain: (function A, function B), function C, and function D. The node with the highest weight is from function C to function D, so we merge function C and function D to obtain a new node (function C, function D). Finally, two nodes remain: (function A, function B) and (function C, function D).

[0160] Step S132: Repeat step S131 above, merging nodes with high weights in sequence, and ensuring that the final ordered sequence is also arranged according to the strategy of decreasing weights.

[0161] Step S133: Repeat the solution strategy from steps S131 to S132 multiple times to find multiple solutions, and finally select the overall optimal solution as the final ordered sequence for binary rearrangement.

[0162] Step S14: The ordered sequence symbol table obtained in step S13 is handed over to the linker to generate an ordered binary code segment, thereby achieving code segment memory optimization.

[0163] The method provided in this application's embodiments records a directed graph based on code execution order weights. It can be divided according to four scenarios: startup, foreground / background switching, high-frequency business, and core business. Finally, it assembles all symbols according to the original runtime execution order, forming a superior binary arrangement. This results in more efficient and lower physical memory usage for runtime code segments. It minimizes the occurrence of high-frequency function accesses being too far from the current PC, which would lead to inefficient random access and trigger numerous page faults and I / O loads.

[0164] The method described in this application has the following beneficial effects: it can optimize application memory without modifying the application or code, significantly reducing application startup memory and the memory increment of code segments when switching between foreground and background. Furthermore, it can help optimize foreground memory crashes and reduce the probability of users encountering memory-related crashes.

[0165] This application proposes a method to optimize application memory usage based on binary reordering and the limitation of physical page memory load size for page fault words. Within a limited page fault, it loads as much frequently executed or soon-to-be-executed code as possible, thereby alleviating the problem of useless code being frequently loaded into active page memory due to random code segment arrangement. This also reduces the overall application startup memory or runtime code segment physical memory.

[0166] The following continues to describe the exemplary structure of the program running device 354 provided in the embodiments of this application as a software module. In some embodiments, such as Figure 3 As shown, the software module stored in the program execution device 354 of the memory 350 can be the program execution device in the server 300, including:

[0167] The acquisition module 3541 is used to acquire the function set corresponding to the program to be run; the first determination module 3542 is used to determine the frequency of each function in the function set when it is called; the second determination module 3543 is used to determine the page memory with contiguous memory for loading the functions in the function set according to the frequency of each function; the loading module 3544 is used to load the functions in the function set into the page memory in sequence; and the calling module 3545 is used to call the functions in the page memory in sequence according to the address of the page memory to run the program to be run.

[0168] In some embodiments, the second determining module is further configured to: sort the functions in the function set according to the frequency of each function to form a function sequence; and adjust the page memory used to load the functions according to the function sequence to form a page memory with contiguous memory.

[0169] In some embodiments, the frequency includes a first call frequency and a first called frequency of the function;

[0170] The second determining module is further configured to: determine the functions whose first call frequency is greater than the first threshold and whose first called frequency is greater than the second threshold as high-frequency functions; sort the high-frequency functions according to the calling order of the high-frequency functions in the program to be run to form a high-frequency function sequence; and arrange the other functions besides the high-frequency functions in the calling order of the program to be run to the head or tail of the high-frequency function sequence to form the function sequence.

[0171] In some embodiments, the second determining module is further configured to: determine the calling function pair and the called function pair corresponding to each function; wherein the frequency includes: a second calling frequency corresponding to the calling function pair and a second called frequency corresponding to the called function pair; determine a first weight of the calling function pair based on the second calling frequency; determine a second weight of the called function pair based on the second called frequency; and sort the functions in the function set according to the first weight and the second weight to form the function sequence.

[0172] In some embodiments, the second determining module is further configured to: perform a weighted summation of the first weight and the second weight of each function according to a first preset ratio to obtain the comprehensive weight of the corresponding function; and sort the functions in the function set in descending order of the comprehensive weight to form the function sequence.

[0173] In some embodiments, the second determining module is further configured to: merge the two functions with the highest comprehensive weight in the function set to obtain a function node; determine the comprehensive weight of the function node; use the function node as a function in the function set to replace the two functions in the function set that are being merged; and repeat the merging process until all functions in the function set are sorted to form the function sequence.

[0174] In some embodiments, the second determining module is further configured to: determine the function located before and adjacent to the function node in the function set as a first function, and the function located after and adjacent to the function node as a second function; determine the sum of the weights of the first function to the functions in the function node as a first weighted sum; determine the sum of the weights of the functions in the function node to the second function as a second weighted sum; and perform a weighted summation of the first weighted sum and the second weighted sum according to a second preset ratio to obtain the comprehensive weight of the function node.

[0175] In some embodiments, the apparatus further includes: a function symbol acquisition module, configured to acquire the function symbol of each function called during the execution of the program by means of compiler instrumentation or instrumentation during the execution of the program; a recording module, configured to record the function symbols sequentially according to the order in which the functions are called, forming the function symbol linked list; correspondingly, the acquisition module is further configured to: acquire the function set corresponding to the program to be executed according to the function symbol linked list.

[0176] It should be noted that the description of the apparatus in this application embodiment is similar to the description of the method embodiment described above, and has similar beneficial effects as the method embodiment; therefore, it will not be repeated. For technical details not disclosed in this apparatus embodiment, please refer to the description of the method embodiment of this application for understanding.

[0177] This application provides a storage medium storing executable instructions. When these executable instructions are executed by a processor, they cause the processor to perform the method provided in this application, for example... Figure 4 The method shown.

[0178] In some embodiments, the storage medium may be a computer-readable storage medium, such as a ferromagnetic random access memory (FRAM), a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), flash memory, magnetic surface memory, optical disk, or a compact disk-read-only memory (CD-ROM); or it may be a device that includes one or any combination of the above-mentioned memories.

[0179] In some embodiments, executable instructions may take the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a standalone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.

[0180] As an example, executable instructions may, but do not necessarily, correspond to files in a file system. They may be stored as part of a file containing other programs or data, for example, in one or more scripts within a Hyper Text Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple co-located files (e.g., files storing one or more modules, subroutines, or code sections). As an example, executable instructions may be deployed to execute on a single computing device, or on multiple computing devices located in one location, or on multiple computing devices distributed across multiple locations and interconnected via a communication network.

[0181] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, and improvements made within the spirit and scope of this application are included within the scope of protection of this application.

Claims

1. A method for running a program, characterized in that, include: Determine the set of services corresponding to the program to be run; wherein, the set of services includes at least the following services: startup service, foreground / background switching service, high-frequency service, and core service; each service in the set of services corresponds to a code segment, and the code segment is used to implement the service; The code segments are sorted according to the order in which the business functions are implemented, resulting in a code segment sequence. The function symbols of the functions in each code segment in the code segment sequence are sorted to form a linked list of function symbols; Based on the function symbol linked list, obtain the function set corresponding to the program to be run; Based on the number of times the function symbol of each function appears in the function symbol chain, determine the frequency of each function in the function set when it is called; Based on the frequency of each function, determine the page memory used to load the functions in the function set and having contiguous memory; The functions in the function set are loaded into the page memory in sequence; The functions in the page memory are called sequentially according to the address of the page memory to run the program to be run.

2. The method according to claim 1, characterized in that, The step of determining, based on the frequency of each function, a page of memory with contiguous memory for loading functions in the function set includes: Based on the frequency of each function, the functions in the function set are sorted to form a function sequence; Based on the function sequence, the page memory used to load the function is adjusted to form page memory with contiguous memory.

3. The method according to claim 2, characterized in that, The frequency includes the first call frequency and the first called frequency of the function; The step of sorting the functions in the function set according to the frequency of each function to form a function sequence includes: Functions whose first call frequency is greater than the first threshold and whose first called frequency is greater than the second threshold are identified as high-frequency functions; The high-frequency functions are sorted according to their calling order in the program to be run, forming a high-frequency function sequence. Other functions besides the high-frequency function are arranged sequentially at the beginning or end of the high-frequency function sequence according to their corresponding calling order in the program to be run, thus forming the function sequence.

4. The method according to claim 2, characterized in that, The step of sorting the functions in the function set according to the frequency of each function to form a function sequence includes: Determine the calling function pair and the called function pair corresponding to each function; wherein, the frequency includes: the second calling frequency corresponding to the calling function pair and the second called frequency corresponding to the called function pair; The first weight of the function pair is determined based on the second call frequency; The second weight of the called function pair is determined based on the second call frequency; Based on the first weight and the second weight, the functions in the function set are sorted to form the function sequence.

5. The method according to claim 4, characterized in that, The step of sorting the functions in the function set according to the first weight and the second weight to form the function sequence includes: For each function, the first weight and the second weight are weighted and summed according to a first preset ratio to obtain the comprehensive weight of the corresponding function; The functions in the function set are sorted in descending order of their comprehensive weights to form the function sequence.

6. The method according to claim 5, characterized in that, The step of sorting the functions in the function set according to the comprehensive weight from high to low to form the function sequence includes: The two functions with the highest overall weight in the function set are merged to obtain function nodes; Determine the overall weight of the function nodes; The function node is used as a function in the function set to replace the two functions in the function set that are being merged. The merging process is repeated until all functions in the function set are sorted to form the function sequence.

7. The method according to claim 6, characterized in that, Determining the comprehensive weight of the function node includes: The function located before and adjacent to the function node in the function set is identified as the first function, and the function located after and adjacent to the function node is identified as the second function. The sum of the weights of the functions from the first function to the function node is determined as the first weight sum; The sum of the weights from the functions in the function node to the second function is determined as the second weight sum; The first weight and the second weight are weighted and summed according to a second preset ratio to obtain the comprehensive weight of the function node.

8. The method according to any one of claims 1 to 7, characterized in that, The method further includes: The function symbol of each function called by the program during runtime is obtained by using either compiler instrumentation or runtime instrumentation. The function symbols are recorded sequentially according to the order in which the functions are called, forming the function symbol linked list.

9. A program execution device, characterized in that, include: An acquisition module is used to determine the set of services corresponding to the program to be run; wherein, the set of services includes at least the following services: startup service, foreground / background switching service, high-frequency service, and core service; each service in the set of services corresponds to a code segment, and the code segment is used to implement the service; the code segments are sorted according to the order in which the services are implemented to obtain a code segment sequence; the function symbols of the functions in each code segment in the code segment sequence are sorted to form a function symbol linked list; and the set of functions corresponding to the program to be run is obtained according to the function symbol linked list. The first determining module is used to determine the frequency of each function in the function set when a function is called, based on the number of times the function symbol of each function appears in the function symbol chain list. The second determining module is used to determine, based on the frequency of each function, a page memory with contiguous memory for loading functions in the function set; A loading module is used to load the functions in the function set into the page memory in sequence; The calling module is used to sequentially call the functions in the page memory according to the address of the page memory, so as to run the program to be run.

10. The apparatus according to claim 9, characterized in that, The second determining module is further configured to sort the functions in the function set according to the frequency of each function to form a function sequence; Based on the function sequence, the page memory used to load the function is adjusted to form page memory with contiguous memory.

11. The apparatus according to claim 10, characterized in that, The frequency includes the first call frequency and the first called frequency of the function; The second determining module is further configured to determine the function whose first call frequency is greater than the first threshold and whose first called frequency is greater than the second threshold as a high-frequency function; The high-frequency functions are sorted according to their calling order in the program to be run, forming a high-frequency function sequence. Other functions besides the high-frequency function are arranged sequentially at the beginning or end of the high-frequency function sequence according to their corresponding calling order in the program to be run, thus forming the function sequence.

12. A computer-readable storage medium, characterized in that, It stores executable instructions for causing a processor to execute, thereby implementing the method of any one of claims 1 to 8.