CN102707947B - High-efficiency dynamic type conversion implementation method - Google Patents

High-efficiency dynamic type conversion implementation method Download PDF

Info

Publication number
CN102707947B
CN102707947B CN201210124840.XA CN201210124840A CN102707947B CN 102707947 B CN102707947 B CN 102707947B CN 201210124840 A CN201210124840 A CN 201210124840A CN 102707947 B CN102707947 B CN 102707947B
Authority
CN
China
Prior art keywords
class
headgroup
chain
conversion
limit
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Expired - Fee Related
Application number
CN201210124840.XA
Other languages
Chinese (zh)
Other versions
CN102707947A (en
Inventor
张超
韦韬
黎斯达
丁羽
陈兆丰
段镭
张利华
赵晓濛
徐先栋
李坤
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Peking University
Original Assignee
Peking University
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Peking University filed Critical Peking University
Priority to CN201210124840.XA priority Critical patent/CN102707947B/en
Publication of CN102707947A publication Critical patent/CN102707947A/en
Application granted granted Critical
Publication of CN102707947B publication Critical patent/CN102707947B/en
Expired - Fee Related legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Abstract

The invention provides a high-efficiency dynamic type conversion implementation method. The method comprises a complier parsing source codes and constructing a class hierarchy chart, a first base class chart and a first base class chain; constructing a cross conversion domain according to the class hierarchy chart; dividing a downlink conversion domain according to the first base class chain; creating a thread table for each class, and storing to complied binary program; adding a pointer pointing to the thread table, a private table point for processing non-public inheritance and a conversion domain table in a virtual function table of each class, and storing the expanded virtual function table to the complied binary program; and the complier generating a runtime code for each dynamic cast sentence, and completing dynamic type conversion by looking up the thread table, the private table and the conversion domain table. The high-efficiency dynamic type conversion implementation method is used in equipment of the complier and the like to generate high-efficiency executable program and improve the runtime performance of the program.

Description

A kind of implementation method of efficient regime type conversion
Technical field
The invention belongs to program compilation technical field, be specifically related to the efficient implementation of one of the regime type conversion in C Plus Plus, can be used in the equipment such as compiler, for generation of efficient executable program, promote the run time behaviour of program.
Background technology
Compiling is that the source program translation utilizing program compiler (i.e. compiler) to be write by source language (as C++ etc.) is converted to the process of target program (binary program that namely directly can perform on machine), it be the most important foundation stone developed of modern software it
The regime type characteristic that C Plus Plus provides by providing runtime type information (RTTI) to realize other dynamic language (as JavaScript, Python etc.), it allows a variable to be interpreted as polytype in the process of implementation.C++ program variable knows self type information by operationally searching RTTI information, whether checking can be interpreted as target type, thus guarantees its type safety.This function of specific implementation be regime type conversion dynamic_cast operational character.
The type of C++ comprises fundamental type and self-defining class (i.e. class), and regime type changes the conversion normally between class.Relation in C Plus Plus between class comprises publicly-owned public, protection protected and privately owned private tri-kinds of inheritances, also has the one independently empty inheritance of virtual in addition.As fruit A directly inherits from class B (be denoted as A < B, or B > A), then B is claimed to be the direct parent of class A, for class A and B, if there is an inheritance link A < C1 < of A to B ... and < Cn < B (or be called a derivation chain B > Cn > of B to A ... > C1 > A), then title B is the parent (being denoted as A < < B or B > > A) of A, title A is the subclass of B, especially, the direct parent of A is also the parent of A.
In binary program after compiling, each example (i.e. object) of same class has identical memory mapping.Usually, the object of each class is by such own data member, and the subobject formation that its direct parent is corresponding.In addition, empty succession is more special in the memory mapping of object.For class A, if there are two parent C and D to be all directly empty succession from class B, then in A object, only have a B subobject, and comprised by C and D subobject simultaneously.In other words, if there is the non-empty inheritance link of n bar and the empty inheritance link of m (m > 0) bar in A to its parent B, then there is n+1 B subobject in A object, wherein 1 B subobject is comprised by the subobject in the empty inheritance link of all m bars, and all the other each B subobjects correspond respectively to a non-empty inheritance link.
For any one class Complete, and any one Source class subobject, there is some derivation chains in Source to Complete.To wherein each derivation chain Source > B1 > ... > Bn > Complete, intercept the longest public inheritance from Source, form a derivation chain Source > B1 > ... > Bk (namely this chain is all public derivation, but Bk > Bk+1 is not public derivation), claim the derivation chain of this intercepting to be the public derivation chain of this Source subobject in Complete.
The concrete use-pattern of Dynamic_cast operational character is dynamic_cast<TargetType> (src), wherein TargetType be the pointer of class, the quoting or void* of class.When TargetType is the pointer of class, src must be the pointer of an object, and what return is also the pointer of certain object; When TargetType is quoting of class, src must be quoting of an object, returns quoting of certain object.
For dynamic_cast, TargetType is the pointer of class or quoting of class, and its transformation rule is all similar, only has some difference in the rear rreturn value of type checking failure operationally.If TargetType is quoting of class, an exception of dishing out during dynamic_cast failure; If TargetType is the pointer of class, during dynamic_cast failure, return NULL null pointer.Thus, convenient in order to describe, suppose in the present invention that TargetType is this form of pointer of class, and quoting for class, embodiments of the present invention stand good.
Namely suppose that the dynamic_cast operation that we investigate has form: dynamic_cast<Target*> (src), wherein, src is the pointer of the object of a sensing Source type.Meanwhile, assuming that the maximum derivation of src object is to liking cmplt (namely src object is contained in cmplt object, but does not have other objects to comprise cmplt), and the type of cmplt is Complete.
Regime type conversion success or not, the relation between these three types (i.e. class) of Type C omplete depending on the maximum derivation object cmplt of the type Source of source object src, target type Target and source object src.According to the regulation of C++ standard (see The C++Standard, ISO/IEC 14882:2003), the rule of regime type conversion is as follows:
If 1. Source and Target is identical, then directly return source object src;
2. if source object src is null pointer NULL, then return NULL;
If 3. Target is the parent (up conversion) of Source, and if have in source object src and only have a Target class subobject tgt, and a certain bar public derivation chain of tgt comprises src, then return tgt.Otherwise, return NULL.
4. if not 3 kinds of situations above, then Source type must be Polymorphic type (namely wherein having Virtual Function).
If 5. switch target Target* is void*, then return the maximum derivation object cmplt of source object src.
6. if not several situation above, then regime type conversion operationally must carry out following type checking:
If a) Source is certain parent (down conversion) of Target, and if only comprise a Target class object tgt in all derivation chains of source object src, and a certain bar public derivation chain of src comprises tgt, then return tgt.
If b) there is not father/subclass relation (cross over transition) between Source and Target, if and certain the public derivation chain of source object src comprises its maximum derivation object cmplt, and have in cmplt object and only have a Target subobject tgt, and certain the public derivation chain of tgt also comprises cmplt, then return tgt.
C) otherwise, convert failed, returns NULL null pointer.
When compiling, the regime type conversion of front 3 rules can directly be completed when compiling by compiler, does not need runtime code to carry out operation bidirectional.Thus, the implementation of common regime type conversion only need pay close attention to Polymorphic type to implementation during the operation of the conversion of void* (rule 5), down conversion (regular 6a) and cross over transition (regular 6b).Existing dynamic_cast realizes, as the Visual C++ compiler (being called for short VC) of Microsoft and GNU C++ compiler (GCC) the implementation adopted, be all faced with serious performance issue.
The object of Polymorphic type (see rule 4) usually front four bytes saves an empty list index vfptr, and each object usually and the polymorphic direct parent of its first non-void share same empty list index vfptr.Vfptr has pointed to the virtual table vftable of this object, saves the Virtual Function pointer of object, and save the runtime type information RTTI of object in the bottom (namely address is the core position of vfpt-4) of this table in this table.VC and GCC is the RTTI by operationally searching src object, and mates with the type information typeinfo of Source type and Target type.
Fig. 1 is the code sketch of a C++ example procedure.What Fig. 2 showed is code shown in accompanying drawing 1 after GCC compiling, its runtime type information RTTI example.When code runs, its RTTI pointer is obtained by the vfptr pointer of src object, then the type information typeinfo of Complete class (the class A namely in figure) is obtained according to RTTI pointer, the type packets of information containing such name, such direct parent number, such type information typeinfo of all direct parent, the inheritance etc. of each direct parent.In GCC compiler language, runtime code, by recursively searching for typeinfo, finds all parent information of Complete, and mates the type information of Source and Target, and then judge whether can change successfully.In this implementation, it is a performance bottleneck that search typeinfo reduces the inherited information of Complete, consuming timely when it runs is directly proportional to parent object number.Thus, when the inheritance hierarchy more complicated of class (than during if any level Four parent), the time that regime type conversion operationally spends will be the hundred times of common multiplying.
Summary of the invention
The object of the invention is to solve the problems of the prior art, the implementation that a kind of regime type change is provided, can improve regime type conversion run time behaviour, and then lifting binary program overall operation time performance.
The implementation method of regime type conversion of the present invention, its step comprises:
1) compiler is resolved source code, builds Similar integral figure and headgroup class figure according to analysis result in compilation process; And according to described headgroup class figure, build headgroup class chain;
2) for each class, compiler builds cross over transition territory according to its Similar integral figure; And according to headgroup class chain, the class not belonging to its cross over transition territory in such parent is divided into some down conversion territories;
3) for each class, compiler creates a thread table for it, and is stored in the binary program after compiling; For the headgroup class chain of the parent intersection of sets collection non-NULL with such, this thread table retains a list item for it;
4) compiler is in traditional virtual table that each class is all, increases by one respectively and points to the pointer of described thread table, the private list index of the non-public inheritance of a process and a Transformation Domain table; To each intersection/down conversion territory, this Transformation Domain table retains a list item for it; Virtual table after this expansion is stored in the binary program after compiling;
5) for each dynamic_cast statement, code during compiler generating run, this runtime code is shown by the thread searching source object, private shows and Transformation Domain table completes regime type conversion.
Below know-why of the present invention is described further.
Basic thought of the present invention is: by adopting a kind of encoding scheme of novelty, and compressed cache institute is successful regime type transitional information likely; Operationally by searching a short and small array, and then complete regime type conversion fast.This thought is based on following true or principle:
First, for regime type conversion dynamic_cast<Target*> (src), if the Type C omplete of the maximum derivation object cmplt of known src when compiling, and all derivation chain informations of src to cmplt, then directly can complete regime type conversion when compiling, and not need the odd word of runtime code.
Secondly, the Type C omplete of the maximum derivation object cmplt that each object src associates, and all derivation chains of src to cmplt, all closely bound up with the vtable of this src object, because compiler builds a different vtable can to the different polymorphic subobject of all reference positions of each Complete class (it must be polymorphic object that the regime type that the present invention pays close attention to changes its source object src).
Thus, when compiling, can be each vtable plus external information, record institute is the relevant information changed of successful regime type likely.Operationally, directly can inquire about these information of vtable attachment, and then complete regime type conversion rapidly.
The following describes the implication of related notion in the inventive method.
Similar integral figure: one digraph G (Complete, V, E1, fV, fEV, fEP), wherein Complete be this succession figure for concrete kind.(1) V is the set on summit, represents classes all in source code respectively; (2) E1 is the set on limit, and limit A → B belongs to E1, a direct parent of and if only if B is A; (3) fV:V → bool is a mapping, and whether describe summit is polymorphic class.For summit A, fV (A)=true, and if only if that A is polymorphic class (namely comprising Virtual Function); (4) fEV:E1 → bool is a mapping, describes the whether empty succession in limit.Limit A → B, fEV (A → B)=true and if only if A void is inherited from B; (5) fEP:E1 → bool is also a mapping, and describing limit is public inheritance.For limit A → B, fEP (A → B)=true and if only if A public inheritance from B.
The Similar integral figure of whole program, can be built by the source code of analysis program.
For code shown in Fig. 1, its Similar integral figure is shown in Fig. 5.
Class layout: class layout can be obtained by Similar integral figure, also can first build class layout, then obtain Similar integral figure.Digraph LG (Complete, V ', E2, fT, fEV, fEP), wherein Complete be this layout for concrete kind.(1) V ' is vertex set, represents all subobjects of Complete class respectively; (2) E2 is the set on limit, and limit objA → objB belongs to E2, and and if only if, and objB subobject is directly contained in objA subobject; (3) fT:V ' → ClassType is a mapping, summit objA, fT (objA) is returned to the class of objA subobject; (4) fEV:E2 → bool is a mapping, describes the whether empty succession in limit; Limit objA → objB, fEV (objA → objB)=true and if only if objA void is inherited from objB; (5) fEP:E2 → bool is also a mapping, and describing limit is public inheritance; For limit objA → objB, fEP (objA → objB)=true and if only if objA public inheritance from objB.
Equally, by the source code of analysis program, to each class Complete, its layout can be built.
For code shown in Fig. 1, its class layout is shown in Fig. 4.
Headgroup class figure: one digraph FBG (V, E3), wherein (1) V is vertex set, represents all classes in source code respectively; (2) E3 is the set on limit, and limit A → B belongs to E3, the polymorphic direct parent of B is first the non-void of A that and if only if.
Headgroup class figure can be built by analysis source code equally, and headgroup class figure is a forest.Vfptr is shared for any in headgroup class figure a limit A → B, subclass object A and parent object B.
For code shown in Fig. 1, its headgroup class figure is shown in Fig. 6.
Headgroup class chain: some limits of removing headgroup class figure, can obtain several headgroup class chains thread.Specifically, for a limit B1 → B2 any in headgroup class figure, if it is public inheritance, and for any class Complete simultaneously inherited from B1 and B2, in Complete class layout, any one B2 class subobject is contained in a B1 class subobject all just, then retain this limit, otherwise remove this limit.Like this, headgroup class figure is transformed into a new forest.Then, for every tree in forest, adopt greedy method to intercept wherein most long-chain one by one, finally obtain some mutually disjoint headgroup class chain thread.
To each headgroup class chain, it all has character: (1) for any headgroup class chain Bn → ... → B2 → B1, the parent intersection of sets collection of it and any class Complete must be Bm → ... → B2 → B1 (wherein m <=n) this form.(2) if regime type conversion dynamic_cast<Bi*> (src) can be successful, wherein the maximum derivation object type of src object is Complete, then for all j meeting i <=j <=m, dynamic_cast<Bj*> (src) can both be successful, and their pointed same positions of returning.
For code shown in Fig. 1, its headgroup class chain figure is shown in Fig. 7.
Cross over transition territory: for each class Complete, inherit figure G according to it, all addressable and unique class node form its cross over transition territory crossDom.First, it is addressable and unique for identifying Complete node.Then, all vertex v in the bottom-up Complete of traversal one by one Similar integral figure G, identify its accessibility and uniqueness.For accessibility, if there is public inheritance w → v, and w is addressable, then v is addressable.And for uniqueness, (1) points to v if only there is a limit w → v, and w is unique, then v is unique; (2) if there are many limits to point to v, and all limits are all empty successions, then v is also unique.(3), under other situation, v is not unique.Finally, all addressable and unique summits form crossDom.
The cross over transition territory of Complete class has following character: (1) is for all class Target in cross over transition territory, if certain the public derivation chain of src comprises Complete object, then dynamic translation dynamic_cast<Target*> (src) necessarily can be successful.(2) for all Target classes that cross over transition is overseas, if dynamic_cast<Target*> (src) success, must be then down conversion (note: be noted that, during operation, up conversion is not paid close attention in regime type conversion).
Down conversion territory: for each class Complete, in the set of his father's class, does not belong to all classes in cross over transition territory, can be divided into some down conversion territories by the headgroup class chain belonging to it.Specifically, the parent A of Complete and parent B belongs to same down conversion territory, and and if only if, and A and B belongs to same headgroup class chain.
The down conversion territory of Complete class has character: (1) for any headgroup class chain Bn → ... → B2 → B1, suppose the parent intersection of sets collection of it and Complete be Bm → ... → B2 → B1, then B1, B2, ... Bm all belongs to the cross over transition territory of Complete, or they form some down conversion territories of Complete just.(2) for Complete any down conversion territory Bm → ... → B2 → B1 and any subobject src, the infima species supposing a certain bar public derivation chain being contained in src in this territory is Bk, then the class of certain the public derivation chain of all src of being contained in is Bk, Bk+1 just, ..., Bm.(3) for Complete any down conversion territory Bm → ... → B2 → B1 and any subobject src, the class supposing certain the public derivation chain of all src of being contained in is Bk, Bk+1, ..., Bm, then dynamic_cast<Bj*> (src) can successfully and if only if k <=j <=m, and only comprises a Bk object in all public derivation chains of src.
The solution of the present invention does not operationally need to carry out recursive search to the layout of Complete class, only needs to search the thread table of Complete, then directly reads relevant list item and completes regime type conversion.And list item number in thread table is general very little, such as in famous large-scale C++ project Google Chrome browser, its thread number is less than 16.And the list item in thread table is placed according to the order of sequence.Thus, can be shown by this thread of binary search, only need to search for log16=4 time.And traditional implementation, need to search for whole layout, and the node in layout is unordered, thus it is searched number of times and is directly proportional to interstitial content.More seriously, if the inheritance hierarchy inherited in figure is many, interstitial content is many, and its search performance is lower.The present invention has following unique advantage:
1) method of the present invention compares traditional method, and its run time behaviour has tremendous increase.Method of the present invention only spends tens multiplying times in reality test, and comparing traditional method needs a cost hundreds of multiplying time (even more), its performance boost more than 10 times;
2) when method of the present invention is run, required time is basic fixing, with the inheritance hierarchy change of target program, change greatly can not occur.Classic method is when the complexity that inheritance hierarchy becomes, and the required spended time of regime type conversion can rise appreciably.This characteristic makes this programme be applicable to all programs very high to requirement of real-time, such as, may be used in embedded device.
Accompanying drawing explanation
Fig. 1 is the code sketch of a C++ example procedure.
Fig. 2 is run-time memory layout and the RTTI hum pattern that GCC compiles the instance objects of the class A obtained.
Fig. 3 is the flow chart of steps of the implementation method of regime type of the present invention conversion.
Fig. 4 is the layout of class A and class H in Fig. 1.
Fig. 5 is the succession figure of class A and class H in Fig. 1.
Fig. 6 is the headgroup class figure of example procedure in Fig. 1.
Fig. 7 is the headgroup class chain figure of example procedure in Fig. 1.
Fig. 8 is the cross over transition territory of class A in Fig. 1.
Fig. 9 is each down conversion territory of class A in Fig. 1.
Figure 10 is the regime type conversion implementation memory mapping schematic diagram operationally that the present invention proposes.
Figure 11 is the result of implementation figure of example procedure under the present invention program of Fig. 1.
Figure 12 is the class layout of another one example procedure.
Figure 13 is the measurement results schematic diagram carrying out spent time when regime type is changed for code shown in Figure 11.
Embodiment
Below by embodiment also by reference to the accompanying drawings, the present invention is further illustrated.
The present invention is a kind of efficient regime type conversion method towards C Plus Plus.Fig. 1 is one section of C++ code sample, and this code snippet describes classes (totally 19) all in example procedure and its inheritance each other.This code snippet is one and simplifies version, eliminates internal members's function and the member variable information of each class.In its full version code, each class respectively comprises a Virtual Function and an integer member variable, and such as class D2 comprises Virtual Function void fooD2 () and an integer member variable int intD2.
Fig. 2 illustrates this code sample after GCC compiling, a category-A object memory mapping figure operationally.The RTTI information wherein changed for regime type is exactly the typeinfo of class.Define the name that typeinfo must comprise class in C++ standard, all the other realize details by each compiler unrestricted choice.There are three kinds of typeinfo in GCC, correspond respectively to class, other classes of thering is no the class of parent, only having a non-empty parent.The typeinfo information of all direct parent of A is contained in the typeinfo of each class A, and the inheritance of all direct parents and A, and offsetting from each other between them.Regime type conversion during operation depends on this typeinfo information.In simple terms, for regime type conversion dynamic_cast<Target*> (src), runtime code can search for the typeinfo of the maximum derivation object of src object, then the typeinfo of all parents is recursively searched for, and compare with the typeinfo of Source and Target, finally calculate transformation result.The run time behaviour of this implementation is very poor, especially all the more so for the class inheriting complicated structure.
The regime type conversion method of the present embodiment, is applicable to the pointer of class or quoting of class, and its key step as shown in Figure 3, is described in detail as follows:
1) according to source code analysis result, the layout LG of each Complete class is built.
Fig. 4 is the class layout of class A and class H in this code sample.Inherit from A1 and A2 for the non-void of class A, A, thus, A is made up of an an A1 and A2 subobject.Accordingly, in class layout, there are two limit A → A1 and A → A2.In like manner, the non-void of class A3 is inherited from C and empty to be inherited from A4, thus comprises a C subobject in A3, and A4 subobject is then that " void " is contained in A3, namely this A4 subobject by all empty successions from A4 and the subobject being contained in complete A object share.The rest may be inferred, can build the class layout of A.Common compiler has all achieved the structure of class layout.
2) according to source code analysis result, Similar integral figure G is built.
Fig. 5 is the Similar integral figure of class A and class H in this code sample.In class layout, each class may corresponding multiple node (namely there is multiple subobject of the same type).And in Similar integral figure, the corresponding node of each class.Equally, by scanning the inheritance of class one by one, can be easy to construct Similar integral figure.Such as, A inherits from A1 and A2, then there is limit A → A1 and A → A2 in the drawings.
Similar integral figure can be obtained by class layout; Also can according to the analysis result direct construction Similar integral figure of source code, and carry out step 1).
3) according to source code analysis result, headgroup class figure FBG is built.
Fig. 6 is the headgroup class figure of code sample.Each class corresponds to a node in this figure, and limit N1 → N2 exists, and first polymorphic parent of non-void of N1 class that and if only if is N2.Equally, by scanning the inheritance of class one by one, can be easy to construct headgroup class figure.Such as, first polymorphic parent of non-void of A is A1, then there is limit A → A1 in the drawings.
Based on this headgroup class figure, distribute an identifier to each node (i.e. each class).Allocation scheme is as follows: adopt first root sequence to travel through every tree, gives identifier to successively each node according to traversal order.Such as, for the one tree of Fig. 6, adopt first root sequence to travel through C2-C1-C-A3-A1-B-A2-G, each peer distribution identifier 1,2 can be given successively ..., 9.The rest may be inferred for other.The identifier that this scheme is distributed must meet character: the identifier of any class M is greater than the identifier of its headgroup class N.
4) according to headgroup class figure FBG, headgroup class chain is built.
Fig. 7 is the headgroup class chain of code sample.Based on headgroup class figure, headgroup class chain can be built.Concrete steps are: (1), for the every bar limit in headgroup class figure, if it is non-public inheritance, deletes this limit.Limit A3-> C and A2 → B in such as Fig. 6 is non-public inheritance, thus deleted.(2) for the every bar limit N1 → N2 in headgroup class figure, if in original program, there is certain class M and inherit from N1 and N2 simultaneously, but in M, the direct derivation object of certain N2 subobject not N1, then delete this limit.Such as the limit B → C in Fig. 6, in original program, class A inherits from B and C simultaneously, but A its immediate parent object of C class subobject A::A1::A3::C is an A3 subobject, instead of a B subobject, thus this limit is deleted.(3) in the figure of remainder, adopt greedy algorithm, for every tree in figure, take out most long-chain wherein one by one, obtain some headgroup class chains.Such as, headgroup class figure in Fig. 6 is after above-mentioned (1) and (2) step, limit A3-> C, A2 → B and B → C is deleted, in remaining figure, through greedy algorithm, the headgroup class chain figure shown in Fig. 7 can be obtained.
Based on headgroup class chain figure, distribute a numbering to each headgroup class chain, the headgroup class chain at such as class C place is endowed numbering 1, and the headgroup class chain at class B place is endowed numbering 3.Thus each class is encoded as (cid, tid), wherein cid is such identifier, and tid is the headgroup class chain numbering belonging to such.Such as class A is encoded to (6,2).Under this encoding scheme, the coding of each class is unique and different.
5) to each class Complete, cross over transition territory is built according to its Similar integral figure.
Fig. 8 is the cross over transition territory of class A.And if only if, and summit (class) v is addressable (namely v is a publicly-owned parent of A), and when v is unique (i.e. existence anduniquess v subobject in A object), v belongs to the cross over transition territory of A.Thus, the concrete building mode in the cross over transition territory of A is: first, and mark A is addressable and unique.Then, the bottom-up category-A that travels through one by one inherits all vertex v in figure G, judges accessibility and the uniqueness on each summit (class).Finally, all addressable and unique summits form cross over transition territory.
For node v, its accessibility judges in the following way: if there is certain limit w → v is public inheritance, and w is addressable, then v is also addressable.Be addressable by A, can draw A1, A2, A3 and A4 are addressable, other class all inaccessibles.
For node v, its uniqueness can be judged by following principle: 1) if only there is the limit (being assumed to be w → v) that is pointed to v, and w is unique, then v is unique.2) if there is the limit that many are pointed to v, but all these limits are all empty successions, then v is unique.3), under other situations, v is not unique.Be unique by A, can draw A1, A2, A3, A4, B, E, E1, E2 are unique, and other are not unique.The layout of this conclusion and class matches.
Finally, the cross over transition territory of A is obtained: A, A1, A2, A3, A4.
6) to each class Complete, in its parent, all classes not belonging to its cross over transition territory, the headgroup class chain belonging to such, is divided into some down conversion territories.
Fig. 9 is all down conversion territories of class A in this code sample.For all classes not belonging to cross over transition territory (i.e. A, A1, A2, A3, A4) in the parent set of A, the headgroup class chain belonging to each class, can obtain some down conversion territories.Such as class B belongs to headgroup class chain 3, and it forms a down conversion territory of class A.And class D and D1 belongs to headgroup class chain 6, they form another down conversion territory of class A.
Give cross over transition territory and all down conversion territories (having 6 down conversion territories in this code sample) numbering of class A respectively.Wherein cross over transition Field Number is 0 (being designated dom0 in Fig. 8), and other down conversion territory number consecutively is 1,2 ..., 6 (be designated dom1 in Fig. 9, dom2 ..., dom6).
7) each class Complete, creates a thread table, is stored in the binary program after compiling.For virtual table vtable each in Complete, additionally add a threadPtr pointer, a privatePtr pointer and a Transformation Domain table in the other direction at it.
For virtual table vtable (being stored in the binary program after compiling) each in Complete, suppose that all subobjects sharing this vtable are ptr (Source1), ptr (SourceN), wherein type Source1 < ... < SourceN, and ptr is the memory address of subobject reference position.In traditional scheme in vtable opposite direction address be vfptr-4 and vfptr-8 core position on stored the RTTI pointer of Complete class and the current subobject skew offset to Complete object respectively, the present invention program is opposite direction additionally additional a threadPtr pointer (address is vfptr-12), a privatePtr pointer (address is vfptr-16) and a Transformation Domain table (address is vfptr-20) of this virtual table vtable.
Figure 10 is the high-efficiency dynamic type conversion implementation object memory mapping schematic diagram operationally that the present invention proposes.For each vftable of class Complete, outside its original RTTI pointer and skew offset information, add threadPtr pointer, privatePtr pointer and a Transformation Domain table in addition.Be explained respectively below:
A) threadPtr pointer
The thread table of this pointed Complete class, a list item is preserved for each with the headgroup class chain of Complete parent set common factor non-NULL in this table, record the numbering tid of this headgroup class chain respectively, the maximum identifier maxTgtCid of the class of Complete parent set is contained in this headgroup class chain, the Transformation Domain numbering domid of the Complete class that this headgroup class chain is corresponding, this headgroup class chain is to the skew tgtOffset of this Transformation Domain.Obtain when wherein maxTgtCid and domid builds headgroup class figure and Transformation Domain above, tgtOffset then can calculate fast according to domid.If domid is 0 (namely this headgroup class chain is contained in cross over transition territory), then tgtOffset is the skew of the subobject (only having) in this headgroup class chain to Complete object; Otherwise tgtOffset=0.
In actual storage, search performance problem when considering operation, the list item in thread table divides two sections of storage, and wherein the table of forward only stores the numbering tid of headgroup class chain, stores the maxTgtCid of the headgroup class chain of correspondence, domid and tgtOffset in reverse table.
Carry out example (as shown in figure 11) using class A as Complete class, in code sample, comprise all 9 the headgroup class chains except being numbered 5 and 10 with the headgroup class chain of the parent set common factor non-NULL of A.Thus, in the thread table of A, comprise 9 list items, store successively according to the numbering of headgroup class chain respectively.For headgroup class chain 4, the parent set of it and A occur simultaneously for A2}, wherein maximum class identifier is exactly the identifier 8 of A2 self, therefore maxTgtCid=8; And this headgroup class chain belongs to cross over transition territory, therefore domid=0; And namely A2 subobject on this headgroup class chain is the skew 44 of A2 relative to A object to the skew of the root node (i.e. class A) in cross over transition territory, therefore tgtOffset=44.For another one headgroup class chain 6, it comprise class D1, D, F}, and the parent set of it and A occur simultaneously for D1, D}, wherein maximum class identifier is exactly the identifier 11 of D, therefore maxTgtCid=11; And this headgroup class chain belongs to down conversion territory 3, therefore domid=3; Because it does not belong to cross over transition territory, therefore tgtOffset=0.
B) Transformation Domain table
The Transformation Domain table increased in addition in each vftable of class Complete, for each Transformation Domain domid of Complete retains a list item, comprises a minTgtCid and srcOffset in list item.Suppose that all subobjects sharing this vftable are ptr (Source1) ..., ptr (SourceN).For the territory being numbered domid, namely this N number of subobject is srcOffset to the skew in domid territory; And can from this N number of subobject certain object successful conversion some class Target (belonging to domid territory) in, its correspondence the minimum value of cid be minTgtCid.MinTgtCid can pass through to investigate subobject ptr (Source1) ..., the public derivation chain of ptr (SourceN) and the common factor in domid territory calculate.
Using class A as Complete example (as shown in figure 11), class A comprises a cross over transition territory and 6 down conversion territories.Thus, 7 list items are comprised in the Transformation Domain table above each vftable.
First for subobject A::A1::A3::C::C1::C2, remember that this object is ptr (C2), share all to liking { ptr (C2) of its vftable, ptr (C1), ptr (C), ptr (A3), ptr (A1), ptr (A) }, the public derivation chain set of these subobjects is { C2 → C1 → C, A3 → A1 → A}.This public derivation chain set only has common factor with Transformation Domain 0 and Transformation Domain 1, therefore the list item minTgtCid of dom2 ..., dom6 is infinitely great INF (actual value 255 in corresponding Transformation Domain table, because the headgroup class chain of length more than 255 can not be there is in practical programs), there is not N/A (actual value 0) in corresponding srcOffset.And for the Transformation Domain 0 and 1 with public derivation chain set common factor non-NULL.Wherein Transformation Domain 0 is cross over transition territory, thus minTgtCid be the minimum identifier 4, srcOffset of all classes (i.e. A, A1, A2, A3, A4) in cross over transition territory then for sharing the subobject of this vftable to the skew of category-A, namely 0.For Transformation Domain 1, the public derivation chain of subobject and the common factor of this Transformation Domain of sharing this vftable are { C2, C1, C}, these classes can obtain from certain object conversion the subobject sharing this vftable, therefore minTgtCid is the minimum identifier in C2, C1 and C, and namely 1; And this Transformation Domain is not cross over transition territory, therefore srcOffset is then 0.
Again for subobject A::C::C1::C2 (this C is to one that the likes A empty subobject inherited), remember that this subobject is obj (C2), the all subobjects sharing its vftable are { obj (C2), obj (C1), obj (C) }, the public derivation chain set of these subobjects is that { C2 → C1 → C}, it only has common factor with Transformation Domain 1.Therefore for remaining 1 cross over transition territory and 5 down conversion territories, its minTgtCid=INF, and srcOffset=N/A.For Transformation Domain 1, the public derivation chain of subobject and the common factor of this Transformation Domain of sharing this vftable are { C2, C1, C}, these classes can obtain from certain object conversion the subobject sharing this vftable, therefore minTgtCid is the minimum identifier in C2, C1 and C, and namely 1; And this Transformation Domain is not cross over transition territory, therefore srcOffset is then 0.
C) privatePtr pointer
A privatePtr pointer is also add in each vftable of class Complete.If there is not non-public inheritance between all subobjects of this vftable shared, then this pointer is empty NULL.Otherwise, this pointed private table.This privateTable shows to retain a list item for all Transformation Domain domid of Complete class, and record is shared in all objects of this vftable, can the minimum value minSrcCid of the identifier of the object of certain class in successful conversion to domid Transformation Domain.If the minTgtCid=INF of domid Transformation Domain, then corresponding minSrcCid=INF.Otherwise investigate and share all subobjects of this vftable, calculating wherein can the up-to-date identifier of the object of certain class in successful conversion to domid territory.
Using class A as Complete example (as shown in figure 11), class A comprises a cross over transition territory and 6 down conversion territories.Thus, the privateTable pointed by each non-NULL privatePtr comprises 7 list items.
First for subobject A::A1::A3::C::C1::C2, remember that this object is ptr (C2), share all to liking { ptr (C2) of its vftable, ptr (C1), ptr (C), ptr (A3), ptr (A1), ptr (A) }, there is non-public inheritance A3 → C between these subobjects, thus privatePtr non-NULL.In its privateTable pointed to, owing to being numbered 2,3,4, minTgtCid=INF in the Transformation Domain of 5,6, therefore corresponding minSrcCid=INF.And for Transformation Domain 0, subobject ptr (C2), ptr (C1), ptr (C), ptr (A3), ptr (A1), have in ptr (A) and only have { ptr (A3), ptr (A1), ptr (A) } can be transformed in this Transformation Domain in certain class, thus minSrcCid is minimum identifier 4 in A3, A1 and A.And for Transformation Domain 1, subobject ptr (C2), ptr (C1), ptr (C), ptr (A3), ptr (A1), have in ptr (A) and only have { ptr (C2), ptr (C1), ptr (C) } can be transformed in this Transformation Domain in certain class, thus minSrcCid is minimum identifier 1 in C2, C1 and C.
Again for subobject A::C::C1::C2 (this C is to one that the likes A empty subobject inherited), remember that this subobject is obj (C2), the all subobjects sharing its vftable are { obj (C2), obj (C1), obj (C) }, wherein there is not non-public inheritance, therefore privatePtr=NULL.
Figure 11 is exactly the concrete result of implementation that the present invention is directed to code sample, which show the result that the vftable corresponding to two subobject A::A1::A3::C::C1::C2 and A::C::C1::C2 is expanded, and the thread of category-A shows information.Relevant construction step has made explanation when describing Figure 10, repeats no more here.
8) by compiler for target program provides one section of runtime code, to realize dynamic_cast function, when completing operation, search thread table, Transformation Domain table etc., so complete regime type conversion.
This code is encapsulated as a function, is finally linked in the binary program after compiling.This function accepts three parameters: source object src pointer, the coding (srcCid, srcTid) of source object type Source, and the coding (tgtCid, tgtTid) of target type Target.In this function, the logic of code is as follows:
If a) src pointer is for controlling pin NULL, return NULL.
B) according to src pointer, obtain its vfptr, and then obtain its vtable.
If c) Target is void*, then takes out offset from vfptr-8 position, and return void* pointer tgt, wherein tgt=(char*) src+offset.Otherwise
D) read threadPtr, the thread finding it to point in vfptr-12 position to show.
E) in this thread shows, search the tgtTid of the correspondence of Target type.If do not find tgtTid in this table, then return NULL.Otherwise
F) from this thread shows, read the maxTgtCid stored in tgtTid list item, if tgtCid>maxTgtCid, then return NULL.Otherwise
G) from this thread shows, domid and tgtOffset stored in tgtTid list item is read.
H) find Transformation Domain table from vfptr-20 position, read its domid list item, obtain minTgtCid and srcOffset.If tgtCid < is minTgtCid, then return NULL.Otherwise
I) privatePtr is taken out from vfptr-16 position,
If i. privatePtr is NULL, then change successfully, and return Target* pointer tgt, wherein tgt=(char*) src+srcOffset+tgtOffset.Otherwise
The privateTable table that ii finds privatePtr to point to, reads its domid list item, obtains minSrcCid.If srcCid < is minSrcCid, then return NULL.Otherwise
Iii. change successfully, and return Target* pointer tgt, wherein tgt=(char*) src+srcOffset+tgtOffset.
The present invention is by adopting a kind of encoding scheme of novelty, and compressed cache institute is successful regime type transitional information likely.Operationally by searching a short and small array, and then complete regime type conversion fast.The implementation performance that the present invention compares existing compiler there is significant increase, through experimental test, usually can promote the performance of tens times.
Figure 12 is the layout of the class of testing code sample used, Figure 13 is three conventional compiler GCC, VC, Intel CC is (because relevant trade mark limits, respectively with compiler 1 in figure, 2, 3 replace these three, its relative ranks has nothing to do) and the solution of the present invention, for code shown in Figure 12, carry out the tolerance of regime type conversion dynamic_cast<B1*> (src) the required time consumed, wherein src type is Ai, the maximum derivation object type of src is Ci, i is then transverse axis 0 in figure, 1, 2, 3, 4, represent Complete class (i.e. C0, C1, ..., C4) inheritance hierarchy.Can see, the run time behaviour of the solution of the present invention is tens times of conventional compiler, and when inheritance hierarchy is comparatively complicated (such as during four layers of succession), the performance that the present invention promotes is more.
It is last it is noted that above embodiment is only in order to illustrate technical scheme of the present invention but not to be limited; those of ordinary skill in the art can modify to technical scheme of the present invention or equivalent replacement; and not departing from the spirit and scope of technical solution of the present invention, protection scope of the present invention should be as the criterion with described in claim.

Claims (9)

1. an implementation method for high-efficiency dynamic type conversion, its step comprises:
1) compiler is resolved source code, builds Similar integral figure and headgroup class figure according to analysis result in compilation process; And according to described headgroup class figure, build headgroup class chain; The step building headgroup class chain comprises:
A) for the every bar limit in headgroup class figure, if it is non-public inheritance, this limit is deleted;
B) for the every bar limit N1 → N2 in headgroup class figure, if in original program, there is certain class M and inherit from N1 and N2 simultaneously, but in M, the direct derivation object of certain N2 subobject not N1, then delete this limit;
C) in the figure of remainder, adopt greedy algorithm, for every tree in figure, take out most long-chain wherein one by one, obtain some headgroup class chains;
2) for each class, compiler builds cross over transition territory according to its Similar integral figure; And according to headgroup class chain, the class not belonging to its cross over transition territory in such parent is divided into some down conversion territories;
3) for each class, compiler creates a thread table for it, and is stored in the binary program after compiling; For the headgroup class chain of the parent intersection of sets collection non-NULL with such, this thread table retains a list item for it;
4) compiler increases the pointer of a sensing described thread table, the private list index of the non-public inheritance of a process and a Transformation Domain table respectively in all virtual tables of each class; To each intersection/down conversion territory, this Transformation Domain table retains a list item for it; Virtual table after this expansion is stored in the binary program after compiling;
5) for each dynamic_cast statement, code during compiler generating run, is shown by the thread searching source object, private shows and Transformation Domain table completes regime type conversion.
2. the method for claim 1, is characterized in that, the target type of described regime type conversion is the pointer of class or quoting of class.
3. the method for claim 1, is characterized in that: for class A and class node v thereof, and and if only if when node v has accessibility and uniqueness in the Similar integral figure of A, and node v belongs to the cross over transition territory of class A.
4. method as claimed in claim 3, it is characterized in that, described accessibility judges in the following way: if there is certain limit w → v is public inheritance, and w is addressable, then v is also addressable.
5. method as claimed in claim 3, it is characterized in that, described uniqueness is judged by following principle:
1) if only there is the limit that is pointed to v, and be assumed to be w → v, and w is unique, then v is unique;
2) if there is the limit that many are pointed to v, but all these limits are all empty successions, then v is unique.
6. the method for claim 1, is characterized in that: step 3) described list item comprises: be contained in the skew to this Transformation Domain of the maximum identifier of the class of parent set, the Transformation Domain numbering of class that described headgroup class chain is corresponding and described headgroup class chain in the numbering of described headgroup class chain, described headgroup class chain.
7. method as claimed in claim 6, is characterized in that: described list item divides two sections of storages, and wherein the table of forward stores the numbering of headgroup class chain, and reverse table stores other list item.
8. the method for claim 1, is characterized in that: for a virtual table of class A, if there is not non-public inheritance between all subobjects of this virtual table shared, then described private list index is empty; Otherwise this pointed private table, this private shows as all Transformation Domains of class A retain a list item, and record can the minimum value of the identifier of the object of certain class in successful conversion to Transformation Domain.
9. the method for claim 1, is characterized in that: by step 5) described code wrap is a function, is linked in the binary program after compiling; This function accepts three parameters: source object pointer, source object type coding and target type are encoded.
CN201210124840.XA 2012-04-25 2012-04-25 High-efficiency dynamic type conversion implementation method Expired - Fee Related CN102707947B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201210124840.XA CN102707947B (en) 2012-04-25 2012-04-25 High-efficiency dynamic type conversion implementation method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201210124840.XA CN102707947B (en) 2012-04-25 2012-04-25 High-efficiency dynamic type conversion implementation method

Publications (2)

Publication Number Publication Date
CN102707947A CN102707947A (en) 2012-10-03
CN102707947B true CN102707947B (en) 2015-03-04

Family

ID=46900771

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201210124840.XA Expired - Fee Related CN102707947B (en) 2012-04-25 2012-04-25 High-efficiency dynamic type conversion implementation method

Country Status (1)

Country Link
CN (1) CN102707947B (en)

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107707544A (en) * 2017-09-29 2018-02-16 国网江苏省电力公司电力科学研究院 A kind of equipment automatic identification collocation methods of IEC 61850
CN108491190A (en) * 2018-03-28 2018-09-04 武汉斗鱼网络科技有限公司 Similar integral structural information acquisition methods, device and electronic equipment
CN109828796B (en) * 2019-01-17 2022-04-15 武汉精立电子技术有限公司 Plug-in calling method and device based on microkernel architecture
CN111124527A (en) * 2019-10-24 2020-05-08 成都无糖信息技术有限公司 Method for extracting virtual table function list in dynamic link library

Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7516449B2 (en) * 2005-01-04 2009-04-07 International Business Machines Corporation Run-time type conversion

Patent Citations (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7516449B2 (en) * 2005-01-04 2009-04-07 International Business Machines Corporation Run-time type conversion

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
何洪辉,刘骥宇.RTTI机制浅析.《计算机与现代化》.2010, *
苏常友,文建国.一种定制的、更高效的C++RTTI实现方法.《软件时空》.2011, *

Also Published As

Publication number Publication date
CN102707947A (en) 2012-10-03

Similar Documents

Publication Publication Date Title
Schulte et al. A multi-language computing environment for literate programming and reproducible research
CN103559449B (en) The detection method of a kind of code change and device
CN100474251C (en) Area based memory management for object-oriented program
US8307350B2 (en) Multi level virtual function tables
CN106462425A (en) Complex constants
CN102707947B (en) High-efficiency dynamic type conversion implementation method
CN100458687C (en) Shared code caching method and apparatus for program code conversion
CN104662514A (en) Modernization of legacy software systems based on modeled dependencies
Zhu et al. Conflict resolution for structured merge via version space algebra
CN103645930B (en) Assembly level is across the construction method of file Scheduling Framework
Schüle et al. Freedom for the sql-lambda: Just-in-time-compiling user-injected functions in postgresql
US5625822A (en) Using sorting to do matchup in smart recompilation
Mannel et al. Finding complex process-structures by exploiting the token-game
CN115495087A (en) Method for realizing reflection mechanism in block chain, compiling method, compiler and Wasm virtual machine
Lano et al. Translating UML-RSDS OCL to ANSI C
Vollmer et al. Compiling tree transforms to operate on packed representations
US5446899A (en) Hint generation in smart recompilation
Dujardin et al. Fast algorithms for compressed multimethod dispatch table generation
Conchon et al. Designing a Generic Graph Library Using ML Functors.
US8484614B2 (en) Fast late binding of object methods
US5535392A (en) Using hint generation to cause portions of object files to remain the same
Kieburtz Programming without pointer variables
CN105094209B (en) The restorative procedure and device of data
Zhu et al. Mastery: Shifted-code-aware structured merging
Kozlovics Fast model repository as memory for web applications

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C14 Grant of patent or utility model
GR01 Patent grant
CF01 Termination of patent right due to non-payment of annual fee
CF01 Termination of patent right due to non-payment of annual fee

Granted publication date: 20150304

Termination date: 20200425