TECHNICAL FIELD

The present invention relates to the field of computer technologies, and in particular, to a method and an apparatus for compiling a source code object, and a computer.

BACKGROUND

At present, a working process of developer-oriented programming software is mainly as follows: first receiving source code that is input by a developer; then compiling the source code, where the compiling process is specifically converting the source code into machine-recognizable target code; and finally executing the target code to implement a corresponding function.

In practical application, multiple objects are defined in the source code that is input by the developer, and the object is an entity that is obtained by encapsulating data and a related operation. To help manage a memory, a compiler that performs the compiling operation usually uses the following method to compile each object in the source code: first allocating a counter to each object and then setting a counting rule for each counter. The counting rule may be specifically as follows: When it is detected that a corresponding object is cited by a thread, a value of the counter increases by 1; and when it is detected that a corresponding object is released by a thread, a value of the counter decreases by 1, and when a value of a counter of an object is 0, memory space occupied by the object is released.

In a terminal, most threads run in parallel. Therefore, when the foregoing method is used, one object may be simultaneously cited by multiple threads in an execution phase of the target code. To ensure exclusivity of each thread for a cited object, in a compiling phase of the source code, in addition to setting an operation rule for the counter of each object, a locking rule and an unlocking rule are set for the counter of each object. The locking rule may be specifically as follows: When it is detected that a current object is cited by a thread, the counter is locked, and in this case, threads other than the current thread cannot operate the object. The unlocking rule may be specifically as follows: When it is detected that the current object is released by the foregoing thread, the counter is unlocked, and in this case, any thread can cite the current object. In practical application, there are thousands of objects defined in the source code. Therefore, using the foregoing compiling method results in low efficiency for compiling the objects in the source code.

SUMMARY

Embodiments of the present invention provide a method and an apparatus for compiling a source code object, and a computer, to improve efficiency for compiling an object in source code.

According to a first aspect, the present invention provides a method for compiling a source code object, where the method includes: determining, by a compiler, an object type of a to-be-compiled object in source code, where a counter is set for the to-be-compiled object; and when the object type of the to-be-compiled object is a type that can be operated by only one thread at one moment, setting, by the compiler, a counter counting rule for the counter of the to-be-compiled object. The counter counting rule may be specifically as follows: When it is detected that a corresponding object is cited by a thread, a value of the counter increases by 1; and when it is detected that a corresponding object is released by a thread, a value of the counter decreases by 1, and when a value of a counter of an object is 0, memory space occupied by the object is released.

With reference to the first aspect, in a first possible implementation, the method further includes: when the object type of the to-be-compiled object is a type that can be simultaneously operated by multiple threads at one moment, setting, by the compiler, a counter counting rule, a counter locking rule, and a counter unlocking rule for the counter of the to-be-compiled object. The counter locking rule may be specifically as follows: When it is detected that a current object is cited by a thread, the counter is locked, and in this case, threads other than the current thread cannot operate the object. The counter unlocking rule may be specifically as follows: When it is detected that the current object is released by the foregoing thread, the counter is unlocked, and in this case, any thread can cite the current object.

With reference to the first aspect or the first possible implementation of the first aspect, in a second possible implementation, the determining, by a compiler, an object type of a to-be-compiled object in source code includes: determining, by the compiler, whether the to-be-compiled object is a parent object or a child object; when determining that the to-be-compiled object is the parent object, determining, by the compiler, the object type of the to-be-compiled object according to an object identifier of the to-be-compiled object, where the object identifier is an identifier preset for the to-be-compiled object when the source code is compiled, and different identifiers correspond to different object types; and when the to-be-compiled object is the child object, determining, by the compiler, that the object type of the to-be-compiled object is an object type of a parent object of the to-be-compiled object.

With reference to the first aspect or the first possible implementation of the first aspect, in a third possible implementation, the determining, by a compiler, an object type of a to-be-compiled object in source code includes: obtaining, by the compiler, the object identifier of the to-be-compiled object, where the object identifier is preset for the to-be-compiled object when the source code is compiled; and determining, by the compiler, the object type of the to-be-compiled object according to the object identifier of the to-be-compiled object.

With reference to the first aspect, or the first possible implementation, or the second possible implementation, or the third possible implementation of the first aspect, in a fourth possible implementation, the method further includes:

obtaining, by the compiler from the source code, operation code associated with the to-be-compiled object;

determining, by the compiler according to a type of the operation code associated with the to-be-compiled object, whether the object type of the to-be-compiled object is set correctly; and sending, by the compiler, alarm information when the object type of the to-be-compiled object is set incorrectly.

According to a second aspect, an object processing method is provided, and the method includes: setting, by a compiler, a counter for an object; and when a type of the object is a type that can be operated by only one thread at one moment, performing, by the compiler, a counting operation on the counter of the object, and skip performing a counter locking operation and a counter unlocking operation.

With reference to the second aspect, in a first possible implementation of the second aspect, the method further includes:

when the object type of the object is a type that can be simultaneously operated by multiple threads at one moment, performing, by the compiler, a counting operation on the counter of the object, and performing a counter locking operation and a counter unlocking operation.

According to a third aspect, an apparatus for compiling a source code object is provided, and the apparatus includes: a determining unit, configured to determine an object type of a to-be-compiled object in source code, where a counter is set for the to-be-compiled object; and a first setting unit, configured to: when the object type of the to-be-compiled object is a type that can be operated by only one thread at one moment, set a counter counting rule for the counter of the to-be-compiled object.

With reference to the third aspect, in a first possible implementation, the apparatus further includes: a second setting unit, configured to: when the object type of the to-be-compiled object is a type that can be simultaneously operated by multiple threads at one moment, set a counter counting rule, a counter locking rule, and a counter unlocking rule for the counter of the to-be-compiled object.

With reference to the third aspect or the first possible implementation of the third aspect, in a second possible implementation, the determining unit is specifically configured to: determine whether the to-be-compiled object is a parent object or a child object; when determining that the to-be-compiled object is the parent object, determine the object type of the to-be-compiled object according to an object identifier of the to-be-compiled object, where the object identifier is an identifier preset for the to-be-compiled object when the source code is compiled; and when the to-be-compiled object is the child object, determine that the object type of the to-be-compiled object is an object type of a parent object of the to-be-compiled object.

With reference to the third aspect or the first possible implementation of the third aspect, in a third possible implementation, the apparatus further includes an alarm unit, configured to: obtain from the source code, operation code associated with the to-be-compiled object; determine, according to a type of the operation code associated with the to-be-compiled object, whether the object type of the to-be-compiled object is set correctly; and send alarm information when the object type of the to-be-compiled object is set incorrectly.

According to a fourth aspect, an object processing apparatus is provided, and the apparatus includes: a setting unit, configured to set a counter for an object; and a first execution unit, configured to: when a type of the object is a type that can be operated by only one thread at one moment, execute a counting rule for the counter of the object, and skip performing a counter locking operation and a counter unlocking operation.

With reference to the fourth aspect, in a first possible implementation of the fourth aspect, the apparatus further includes: a second execution unit, configured to: when the object type of the object is a type that can be simultaneously operated by multiple threads at one moment, perform a counting operation on the counter of the object, and perform a counter locking operation and a counter unlocking operation.

According to a fifth aspect, a computer is provided, including a processor, a memory, and a bus, where the memory is configured to store a computer executable instruction; and the processor is connected to the memory by using the bus, and is configured to execute the computer executable instruction stored in the memory, so that the compiler executes the method for compiling a source code object disclosed in the first aspect, or executes the object processing method disclosed in the second aspect.

It can be learned from the foregoing description that, in the embodiments of the present invention, the object in the source code is preset, according to the operation code associated with the source code, as the type that can be operated by multiple threads at one moment, or the type that can be operated by only one thread at one moment. For example, when the operation code associated with the object is serial code, the object type of the object is set as the type that can be operated by only one thread at one moment. When the operation code associated with the object is parallel code, the object type of the object is set as the type that can be operated by multiple threads at one moment. In addition, when compiling the object in the source code and determining that a current to-be-compiled object is of the type that can be operated by only one thread at one moment, the compiler sets only a counter counting rule for a counter of the to-be-compiled object, and does not set a counter locking rule and a counter unlocking rule. Relative to the prior art in which a counter counting rule, a counter locking rule, and a counter unlocking rule are set for a counter of each object in the source code, compiling efficiency is improved. Moreover, for an object that is operated by only one thread at one moment, operation code associated with the object is serial code, and then in an execution phase of target code, the foregoing object can be operated by only one thread at one moment. Therefore, using the method of the present invention may also ensure exclusivity of each thread for operation object at any moment. In conclusion, according to the present invention, compiling efficiency can be improved on the basis of ensuring exclusivity of each thread for the operation object.

