Manual Refining of Specialized Classes
Through manual refinement and dependency-driven co-specialization technology, the specialization efficiency and performance issues of generic classes in programming languages are solved, achieving more efficient specialized class generation, and optimizing code representation and runtime performance.
Patent Information
- Application Number
- CN202010673980.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Priority Date
- 2014-04-22
- Filing Date
- 2015-04-21
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2035-07-30
AI Technical Summary
When existing programming languages use generic classes, there are trade-offs between code size, generation costs, and type specificity, resulting in potential damage to runtime performance. They also lack user control over specialized classes, making it difficult to implement more efficient specializations. realization.
Manually refining specialized classes allows developers to override or replace methods of generic classes, add methods specific to specialized types, and generate more efficient specialized versions using dependency-driven co-specialization and partial specialization techniques. , control the specialization process, and optimize code representation and implementation.
It improves the specialization efficiency and performance of generic classes, allows for more flexible specialization implementation, reduces code footprint and compilation costs, enhances runtime performance, and meets optimization needs for specific types.
Smart Images

Figure CN111813389B_ABST
Abstract
Description
[0001] This application is a divisional application of the PCT application entering the Chinese national phase, whose international filing date is April 21, 2015, national application number is 201580028725.4, and invention name is “Manual Refining of Specialized Classes”. Technical Field
[0002] The present invention relates generally to software development and more particularly to aspects of language development in any of a variety of programming languages, such as a platform-independent, object-oriented programming language, as one example. Background Art
[0003] In various programming languages, parametric polymorphism (e.g., generics) can be thought of as a way to make a language more expressive while still maintaining full static type safety. Parametric polymorphism can allow functions or data types to be written in a generic way so that they can handle various data types in the same way regardless of their different types. Such functions and data types can be called generic functions and generic data types. For example, a generic list class can be written as List<String, String> . <t>, which means it can be a list of elements of any type T, where T is specified separately from List.
[0004] When using an object-oriented language that does not provide a single type that can be a supertype for all other types, it is often not possible to generalize over certain types without using special conversion operations such as boxing (e.g., automatically converting a value of a primitive type like int to an object of a corresponding wrapper class like Integer). For example, traditionally Java TM The language does not allow generics over objects and primitive types.
[0005] Parametric polymorphism (generics) can also involve trade-offs between code size, generation cost, and type specificity. For example, a programming language may support generics for specialization of base types, but may generate the specialized classes statically, potentially increasing code footprint costs and compilation costs. Another programming language may generate a specialized instantiation of a template for each instantiation used by the program, which may result in a large static footprint. Still a third language may use a templated bytecode format and perform specialization only at runtime, which may require additional steps before using the generic class file, potentially compromising runtime performance. Summary of the invention
[0006] While the runtime specializer may always be able to generate an automatically specialized version of a generic class, in some cases, alternative forms of user control over specialization may be exploited. For example, automatic specialization may be used while also allowing specialization-specific method implementations to be used. In general, the set of members of a generic class may not change when a generic class is specialized. In other words, the same members as in the generic version may be present in the automatically specialized version. However, manual refinement of a specialized class may allow a developer to manually specialize a specific (potentially better) representation and / or implementation of one or more methods of the specialized class.
[0007] Manual refinement of a specialized class may include overwriting an existing or automatically specialized version of a method with a different version of the method. In addition, manual refinement may include including additional methods in a specialized class that are only used for specific (and not other) type parameterizations. Thus, a refined method may be included in a specialized class only when the corresponding generic class is specialized for a specific type parameterization.
[0008] For example, the generic class List can be generic for any T, and the automatically specialized version of List can include all fields and methods of the generic version, but if List is specialized for T = int, a new version of a specific class method can also be included in the specialized class. Therefore, according to some embodiments, if List is specialized for T = int, a manually specialized version of the method can be used instead of the automatically specialized version. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] Figure 1 is a logical block diagram illustrating components of a system that implements dependency-driven co-specialization of specialized classes according to one embodiment.
[0010] Figure 2 is a logical block diagram illustrating dependency-driven co-specialization according to one embodiment.
[0011] Figure 3 is a flow chart illustrating one embodiment of a method for dependency-driven co-specialization as described herein.
[0012] Figure 4 is a logical block diagram illustrating dependency-driven common specialization involving multiple dependencies in a dependency chain according to one embodiment.
[0013] Figure 5 is a logical block diagram illustrating dependency-driven common specialization including multiple dependencies according to an example embodiment.
[0014] Figure 6 is a logical block diagram illustrating partial specialization of a generic class according to one embodiment.
[0015] Figure 7 is a flow chart illustrating one embodiment of a method for partial specialization of a generic class as described herein.
[0016] Figure 8 is a logic block diagram illustrating partial specialization with dependency driven common specialization according to one embodiment.
[0017] Fig. 9 is a logical block diagram illustrating manual refinement of a specialized class according to one embodiment.
[0018] Fig.10 is a flow chart illustrating one embodiment of a method for manual refinement of specialized classes as described herein.
[0019] Fig.11 is a block diagram illustrating manual refinement for adding new methods to a specialized class, according to one embodiment.
[0020] Fig.12 is a flow chart illustrating one embodiment of a method for adding new methods via manual refinement of specialized classes as described herein.
[0021] Fig.13 is a logical block diagram illustrating manual refinement for bulk replacement of generic classes according to one embodiment.
[0022] Fig.14 is a flow chart illustrating one embodiment of a method for bulk replacement of classes via manual refinement as described herein.
[0023] Fig.15 is a logical block diagram illustrating manual refinement for a generic method according to one embodiment.
[0024] Fig.16 is a flow chart illustrating one embodiment of a method for manual refinement of generic methods as described herein.
[0025] Fig.17 is a logical block diagram illustrating an example computer system suitable for implementing dependency-driven common specialization of specialized classes according to one embodiment. DETAILED DESCRIPTION
[0026] This article describes the use of platform-independent, object-oriented languages such as Java TM Various embodiments of enhancements to the .NET programming language for software development to support various features related to specialization of generic types, classes, and methods.
[0027] For example, the loading or operation of a specialized class can trigger the specialization of other classes. For example, if a first class extends a particular superclass, then the specialization of the first class can trigger the specialization of the superclass. In addition, specializing a class can trigger the specialization of various classes that are used internally as part of the implementation of the class. According to one embodiment, the compiler can be configured to recognize dependency relationships between generic classes and describe the classes based on type variables of triggering types (e.g., types and / or type parameterizations) that trigger specializations of other classes based on the specialization of the first class.
[0028] The compiler may include references (e.g., structure references) indicating dependency relationships between classes when generating a class file. For example, in one embodiment, the compiler may be configured to write out (e.g., to a class file) a description of a generic supertype based on a type variable of a generic subtype. Therefore, in some embodiments, a class file may include information (e.g., metadata and / or descriptions) indicating a dependency relationship between one class and another class. Loading a first class may trigger loading of a second class based on the dependency relationship. In addition, according to various embodiments, there may be multiple levels of dependencies, and specialization of one class may trigger specialization of multiple other classes, which in turn may trigger specialization of other classes.
[0029] In addition, a generic class may have more than one specializable type parameter and it may be desirable to specialize one or more of the type variables without specializing other type variables (e.g., making the other type variables generic). In some embodiments, the result of partial specialization may be one or more additional generic classes that are further specializable for the remaining type parameters. According to some embodiments, a specializer executed during runtime may be configured to partially specialize a generic class (e.g., to produce a partially specialized class) and then further specialize the partially specialized class to generate a fully specialized class. Thus, rather than performing specialization of a generic class all at once (such as converting a Map<T> to a Map<T> ), a specialization may be performed on the same class.<K,V> Specialization into Map<int,int> or Map<long,int> ), but a type parameter can be partially specialized (such as producing a Map<K,int> ), then at some later time, the remaining type parameter(s) can be specialized, such as to generate a Map<int,int> or Map<long,int> .
[0030] Although the runtime specializer can always generate the automatic specialization version of the generic class, in some cases, the alternative form of the user's control of specialization can allow the use of automatic specialization, while also adding (or rewriting) the method implementation specific to the specialization. Generally speaking, when a class is specialized, the member set of the generic class may not change. In other words, the same member as in the generic version can exist in the automatic specialization version. However, the manual refinement of the specialization class can allow developers to manually specialize the specific (possibly better) representation and / or implementation of the specialization class. According to various embodiments, alternative and additional methods, data layouts, fields, etc. can change between the original generic class (and the automatic specialization version of the generic class) and the replacement class. Therefore, in some embodiments, alternative versions of classes, methods, fields, etc. can be provided for generic classes, while maintaining the same interface and representation, and in other embodiments, completely different representations (e.g., different and / or additional methods, data layouts, fields, etc.) can be provided for the specific specialization of the generic class.
[0031] For example, a generic class List can be generic for any T and an automatically specialized version of List can include all the fields and methods of the generic version, but if List is specialized for T=int, a new version of a particular class method can also be included in the specialized class. Therefore, according to some embodiments, if List is specialized for T=int, a manually specialized version of the method can be used instead of the automatically specialized version. Manual refinement of a specialized class can include overriding an existing or automatically specialized version of a method with a different version of the method. In addition, manual refinement can also include including additional methods in a specialized class that is only used for specific (and no other) type parameterizations. Therefore, a refined method can be included in a specialized class only when the corresponding generic class is specialized for a specific type parameter.
[0032] In addition, specialization can include providing replacement classes for generic classes. Bulk replacement of specialized classes can involve the ability to replace automatic specializations of generic classes with replacement classes. According to some embodiments, when a class is specialized for a particular type parameter, the automatically specialized version of the class may not be used at all and, instead, a completely different handwritten class may be used. In some embodiments, alternative, handwritten specializations of a class may be used (and / or required) when compiling a generic class. However, in other embodiments, alternative (e.g., replacement or refinement) specializations of a generic class may be used after the class is compiled (e.g., before or when the generic class is specialized).
[0033] In addition, a replacement class may have the same interface as the generic or auto-specialized version, but it may have a completely different representation and / or implementation. However, in some embodiments, a replacement class must have (e.g., may be required to have) the same interface as the generic or auto-specialized version. For example, according to some embodiments, a replacement class that does not have all methods (e.g., all non-private methods) of the base class (and therefore the auto-specialized version of the class) may result in errors.
[0034] In some embodiments, a replacement class may be required to have (at least) all of the same methods that a generic or auto-specialized version of a class would have (e.g., so that the alternative version of a class obeys the Liskov Substitution Principle). The runtime environment may load the alternative version of a class whenever a particular specialization is instantiated. A generic or auto-specialized version of a class may include information identifying the alternative version of the class and indicating which type parameterizations may trigger use of the alternative version.
[0035] Alternatively, information identifying alternative versions of a class and indicating which type parameterizations may trigger use of the alternative version may be stored somewhere other than the generic or automatically specialized version of the class, such as in a class file or in an information registry, and thus the runtime may not have to load the generic or automatically specialized version of the class when the alternative version of the class is used.
[0036] In the following detailed description, numerous specific details are set forth to provide a thorough understanding of the claimed subject matter. However, those skilled in the art will appreciate that the claimed subject matter can be practiced without these specific details. In other cases, in order not to obscure the claimed subject matter, methods, devices, or systems are not described in detail below because they are known to those of ordinary skill in the art.
[0037] Although various embodiments are described herein by way of examples of several embodiments and illustrative drawings, those skilled in the art will recognize that the embodiments are not limited to the described embodiments or drawings. It should be understood that the drawings and their detailed description are not intended to limit the embodiments to the specific forms disclosed, but rather are intended to cover all modifications, equivalents, and alternatives within the spirit and scope of the present disclosure. Any title used herein is only for organizational purposes and is not meant to be used to limit the scope of the description. As used throughout this application, the word "may" is used in a permissive sense (i.e., meaning that it is possible) rather than in a mandatory sense (i.e., meaning that it must). Similarly, the words "include" and "comprise" mean including but not limited to.
[0038] Some parts of the following detailed description are given in terms of algorithms or symbolic representations of operations on binary digital signals stored in the memory of a special device or a dedicated computing device or platform. In the context of this particular specification, the term special device, etc. includes a general-purpose computer, once it is programmed to perform a specific function according to instructions from program software. Algorithmic descriptions or symbolic representations are examples of techniques used by ordinary technicians in signal processing or related fields to convey the essence of their work to other technicians in the field. Algorithms are here and are generally considered to be self-consistent sequences of operations or similar signal processing that lead to desired results. In this context, operations or processing involve physical manipulation of physical quantities. Usually, but not necessarily, such quantities can take the form of electrical or magnetic signals that can be stored, transmitted, combined, compared or otherwise manipulated. Sometimes, mainly for reasons of common use, it proves to be convenient to refer to such signals as bits, data, values, elements, symbols, characters, items, numbers, numerical values, etc. However, it should be understood that all of these or similar terms should be associated with appropriate physical quantities and are merely convenient labels. Unless otherwise specifically noted, as will be apparent from the following discussion, it should be understood that throughout this specification, discussions utilizing terms such as "processing," "computing," "determining," etc. refer to the actions or processes of a specific apparatus, such as a special purpose computer or similar special purpose electronic computing device. Thus, in the context of this specification, a special purpose computer or similar special purpose electronic computing device is capable of manipulating or transforming signals that are typically represented as physical electrical or magnetic quantities in a memory, register, or other information storage device, transmission device, or display device of a special purpose computer or similar special purpose electronic computing device.
[0039] Now go to Figure 1 , Figure 1 A workflow for compiling and executing a computer program specified in a high-level, platform-independent, object-oriented language according to various embodiments is shown, wherein the language supports basic and reference data types and various methods, features, and enhancements for specialization of generic classes and / or methods as described herein. For purposes of illustration, the following description is primarily based on the use of Java TM However, it should be noted that the described techniques can be used with almost any object-oriented programming language that supports multiple types (such as primitive types, reference types, record types, union types, etc.) and their specializations in an appropriate context.
[0040] According to the illustrated embodiment, the workflow may begin when a compiler, such as compiler 120, may be implemented on one or more computing devices and may receive source code for a computer program, such as source code 110. In various embodiments, source code 110 may be in various high-level and / or platform-independent, object-oriented programming languages, such as Java. TM and / or other languages). For example, when using Java TM In some embodiments, the source code 110 may be provided as a set of .java files. In some embodiments, the source code 110 may be specified using a combination of languages that may include one or more low-level and / or intermediate languages (e.g., assembly). In some embodiments, at least some of the source code may be initially written in a dynamically typed high-level language such as Python or Ruby, while in other embodiments, all of the source code may be written in a dynamically typed high-level language such as Java. TM It can be written in a statically typed language such as .
[0041] In general, a class can be considered as a user-defined type or data structure, which can include data, variables, functions, methods and / or other attributes as members, and represents a definition, blueprint or template for creating a specific type of programming object program. A class can provide initial values for data members and provide implementations for member functions and methods. Classes are often included in libraries. A library can be considered as a collection of resources used by a software program or application. According to some embodiments, a library can include any of various types of resources, including but not limited to data, documents, classes, subroutines and / or type specifications. A library can be organized so that it is used by more than one application (possibly at the same time), and reusability can be promoted by providing resources so that applications do not have to implement (or reimplement) the same behavior.
[0042] Compiler 120 may analyze source code 110 to produce an executable version of the program or bytecode file, such as annotated class file(s) 130 in the depicted embodiment (e.g., in Java TM In various embodiments, different types of executable code formats may be used; for example, binary machine language may be used instead of bytecode. In some scenarios, portions of the executable code (e.g., annotated class file 130) may be bytecode, while other portions may be native binary machine language.
[0043] According to some embodiments, as part of compiling program source code 110 into executable code (e.g., annotated class file 130), compiler 120 may perform a series of analysis operations and generate various intermediate data structures, such as application or implementation specializations, before generating an executable version. For example, the compiler may apply type erasure operations using the encoded (annotated or modified) form of program elements in a generic class to create annotated class file 130. As noted above, the encoded form of program elements may include metadata indicating which type variables of a class (or method) declaration have been erased and which types are erased of type variables.
[0044] In some embodiments, a compiler, such as compiler 120, may retain certain types of information that may not traditionally be retained during the compilation process. For example, in one embodiment, compiler 120 may retain information used as part of performing erasure. The compiler may include (at least part of) such information in annotated class files 130 as metadata about specializations.
[0045] For example, type erasure operations can be applied using the encoded (or annotated) form of program elements in a generic class. Program elements can represent class signatures or declarations, method signatures or declarations, instructions, arguments to instructions, and virtually any program element that represents, moves, or manipulates data.
[0046] The encoded form of a program element may include metadata indicating which type variables of a class (or method) declaration have been erased and which types are erasures of type variables. In addition, metadata may include various types of specialization-related information. For example, metadata may indicate instructions for operating on the values of type variables of a class (or method declaration). Similarly, metadata may indicate that an actual parameter indicates a type that is an erased version of a type variable of a class or method declaration.
[0047] According to some embodiments, metadata in annotated class files may include specialization information indicating which type variables have been erased and which types are erased of type variables. Type erasure involves the use of generics in a programming language (e.g., the Java language). When performing a type erasure operation, the compiler may be configured to replace all type parameters in a generic type with their bounds or with the Object type if the type parameter is unbounded. The generated bytecode may therefore contain only ordinary classes, interfaces, and methods.
[0048] In addition, the structural description can be used to expand the way to describe classes in the runtime environment. Instead of describing the class only by name and using the name to locate this class (for example, in the class file on the disk), the class can be referred to as a combination of a code generator function and possibly some actual parameters. In other words, in some embodiments, the class can be described by specifying the structural description of the generator function and possibly one or more formal parameters for the generator function. In some embodiments, the structural description can be considered as a class name (for example, can be used instead of the class name) for almost any situation where the class name can be used (for example, actual parameter type, return type, field type, super type, instanceof or cast operator operand, etc.).
[0049] Thus, in some embodiments, a compiler and / or a virtualized runtime environment may be configured to allow classes to be described using structural descriptions. For example, structural descriptions may be used to describe dependency relationships between classes (e.g., superclass / subclass relationships). According to some embodiments, a structural identifier of a class may be used using structural descriptions, such as in the manner in which an extended class is described in a runtime environment. According to some embodiments, a structural description of a class may appear anywhere, while a class name may appear in a purely nominal VM (e.g., a VM that is not configured to support structural descriptions as class names). For example, a structural description of a class may appear as or in place of an actual parameter type, return type, field type, supertype, instanceof, or operand of a cast operator, and generally anywhere a class name may be used.
[0050] like Figure 1 As shown in , the annotated class file 130 can be passed to an execution environment, such as a virtualized runtime environment 140, that executes the code on the execution platform 100, thereby creating various output data and / or behaviors. According to various embodiments, the virtualized runtime environment 140 can include multiple different components, such as a memory manager 160, a specializer 170, a class loader 175, a bytecode verifier 180 (e.g., to check the validity of the executable code), and / or an interpreter and / or a just-in-time (JIT) compiler 185. In some embodiments, the JIT compiler can be responsible for translating some or all of the bytecodes (e.g., frequently used portions of the bytecodes) into platform-specific machine code to improve the performance of program execution. In some embodiments, the virtualized runtime environment 140 can also include code that implements multiple application programming interface (API) libraries 190. In some embodiments, the virtualized runtime environment 140 can run on top of lower-level software such as an operating system 195.
[0051] In various embodiments, output or behavior generated as a result of execution of the compiled code may include data stored in various levels of system memory (e.g., memory objects and / or data structures), data stored on persistent storage devices (e.g., files on a file system), etc. Behavior may also include various program functions, such as displaying output on a screen, sending messages over a network, and / or otherwise interacting with various users and / or components.
[0052] In some embodiments, the virtualized runtime environment 140 may use the annotated class file(s) 130 generated by the compiler 120. According to some embodiments, when loading a class to be specialized with a particular parameterization, the virtualized runtime environment 140 may use the annotated class file 130 as a template (along with the parameters to be used for the specialization) and may generate a new class that is a particular specialization of the class being specialized. For example, when specializing a class, a set of type parameters may be part of the input for the specialization (e.g., List<String, String> ).<any T> Can be specialized using T = int as a parameter to obtain List <int>). Generally speaking, the annotated class file 130 can be considered as a class file marked with metadata related to the specialization. In addition, according to various embodiments, the annotated class file 130 can become a new class that is a specialization of the annotated class.
[0053] Dependency-driven co-specialization of specialized classes
[0054] According to some embodiments, the loading or operation of a specialized class may trigger the specialization of other classes. For example, if "classArrayList <t>extends AbstractList <t>", then ArrayList <int>An instantiation of AbstractList can be requested <int>Similarly, given a class Map that internally includes as part of its implementation a class that represents a node that maps<keyType,ValueType> , the keyType and valueType of this class will also be generic. Using dependency-driven common specialization, according to some embodiments, when the generic class Map<keyType,valueType> is instantiated as a map from int to long (for example, Map<int,long> ),Map<int,long> The specialized implementation of can then trigger the specialization of (one or more) dependent types (such as the internal node classes mentioned above).
[0055] Thus, according to some embodiments, a compiler such as compiler 120 may recognize dependency relationships between generic classes and may describe dependent types in terms of type variables that trigger the type (e.g., the type that triggers the specialization of the dependent type). For example, when a compiler writes out a class file instead of nominally writing / doing everything, such as when an ArrayList<T> is created, the compiler may write a class file that is not a class file, but a class file that is not a class file. <t>When erased to just ArrayList so that ArrayList extends List (as is done in legacy compilers), a compiler configured to implement dependency-driven common specialization can include structure references indicating dependency relationships in the class file.
[0056] For example, if ArrayList extends List, instead of just using the name List, the compiler can use a structural description that refers to, for example, "the class produced by applying a specialization transformation to List using a type parameter T". In other words, the compiler 120 can be configured to write out (e.g., to a class file) a description of the generic supertype in terms of the type variables of the generic subtype. Thus, a class file can include a statement indicating that the class ArrayList <t>Extends the description of a class generated by applying an actual parameter (e.g., T) to a specialized code generator. Loading an ArrayList may trigger the loading of a List and the List may be described by a structural description so that a specializer (and / or a class loader) may apply the structural description to generate a specific parameterized class List for T and load it into a runtime cache. Thereafter, the List may be used to extend, such as by an ArrayList.
[0057] According to one embodiment, in the structure description of the superclass (for example, in ArrayList <t>extends List <t>), the type variable T from ArrayList appears in the structure description of the supertype, so that when ArrayList <t>When T=int is specialized, occurrences of T in the supertype structure description are replaced by int. Thus, the type parameter of T as declared in ArrayList gives the meaning of "T" in the "extends" clause and in the ArrayList body. When the class is generated, the class ArrayList <int>Can be extended by applying List to int specialization transformation and can trigger List <int>The loaded class, so that ArrayList <int>Extending List <int>.
[0058] According to some embodiments, when declared between "<>", the type parameter T introduces the type variable T, similar to the way the formal parameters declared between "()" of a method introduce variables. The type variable T can be used in the signature of the generic class (e.g., as in the "extends" clause) and in the body of the generic class (e.g., as in the type of a field). According to some embodiments, in the signature of the generic class, the type variable T can appear in the type argument position (e.g., "extendsAbstractList <t>"), but not in the top-level position (for example, "extends T"). However, within the body of a generic class, a type variable T can appear in the type argument position (for example, as a field declaration, such as "List <t>f;") and in top-level position (e.g., as a field declaration, such as "T f;"). According to some embodiments, co-specialization may be triggered whenever a type variable T is used in a type argument position.
[0059] At runtime, specialization of one type (or class) can trigger specialization of other types. For example, according to some embodiments, if Foo <t>The implementation references Bar <t>, then the specialization of Foo may trigger the specialization of Bar (e.g., because Bar may be required to complete the specialization of code for Foo). A runtime environment, such as virtualized runtime environment 140, may utilize any of various types of information to identify dependency relationships, such as through use by a compiler of information about dependent types included in class files.
[0060] Now go to Figure 2 , Figure 2 is a logical block diagram illustrating an example embodiment of dependency-driven common specialization of specialized classes as described herein. Figure 2 As shown in , class file 200 may include generic class A 210 and generic class B 220 having a dependency relationship 230. For example, in one embodiment, class A may depend on class B. According to some embodiments, when dependency specializer 250 specializes class A to generate class A 260, such as upon instantiation of class A parameterized to a particular type, dependency specializer 250 may determine (e.g., identify) the dependency relationship between class A and class B and may therefore also specialize class B to generate class B 270. Thus, dependency relationship 230 between generic class A 210 and generic class B 220 may trigger specialization of class B whenever class A is specialized.
[0061] From a developer's perspective, co-specialization occurs automatically (eg, without any specific command from the developer) simply based on the dependency relationship between the different types (or classes).
[0062] Figure 3 3 is a flow chart illustrating one embodiment of a method for dependency-driven common specialization of specialized classes as described herein. As shown in block 300, the method for implementing dependency-driven common specialization may include specializing a first generic class defined in an object-oriented programming language in a platform-independent object-oriented runtime environment. The first generic class may include one or more specializable type variables and each of the one or more type variables may be specializable via one or more type parameterizations. In addition, the first generic class may be specialized for a specific one type parameterization.
[0063] For example, as described above, the generic class A 210 defined in the class file 200 can be instantiated for a specific type parameterization. Thus, the generic class can be specialized for the specific type parameterization in the virtual runtime environment 140 (e.g., in a platform-independent object-oriented runtime environment).
[0064] According to an example embodiment, based on the Java class ArrayList <t>Can be specialized for type int. Therefore, according to an example embodiment, the generic class ArrayList can be specialized for T=int in a Java-based virtual machine.
[0065] As shown in block 310, according to some embodiments, a dependency relationship may be determined between a first generic class and a second generic class. For example, a specializer of a runtime environment (such as specializer 170 and / or dependency specializer 250 of virtualized runtime environment 140) may be configured to determine a dependency relationship between two classes, such as Figure 2 2 . The dependency relationship 230 between generic class A 210 and generic class B 220 is shown in FIG.
[0066] According to various embodiments, a dependency between two classes may involve almost any type of use of type or class B in class A (e.g., through which it is created or triggered). For example, a dependency relationship may involve the use of a supertype, a field descriptor, a method descriptor, or the use of another generic type or class in a method body (or through which it is defined / created). As an example, the class ArrayList<T> <t>Can extend AbstractList <t>, whereby the class ArrayList is created (e.g., defined) <t>and class AbstractList <t>The dependency relationship between them.
[0067] Whenever a generic type parameter (for example, in the example above, <t>Dependency-driven co-specialization may be triggered (e.g., specialization may occur) whenever a generic type parameter appears as a supertype, as a target of a cast, as a type of a method parameter, or as a return type, or almost anywhere a type parameter may appear, according to some embodiments. Thus, if a particular generic class appears almost everywhere (e.g., in an API or implementation of a dependent class), then in order to be able to generate / specialize the dependent class, the particular generic class may also be generated / specialized (e.g., before, during, or after the generation / specialization of the dependent class), according to some embodiments.
[0068] As shown in block 320, according to one embodiment, in response to determining the dependency relationship, the second generic class may be specialized for a specific type parameterization based on the determined dependency relationship. Continuing with the above example, ArrayList <t>Specializations parameterized for specific types can trigger AbstractList <t>According to some embodiments, if the class ArrayList <t>is instantiated, and therefore specialized for T = int, then the class AbstractList <t>Can also be specialized for T = int.
[0069] In addition, the same dependency relationship between classes (or other types) can also be used to trigger specializations for different parameterizations. For example, specializing ArrayList for int <t>Can trigger AbstractList <t>A specialization for int. Subsequently, ArrayList <t>Can be specialized for long, which can trigger AbstractList <t>Specialization of long. In other words, according to some embodiments, once a dependency relationship between two generic classes is defined, the dependency relationship can trigger specialization of the dependent class whenever the first class is specialized (and no matter how many times).
[0070] Although described above using dependencies based on superclass relationships, in other embodiments, dependencies can be based on other relationships between classes. For example, if the helper class LinkedListNode <t>Nested in class LinkedList <t>In the body of the LinkedList <t>The instantiation of can trigger the common specialization of LinkedListNode.
[0071] Although described above as two separate and distinct specializations performed in a particular order, according to various embodiments, the specializations of the first and second generic classes may occur in any order and / or may overlap. For example, in one embodiment, specializer 250 may determine a dependency relationship between the first generic class and the second generic class and, in response, may specialize the second generic class before specializing the first generic class. In another embodiment, specializer 250 may begin specializing the first generic class, determine the dependency relationship, specialize the second generic class, and then complete the specialization of the first generic class.
[0072] Although the above Figure 2 and Figure 3 The example shows a single dependency relationship between two classes, but dependency-driven co-specialization can involve multiple (or even many) layers of dependencies (or dependency chains). In some embodiments, there can be many levels of dependencies in both depth and breadth. For example, Figure 4 is a logical block diagram illustrating dependency-driven common specialization involving multiple dependencies in a dependency chain according to one embodiment. Figure 4 As shown in , a class file may include multiple generic classes, such as generic class A 410, generic class B 420, generic class C 440, and generic class D 430. In addition, the class file may define dependency relationships between generic classes. For example, generic class A 410 may depend on generic class B 420 and generic class C 440, as shown by dependency relationships 425 and 435. In addition, generic class B 420 itself may depend on generic class D 430, as indicated by dependency relationship 435.
[0073] Thus, when a specializer of a runtime environment configured to implement dependency-driven co-specialization (such as dependency specializer 450 of virtualized runtime environment 140) specializes generic class A 410 to generate class A 460, it can determine (or identify) dependency relationships between generic class A 410 and generic class B 420 and generic class C 440. In response to determining the dependency relationship (e.g., when specializing generic class A 410), dependency specializer 450 can specialize generic class B 420 and generic class C 440 to generate class B 470 and class C 480, respectively.
[0074] Furthermore, when specializing generic class B 420 , either independently (eg, due to independent instantiation of class B) or in response to specialization of generic class A 410 , dependency specializer 450 may specialize generic class D 430 to generate class D 490 .
[0075] As noted above, the dependency relationship between two classes can be defined based on various relationships between classes. For example, according to various embodiments, a dependency between two classes can exist because one class extends another class, or because its implementation references another class, or because its API names another class as one of its arguments or return types.
[0076] When a first generic class is specialized, in some embodiments, the specializer may "track" any dependency chains based on dependency relationships between the first generic class and any other generic classes and specialize the other classes accordingly.
[0077] Figure 5 is a logical block diagram illustrating a dependency-driven common specialization including multiple dependencies according to an example embodiment. Figure 5 As shown in , a class file may include multiple class definitions, such as classes 510, 512, 514, 516, and 518. One (or more) classes in a class file may depend on other classes. For example, class 510 may depend on classes 512, 514, 516, and 518. For example, the following example code shows a possible definition of class 510 that depends on other classes:
[0078]
[0079] Therefore, the class SortedList depends on multiple other classes, such as List, Comparator, Collection, and Set. Specifically, List is the superclass of SortedList, Comparator is named as a parameter of SortedList in the field signature, Collection is named in the method signature, and class Set is used in the body of the method. Therefore, SortedList depends on each of the other classes based on different types of dependency relationships.
[0080] When class 510 is specialized to generate class 530, according to an example embodiment, dependency specializer 520 may determine that class SortedList depends on other classes and, in response, may specialize classes 512, 514, 516, and 518 to generate classes 532, 534, 536, and 538, respectively. Specifically, according to one embodiment, specializing SortedList <int>Can trigger List <int>(superclass), Comparator <int>(field signature), Collection <int>(Method signature), Set <int>(method body)
[0081] Furthermore, the timing of the specialization may vary from embodiment to embodiment, such as depending on differences between different runtime environments. For example, in one embodiment, Set may not wait until the method countUnique is actually called during program execution. <int>Only then can it be specialized.
[0082] Although the above examples use public types, dependency-driven common specialization is not limited to public types. For example, the following sample code shows dependency-driven common specialization with non-public types:
[0083]
[0084] According to some embodiments, in the above example, although the class Node is private to LinkedList, it can be specialized whenever LinkedList is specialized.
[0085] Furthermore, the same generic class can be specialized multiple times for different type parameters as part of specializing a single dependent class. As a very simple example, the interface Map<K,V> This can be achieved by using two different ArrayLists, one for the keys and one for the values, as shown in the following sample code:
[0086]
[0087] In the above example, ArrayList is jointly specialized based on the type parameters of K and V.
[0088] Partial specialization of a specialized class
[0089] Generic classes can have more than one specializable type parameter, such as Map in the example.<K,V> It may be desirable to specialize one or more of the type variables without specializing the others (e.g., keeping the others generic). Figure 6 6 is a logic block diagram illustrating partial specialization of a generic class according to one embodiment. According to some embodiments, a specializer, such as a partial specializer 620 of a virtualized runtime environment 140, may be configured to partially specialize a generic class 610 to produce a partially specialized class 630, and may also be configured to subsequently further specialize the partially specialized class 630 to generate a fully specialized class 640.
[0090] For example, generic class 610 may represent a generic class Map<K,V> And the partial specializer 620 can be configured to specialize the generic class 610 into a map with generic keys but int values for the values, such as Map<K,int> As an alternative, the generic Map class<K,V> Can be specialized to have int values for keys but with generic values, such as in Map<int,V> This may be referred to herein as "partial specialization".
[0091] In some embodiments, the result of partial specialization may be one or more additional generic classes that are further specializable for the remaining type parameters. For example, partial specialization class 630 may represent Map<K,int> and can be further specialized to generate a fully specialized class 640 that can represent one or more mappings with a specific type for keys, such as Map<int,int> or Map<long,int> In other words, for a generic type with more than one type variable (for example, Map<K,V> ), where a type variable can be specialized (for example, Map<int,V> ) without specializing the other (one or more) type variables. The resulting class can be considered a partially specialized class that can be further specialized.
[0092] Therefore, instead of performing specializations of generic classes all at once (such as by passing a Map<K,V> Specialization into Map<int,int> or Map<long,int> ), but a type parameter can be partially specialized, such as producing a Map<K,int> , then at some later time, the remaining type parameter(s) can be specialized, such as to generate a Map<int,int> or Map<long,int> .
[0093] Although in Figure 6 200 , but in some embodiments, the partially specialized class 630 may be stored in a different location, such as in a separate class file or in memory in a virtualized runtime environment 140 .
[0094] Figure 7 700 is a flow chart illustrating one embodiment of a method for partial specialization of a generic class as described herein. As shown in block 700, a generic class may be partially specialized. According to one embodiment, a generic class may include multiple specializable type variables, each of which may be specializable via multiple type parameterizations.
[0095] As shown in blocks 710 and 720, partially specializing a generic class may involve specializing the generic class for a specific one of a plurality of type variables via a specific one of a plurality of type parameterizations, as shown in block 710, and not specializing the generic class for another specializable type variable, as shown in block 720, such that the partially specialized class is further specializable for each of the plurality of type parameterizations for at least another of the plurality of specializable type variables. For example,
[0096] Interface Map<any K,any V> {...}
[0097] It is possible to be partially specialized for one type variable (such as K) without specializing another type variable. For example, in the following example,
[0098] class IntBasedMap<any V> implements Map<int,V> {...}
[0099] IntBasedMap can be a partial specialization of Map. Only one type variable of Map (e.g., K) is specialized, thus allowing IntBasedMap (e.g., a partial specialization of Map<int,V> ) is further specialized for V via any or all of the type parameterizations that Map can be specialized for. In some embodiments, a partially specialized class can be further specialized for each of the multiple possible type parameterizations that the class can be specialized for. Thus, according to various embodiments, after being partially specialized for K=int as in the example above, a partially specialized Map (e.g., as an IntBasedMap) can be further specialized for V via other type parameterizations, such as for V=long, V=boolean, V=String, etc. Furthermore, as shown in the example above, a partially specialized class (such as Map<int,V> ) can look like a parameterized type (e.g., in an "extends" clause). In some embodiments, a parameterized type (e.g., Map<int,V> ) can be recognized as a generic class (for example, Map<K,V> ), and a generic class can be partially specialized to form another generic class (for example, Map<int,V> ), allowing parameterized types to be formed. Forming a parameterized type can often mean "instantiating" the generic class Map by binding type arguments to type parameters K and V.<K,V> , but in some embodiments, the identity function can be applied to a partially specialized generic class Map<K,V> , to form a parameterized type, similar to how type String is an identity function applied to the non-generic class String.
[0100] As shown in block 730, according to some embodiments, the partially specialized class may then be further specialized for another specializable type variable, such that the partially specialized class becomes fully specialized. Continuing with the above example, the partially specialized Map<int,V> (e.g., as an IntBasedMap) can be further specialized for the remaining type variables (e.g., V), as in the following example code:
[0101] class IntToIntMap extends IntBasedMap <int>{...}
[0102] Therefore, according to an example embodiment, Map<int,V> can be further specialized for V = int (as an example) to obtain Map<int,int> (e.g., via IntBasedMap and IntToIntMap). In addition, after being further specialized via one type parameterization, the same partially specialized class can be further specialized via other type parameterizations. For example, the same partially specialized Map class can be further specialized for V = long, as in the following example code:
[0103] class IntToLongMap extends IntBasedMap <long>{...}
[0104] In some embodiments, partial specialization of a class may be achieved by partially specializing metadata injected into the class (e.g., to make the class specializable), and then propagating the partially specialized metadata into a new, partially specialized class file so that the result of the partial specialization can be re-specialized. Thus, according to some embodiments, partial specialization may be achieved by forward propagating a projection of the specialization metadata from the original class into the result of the partial specialization.
[0105] This can be achieved by the way the developer writes his code - the way he wants to have different levels of partial specialization. As described in this article, there can be two different reasons to specialize a generic class. First, specializing a generic class can lead to better performance (for example, operations on ints are faster than operations on objects), and second, a generic class can be specialized to take advantage of specific information about the arguments (for example, such as adding new functionality that only works with certain type parameters).
[0106] Therefore, even if you don't know anything about the value (e.g., Map<int,V> ), it would also be beneficial to have a Map where the key is an int. This partially specialized Map class can then be specialized to use specific map values without having to specialize the entire class (for example, because Map<K,V> Partially specialized into Map<int,V> ).
[0107] Thus, according to some embodiments, partial specialization of a class may involve specialization of a class to produce a further specializable class. For example, the following is a partial specialization of HashMap:
[0108] class IntMap <k>extends HashMap<K,int>
[0109] Partial specialization of HashMap requires that metadata (described above) be propagated into the generated class so that the type variable K can be further specialized and so that the result of that further specialization can be recognized as a HashMap<K',int> subtype of .
[0110] In some embodiments, partially specialized classes may be explicitly declared, such as in the following example code:
[0111] class HashMap<int,V> implements Map<int,V>
[0112] Alternatively, multiple different partial specializations (e.g., Map<int,V> 、Map<long,V> etc.), which are themselves specializable as needed whenever a use of a particular type occurs. Thus, there can be multiple different partial specializations and they can appear in a class file anywhere a parameterized type based on a specializable generic class might appear (e.g., supertypes, local variables, field types, types of method parameters, type conversions, etc.).
[0113] Furthermore, partial specialization can be combined with the dependency-driven co-specialization described above. Figure 8 8 is a logical block diagram illustrating partial specialization with dependency-driven common specialization according to one embodiment. Generic class 810 may include multiple generic type variables and may be partially specialized by specializer 850 of virtualized runtime environment 140 to generate partially specialized class 820. In addition, generic class 810 may depend on generic class 840, as indicated by dependency relationship 830. As noted above, dependency relationships may involve the use of a supertype, a field descriptor, a method descriptor, or another generic type or class in a method body (or defined / created by it). For example, generic class 810 may represent a generic class Map<T>.<K,V> , which may utilize the generic MapNode class represented as generic class 840 as part of its implementation, as in the following example code:
[0114]
[0115]
[0116] When specializer 870 partially specializes generic class 810 to generate partially specialized class 820, specializer 870 may determine or identify a dependency relationship between generic class 810 and generic class 840, and may therefore (partially) specialize generic class 840 to generate partially specialized class 860. For example, Map<int> is partially specialized for K=int.<K,V> Can trigger MapNode<K,V> Partial specialization for K = int, as shown in the following example code:
[0117]
[0118] Subsequently, according to one embodiment, partially specialized class 820 may be further specialized to generate fully specialized class 880, which may also trigger further specialization of partially specialized class 860 to generate fully specialized class 890. For example, partially specialized Map<int,V> V = int can be further specialized, thereby triggering further specialization of MapNode(int, V) for V = int, as shown in the following sample code:
[0119]
[0120] As another example, the generic class HashMap<K,V> Map can be extended<K,V> According to some embodiments, such as partially specializing HashMap for K=int, may trigger (partial) specialization of Map for K=int.
[0121] As another example, Map<K,V> The method can take a mapping function from V to U and return a Map<K,U> (Its mapping can be (k,mapper(v)) for all (k,v) in the original map.) When Map is partially specialized, specialization of the mapper function can be triggered based on the dependency relationship between the mapping class and the mapper function, as shown in the following example code:
[0122]
[0123] Manual Refining of Specialized Classes
[0124] Although the runtime specializer may always be able to generate an automatically specialized version of a generic class, in some embodiments, an alternative form of user control over specialization may involve using automatic specialization while also adding (or overriding) specialization-specific method implementations. In general, according to some embodiments, the set of members of a generic class does not change when it is specialized—the same members exist in the automatically specialized version as in the generic version. However, manual refinement of a specialized class may allow a developer to manually specialize a better representation and / or implementation of one or more methods of the specialized class.
[0125] In other words, the generic class List can be generic for any T, and the automatically specialized version of List can include all the fields and methods of the generic version, but if List is specialized for T = int, a new version of the specific method can be included. Therefore, according to some embodiments, List is specialized for T = int, and the manually specialized version of the method can be used instead of the automatically specialized version.
[0126] As another example, methods parameterized for specific types can be added to ArrayList as follows:
[0127]
[0128] In some embodiments, some methods may make sense for some instantiations of a generic class but not for other instantiations. For example, if T is int or long, but not Shoe or some other object, then List<T> <t>The sum() method on List<T> may make sense. Some embodiments may include a declaration-place mechanism for conditional members on a given instantiation. This may also include members that may be inconsistent with each other if they both appear, such as for List<T> <int>The int carries the sum() method and for List <double>The double carries a sum() method. In addition, some embodiments may include a class file indicating that certain members are allowed to be ignored when the class is instantiated as an erased class, i.e., without any type parameterization.
[0129] Manual refinement of specialized classes to override functionality
[0130] According to some embodiments, manual refinement of specialized classes replaces (overrides) functionality that would otherwise be generated by the specializer. In a manner similar to the bulk replacement of automatically specialized classes as described above, methods may also be (manually) added to replace functionality, such as overriding automatically specialized members.
[0131] For some instantiations of a generic class, it may be preferable to replace the automatically specialized versions. Some embodiments may include a mechanism (such as a language mechanism) by which a single class file can both describe generic versions of class members and "override" the generic versions in one or more instantiation-specific layers. Thus, a class can include both generic versions of methods and alternative versions of methods for specific type specializations.
[0132] Fig. 9 is a logical block diagram illustrating manual refinement of a specialized class according to one embodiment. Fig. 9 As shown in , class file 200 may include class A 900, which includes an existing method 910 that can be automatically specialized when class A 900 is specialized. In addition, class A 900 may also include a refined method 920 that is used instead of the existing method 920 when class A 900 is specialized for a specific type parameter. When class A 900 is specialized, a refined specializer 930 may be configured to determine whether the specific type parameterization for which the class is being specialized is the type parameterization for which the refined method should be used. In other words, the specializer may determine that the refined method corresponds to the specific type parameterization for which the class is being specialized. If the refined specializer 930 determines that the refined method 920 should be used, the refined specializer 930 may load and specialize the class including the refined method, as shown by class A 940 and the refined method 950. Alternatively, if the class is being specialized for a different type parameterization that the refine method should not use, refine specializer 930 can load and specialize the class using an auto-specialized version of existing method 910 , as shown by class A 960 and auto-specialized method 970 .
[0133] Thus, according to some embodiments, user-controlled alternatives may be used to enhance specializations by parameterizing overriding specific methods (or adding new methods) for specific types. Thus, developers may be able to utilize the best possible implementation for some types while relying on generic (e.g., automatically specialized) versions for other types. For example, List<T> <int>There may be a sum() method, or an optimized version of an existing method may be manually written for a specific type parameterization. A user of the generic class does not have to know anything about manual refinements. For example, a user of the generic class does not have to request or specify the use of a manual refinement or an overridden version in order to take advantage of the manual refinement or overridden version. Instead, according to some embodiments, simply instantiating the class for a specific type parameter may trigger the use of a manual refinement (e.g., as provided by the developer of the generic class), without requiring any specific knowledge or request of the generic class by the user.
[0134] In one embodiment, a developer of a generic class can write multiple versions (e.g., of an overridden method) and the compiler can be configured to compile the code into multiple versions in a class file. The class file can also include information indicating which versions to use for which type specializations. In addition, the runtime environment can be configured to use the information in the class file to determine which version to use when loading a particular specialization.
[0135] As another example, the Box class can hold values, as shown in the following sample code:
[0136]
[0137] In the above example, the method toString can work for all types T, but perhaps to give a better answer, or due to a faster implementation, a more specialized version of toString() can provide an instantiation of Box for T = int. Furthermore, in some embodiments, an additional type constraint (T = int) can be used in the body of the specialized version of toString() to type check the body, such as to ensure that the compiler knows that the type of "t" is actually "int" and not the generic "T".
[0138] According to some embodiments, when overriding a particular method of a specialized generic class, an alternate version of the method may be included in the class file and may be indicated as applying to a particular set of specializations (e.g., for certain type parameters). In some embodiments, when a method of a generic class is specialized at runtime, if one of the particular specializations corresponding to the alternate version of the method is used, the alternate version of the method may be used (e.g., copied to the specialized class) instead of the automatically specialized version.
[0139] In some embodiments, the refine method can be used for multiple type parameterizations. For example, the refine method can be specific to short and char, as shown in the following example:
[0140] <where T extends Serializable> void writeObject(){...}
[0141] / / Only for short and char
[0142] <where T has 16bits> void swapBytes(){...}
[0143] Fig.10 1 is a flow chart illustrating one embodiment of a method for manual refinement of a specialized class as described herein. As shown in block 1000, a generic class can be specialized for a particular type parameterization. For example, at runtime, a class file can be loaded that includes a class declaration for a generic class that can be specialized over multiple type parameterizations. The class declaration can include a refinement method that is specific to a particular one of the multiple type parameterizations, and when the class is specialized for the particular type parameterization, alternative implementations can be provided for methods of the generic class.
[0144] For example, the specializer may be configured to load the Box class and specialize the Box class for T = int. As shown in block 1010, the specializer may determine a refined method that matches an existing method for the generic class and corresponds to a particular type parameterization. For example, the specializer may determine that a refined toString method that overrides the generic toString method and corresponds to the type parameterization T = int exists for the Box class.
[0145] As shown in block 1020, according to one embodiment, in response to determining that a refined method corresponds to a particular type parameterization, a specializer may include the refined method in a specialized version of a generic class, rather than including an automatically specialized version of an existing method. In other words, when a generic class is specialized for a particular type parameterization, a specializer may be configured to include the refined method in a specialized class based on the particular type parameterization. For example, a specializer may include a refined toString method in a specialized version of a Box class, rather than including a generic or automatically specialized version of the toString method. Thus, when the class is specialized for a particular type parameterization, the refined method may provide an alternative implementation for a method of the generic class.
[0146] In addition, as shown in block 1030, the generic class may be specialized for a different type parameterization, and, as shown in block 1040, the specializer may determine that the refine method does not correspond to the different type parameterization. For example, the Box class from the above example may be specialized for a type parameterization other than T=int, and thus the specializer may determine that the refine toString method does not correspond to the type parameterization. In response, the specializer may not include the refine method in the specialization for the other type parameterization, as shown in block 1050. For example, when the Box class is specialized for another type parameterization other than T=int, the specializer may not include the refine toString method (e.g., the toString method corresponding to T=int) in the specialized version of the class, but may include an automatically specialized version of the toString method. Alternatively, according to one embodiment, another refine toString method may exist for the other type parameterization (e.g., in the class declaration), and thus, when the class is specialized for the other type parameterization, it may be included in the specialized class.
[0147] When the developer has specific knowledge about the type, there may be a better (e.g., smaller, faster, more efficient) implementation than the automatic version. For example, if a program uses the parameterized type ArrayList <boolean>, then ArrayList <t>Automatically specialized versions of the class can utilize a default element size that is larger than the minimum required to store Boolean values. For example, in the Java programming language, by default, Booleans can be stored using 8 bits (or larger). Therefore, in some embodiments, when ArrayList<String, String> is used, <t>When specialized for T = Boolean, developers can provide their own implementation that uses a different storage mechanism (for example, using an internal array of longs and performing bit-by-bit calculations).
[0148] Therefore, there can be a generic ArrayList <t>, however, if it is instantiated for T=Boolean, a different handwritten class may be used instead of the automatically specialized version. In some embodiments, the automatically specialized version and the handwritten version may be required to have the same signature, but may have completely different representations and / or completely different members, etc.
[0149] In addition, the automatically specialized version of a generic class may be only partially rewritten with different methods of different specializations. Thus, rather than replacing the entire class and / or implementation, individual methods that exist only in a particular instantiation may be replaced (e.g., to use a better implementation) or individual methods may be added (as noted above). Thus, according to some embodiments, a developer may provide a handwritten refinement that provides an entirely new representation of a class, individual replaced (e.g., overridden) methods, and / or additional methods.
[0150] For example, although List <int>It is possible to have automatic specialized implementations of add and get, but for certain type parameterizations (e.g., T=int) it would be beneficial to include a sum method that may not be a member of all specialized versions of List (e.g., for all type parameterizations of T).
[0151] For example:
[0152]
[0153] Manual refinement to provide new methods
[0154] In some embodiments, the compiler and / or runtime may allow methods to be manually added to a class for certain (e.g., one, some, or all) specializations of a class, such as to add new functionality to the specialized class that is not present in the automatically specialized version.
[0155] Fig.11 is a block diagram illustrating manual refinement for adding new methods to a specialized class according to one embodiment. Fig.11 As shown in , a class file may include class A 1100, which includes both (one or more) existing methods 1110 and new methods 1120 corresponding only to specific type parameterizations. When class A 1100 is specialized for the type parameterization corresponding to new method 1120, refiner 1130 may include the new method, as well as specialized versions of (one or more) existing methods in the specialized version of the class, as shown by new method 1150 and (one or more) existing method 1160 in class A 1140.
[0156] However, when a class is specialized for a type parameterization that does not correspond to a new method, the specializer may not include the new method in the specialized class, but may instead include only specialized versions of (one or more) existing methods, as shown by class A1170 which includes (one or more) existing methods 1180 but does not include a specialized version of the new method.
[0157] Thus, rather than simply replacing the body of a method based on the instantiation of a type variable, a new method may be added. According to some embodiments, the new method may be referred to as a "conditional" method because it may only be included in certain type specializations.
[0158] As another example:
[0159]
[0160] In the above example, two conditional sum() methods are added to List. One of the Sum() methods is instantiated with List <int>A member of , and the other is an instantiation of List <long>Members of List<?> or generic List<?> <t>Members of.
[0161] Fig.12 12 is a flow chart illustrating one embodiment of a method for adding new methods via manual refinement of specialized classes as described herein. As shown in block 1200, a generic class can be specialized for a particular type parameterization. For example, a specializer can be configured to load a class and specialize the class for a particular type parameterization. Using the example above, a specializer can be configured to load a List class and specialize the List class for T=int. As shown in block 1210, a specializer can determine a new method for a generic class that corresponds to a particular type parameterization. For example, a specializer can determine that for a Box class, there is a new sum() method that corresponds to a type parameterization T=int.
[0162] As shown in block 1220, according to one embodiment, in response to determining that a new method corresponds to a particular type parameterization, the specializer may include the new method in a specialized version of the generic class, as well as automatically specialized versions of any existing methods. For example, the specializer may include a sum() method in a specialized version of the List class, as well as generic or automatically specialized versions of add() and get() methods.
[0163] In addition, as shown in block 1230, the generic class may be specialized for another type parameterization, and, as shown in block 1240, the specializer may determine that the new method does not correspond to the other type parameterization. For example, the List class from the example above may be specialized for a type parameterization other than T=int, and thus the specializer may determine that the new sum() method does not correspond to the type parameterization. In response, the specializer may not include the new method in the specialization for the other type parameterization, as shown in block 1250. For example, when specializing the List class for a type parameterization other than T=int, the specializer may not include the sum() method (e.g., the method corresponding to T=int) in the specialized version of the class, but may only include the automatically specialized versions of the add() and get() methods.
[0164] In some embodiments, a refine method may be used for more than one type parameterization. For example, a generic class may include a refine method for T=Object. In some embodiments, if a generic class is specialized for T=String (i.e., String is an object), a refine method for T=Object may be included in the specialized version of the generic class. However, in other embodiments, if the same generic class is specialized for T=String, a refine method (e.g., for T=Object) may not be included in the specialized version. Thus, in some embodiments, a particular refine method may be used only when the generic class is specialized for the exact type parameterization for which it is specified, while in other embodiments, a refine method may be used for multiple type parameterizations.
[0165] Batch replacement of specialized classes
[0166] In addition, in some embodiments, neither automatic specialization of a generic class nor manual refinement of the class may be used, but instead a completely new, handwritten class may be used. In general, a replacement class may have the same interface as a generic or automatically specialized version, but it may have a completely different representation and / or implementation. Therefore, according to some embodiments, a replacement class may be required to have (at least) all of the same methods that a generic or automatically specialized version of a class would have (e.g., so that the replacement version of the class complies with the Liskov Substitution Principle). At runtime, the runtime environment may load the replacement version of the class whenever a particular specialization is instantiated.
[0167] Fig.13 is a logic block diagram illustrating manual refinement for batch replacement of generic classes according to one embodiment. Fig.13 As shown in , class file 200 may include generic class 1300 and replacement class 1310. Replacement class 1310 may correspond to specific type parameterizations and may be expected to replace generic class 1300 when generic class 1300 is specialized for these specific type parameterizations. For example, when generic class 1300 is specialized for type parameterization A 1350 corresponding to replacement class 1310, refine specializer 1320 may load specialized replacement class 1330 instead of the auto-specialized version of generic class 1300. Alternatively, when generic class 1300 is specialized for a different type parameterization (such as type parameterization B 1360) that replacement class 1310 does not correspond to, specializer may load and specialize generic class 1300, as represented by auto-specialized class 1340.
[0168] In one embodiment, a generic or auto-specialized version of a class may include information identifying an alternative version of the class and indicating which type parameterizations may trigger use of the alternative version. However, in other embodiments, the information identifying the alternative version of the class and indicating which type parameterizations may trigger use of the alternative version may be stored somewhere other than the generic or auto-specialized version of the class (e.g., in a class file or in an information registry), and thus the runtime may not have to load the generic or auto-specialized version of the class when using an alternative version of the class.
[0169] For example, you can provide an alternative custom representation for a given instantiation of a generic class, as in the following example:
[0170]
[0171]
[0172] Similarly, we might have a specialization for a hash table whose keys and values are ints:
[0173]
[0174] In accordance with some embodiments, when implementing bulk replacement of classes, the runtime class loader and / or specializer may be configured to determine that a replacement class should be used (e.g., based on the specific type parameters being instantiated) and locate and load the replacement class to replace the automatically specialized and / or manually refined versions of the generic class.
[0175] Fig.14 14 is a flowchart showing an embodiment of a method for batch replacement of classes via manual refinement as described herein. As shown in block 1400, a generic class can be specialized for a specific type parameterization. For example, using the above example, a specializer can be configured to load an ArrayList class and specialize the ArrayList class for T=Boolean. According to some embodiments, the ArrayList class can be a generic class that is based on a generic class declaration and can be specialized via multiple type parameterizations. As shown in block 1410, a specializer can determine a replacement (e.g., refined) class for a generic class corresponding to a specific type parameterization. For example, a specializer can determine that a refined version of the ArrayList class exists and corresponds to the type parameterization T=boolean. In one embodiment, a class file for ArrayList may include a refined class declaration for ArrayList, which provides (e.g., includes) an alternative implementation used when a generic class is specialized for a specific type parameterization (e.g., T=boolean) for a generic ArrayList.
[0176] As shown in block 1420, according to one embodiment, in response to determining that the replacement class corresponds to a particular type parameterization, the specializer may load the replacement class instead of loading the automatically specialized version of the generic class. For example, the specializer may load an ArrayList<ArrayList<String, String> of an ArrayList<String, String> . <boolean>version, rather than the automatically specialized version of ArrayList.
[0177] Thus, in one embodiment, specializing a generic class may include loading a class corresponding to (e.g., based on) a refined class declaration of the generic class that is specific to a particular type parameterization and provides an alternative implementation for the generic class when the generic class is specialized for the particular type parameterization. Furthermore, the generic class may be specializable for other type parameterizations without loading the class corresponding to the refined class declaration.
[0178] For example, as shown in block 1430, a generic class may be specialized for another type parameterization, and, as shown in block 1440, the specializer may determine that a replacement class does not correspond to the other type parameterization. For example, the ArrayList class from the example above may be specialized for a type parameterization other than T=boolean, and thus the specializer may determine that a replacement class (e.g., ArrayList<T> ) does not correspond to the other type parameterization. <boolean>) does not correspond to the other type parameterization. In response, according to one embodiment, the specializer may not load the ArrayList of ArrayList <boolean>version, but the automatically specialized version of ArrayList can be loaded, as shown in square frame 1450.
[0179] As described above with respect to manual refinement of methods, in some embodiments, a refined class declaration may be used for more than one type parameterization. For example, a generic class may have a refined generic class declaration for T=Object. In some embodiments, if the generic class is specialized for T=String (i.e., String is an object), the refined class declaration for T=Object may be used for the specialized version of the generic class. However, in other embodiments, if the same generic class is specialized for T=String, the refined class declaration (e.g., for T=Object) may not be used for the specialized version of the class. Thus, in some embodiments, a particular refined class declaration may be used only when the generic class is specialized for the exact type parameterization for which the particular refined class declaration is specified, while in other embodiments, the refinement method may be used for multiple type parameterizations.
[0180] In some embodiments, manual refinement of specialized generic classes can also be combined with partial specialization of generic classes and dependency-driven co-specialization, as described above. For example, a partially specialized class can be manually refined to include some additional functionality for one or more specializations, and a partial specialization can also trigger the (full or partial) specialization of one or more other generic classes.
[0181] Manual refinement of generic methods
[0182] In addition to using manual refinement based on a specific instantiation of a class type parameter, as described above, manual refinement can also be provided based on a method type parameter of a generic method. In some embodiments, when a generic method in a class (e.g., the class itself need not be generic) is specialized for a particular type, an alternative version of the method may be used.
[0183]
[0184] Here, we say that this is a specialization of an existing generic method for a specific combination of type parameters. When developers write different versions of the method, the different versions can be propagated into the class file by the compiler, and, at runtime, the specific version of the method is loaded based on the specific type parameters that were specialized.
[0185] Fig.15 is a logical block diagram illustrating manual refinement for a generic method according to one embodiment. Fig.15 As shown in , class file 200 may include generic method 1500 and replacement method 1510. Replacement method 1510 may correspond to specific type parameterizations and may be expected to replace generic method 1500 when generic method 1500 is specialized for these specific type parameterizations. For example, when generic method 1500 is specialized for type parameterization A 1550 corresponding to replacement method 1510, refinement specializer 1520 may load specialized replacement method 1530 instead of the automatically specialized version of generic method 1500. Alternatively, when generic method 1500 is specialized for a different type parameterization (such as type parameterization B 1560) that replacement method 1310 does not correspond to, the specializer may load and specialize generic method 1500, as represented by automatic specialization method 1540.
[0186] Fig.16 16 is a flow chart illustrating one embodiment of a method for manual refinement of generic methods as described herein. As shown in block 1600, a generic method can be specialized for a particular type parameterization. For example, a specializer can be configured to load and specialize a method for a particular type parameterization. Using the example above, a specializer can be configured to load a singletonList method and specialize the singletonList method for T=int. As shown in block 1610, a specializer can determine an alternative method for a generic method that corresponds to a particular type parameterization. For example, a specializer can determine that an alternative version of the singletonList method exists and corresponds to the type parameterization T=int.
[0187] As shown in block 1620, according to one embodiment, in response to determining that an alternative method corresponds to a particular type parameterization, the specializer may load the alternative method instead of loading an automatically specialized version of the generic method. For example, the specializer may load a specialization of the singletonList method.<T=int> version, rather than the automatically specialized version of singletonList.
[0188] Additionally, as shown in block 1630, the generic method may be specialized for another type parameterization, and, as shown in block 1640, the specializer may determine that the replacement method does not correspond to the other type parameterization. For example, the singletonList method from the example above may be specialized for a type parameterization other than T=int, and thus the specializer may determine that the replacement method (e.g., specialization<T=int> In response, according to one embodiment, the specializer may not load the specialization of the singletonList method.<T=int> version, but instead the automatically specialized version of the singletonList method can be loaded, as shown in box 1650.
[0189] In at least some embodiments, a computer system implementing part or all of one or more of the techniques described herein may include a general-purpose computer system that includes or is configured to access one or more computer-readable media. Fig.17 Such a general purpose computing device 3000 suitable for implementing the methods, features, and enhancements described herein is shown. In the illustrated embodiment, the computing device 3000 includes one or more processors 3010 coupled to a system memory 3020 via an input / output (I / O) interface 3030. The computing device 3000 also includes a network interface 3040 coupled to the I / O interface 3030.
[0190] In various embodiments, computing device 3000 may be a uniprocessor system including one processor 3010 or a multiprocessor system including several processors 3010 (e.g., two, four, eight, or another suitable number). Processor 3010 may include any suitable processor capable of executing instructions. For example, in various embodiments, processor 3010 may be a general-purpose or embedded processor that implements any of a variety of instruction set architectures (ISAs), such as an x86, PowerPC, SPARC, or MIPS ISA, or any other suitable ISA. In a multiprocessor system, each processor 3010 may typically, but need not, implement the same ISA.
[0191] The system memory 3020 may be configured to store program instructions and data that may be accessed by the processor(s) 3010. In various embodiments, the system memory 3020 may be implemented using any suitable memory technology, such as static random access memory (SRAM), synchronous dynamic RAM (SDRAM), non-volatile / flash type memory, or any other type of memory. In the illustrated embodiment, program instructions and data that implement one or more desired functions, such as those methods, techniques, and data described above, are shown as being stored in the system memory 3020 as code (i.e., program instructions) 3025 and data 3026. For example, in one embodiment, the memory 3020 and the code 3025 and data 3026 may store program instructions and data for implementing the compiler 120 and / or virtualized runtime environment 140 described above.
[0192] In various embodiments, compiler 120 and / or virtualized runtime environment 140 (and / or any individual submodules thereof) may each be implemented in any of a variety of programming languages or methods. For example, in one embodiment, compiler 120 and / or virtualized runtime environment 140 may be written in any of C, C++, assembly, JAVA, or other general programming languages, while in another embodiment, one or more of them may be written in a different, more specialized programming language. Furthermore, in some embodiments, compiler 120 and / or virtualized runtime environment 140 (and / or its individual submodules) may not be implemented in the same programming language.
[0193] In one embodiment, the I / O interface 3030 may be configured to coordinate I / O traffic between the processor 3010, the system memory 3020, and any peripheral devices in the device, including the network interface 3040 or other peripheral interfaces. In some embodiments, the I / O interface 3030 may perform any necessary protocol, timing, or other data transformations to convert data signals from one component (e.g., the system memory 3020) into a format suitable for use by another component (e.g., the processor 3010). In some embodiments, the I / O interface 3030 may include support for devices attached through various types of peripheral buses, such as variations of the peripheral component interconnect (PCI) bus standard or the universal serial bus (USB) standard. Moreover, in some embodiments, some or all of the functionality of the I / O interface 3030, such as an interface to the system memory 3020, may be incorporated directly into the processor 3010.
[0194] Network interface 3040 may be configured to allow data to be exchanged between computing device 3000 and other devices 3060 attached to one or more networks 3050. In various embodiments, network interface 3040 may support communications via any suitable wired or wireless general purpose data network, such as, for example, an Ethernet type.
[0195] In some embodiments, the system memory 3020 may be configured as described above with respect to Figure 1-6 The storage is used to implement an embodiment of a computer-readable (e.g., computer-accessible) medium of program instructions and data of embodiments of the corresponding methods and apparatuses. However, in other embodiments, program instructions and / or data may be received, sent, or stored on different types of computer-readable media. In general, computer-readable media may include non-transitory storage media or memory media, such as magnetic or optical media, for example, a disk or DVD / CD coupled to the computing device 3000 via an I / O interface 3030. Non-transitory computer-readable storage media may also include any volatile or non-volatile media that may be included in some embodiments of the computing device 3000 as system memory 3020 or another type of memory, such as RAM (e.g., SDRAM, DDRSDRAM, RDRAM, SRAM, etc.), ROM, etc.
[0196] Additionally, computer-readable media may include transmission media or signals such as electrical, electromagnetic, or digital signals transmitted via a communication medium such as a network and / or a wireless link, such as may be implemented via the network interface 3040. Fig.17 Some or all of the multiple computing devices shown in the figure can be used to implement the functions described in various embodiments; for example, software components running on various different devices and servers can cooperate to provide functions. In some embodiments, in addition to or instead of being implemented using a general-purpose computer system, a portion of the described functions can also be implemented using a storage device, a network device, or a special-purpose computer system. As used herein, the term "computing device" refers to at least all of these types of devices and is not limited to these types of devices.
[0197] Various embodiments may also include receiving, sending or storing instructions and / or data implemented according to the foregoing description on a computer-readable medium. In general, a computer-readable medium may include a storage medium or memory medium, such as a magnetic or optical medium, such as a disk or DVD / CD-ROM, a volatile or non-volatile medium, such as a RAM (e.g., SDRAM, DDR, RDRAM, SRAM, etc.), a ROM, etc. In some embodiments, a computer-readable medium may also include a transmission medium or signal, such as an electrical, electromagnetic or digital signal, transmitted via a communication medium such as a network and / or a wireless link.
[0198] The various methods shown in the figures and described herein represent exemplary embodiments of the methods. The methods can be implemented in software, hardware, or a combination thereof. In the various methods, the order of the steps can be changed, and various elements can be added, reordered, combined, omitted, modified, etc. The various steps can be performed automatically (e.g., without being directly prompted by user input) and / or programmatically (e.g., according to program instructions).
[0199] Although various systems and methods have been described herein with reference to specific embodiments and in their context, it should be understood that these embodiments are illustrative and the scope of the present disclosure is not limited to these specific embodiments. Many variations, modifications, additions, and improvements are possible. For example, the blocks and logic units identified in the description are used to understand the described embodiments, and are not meant to limit the present disclosure. For example, in some embodiments, the actions, processes, methods, tasks, or functions described as being performed by the compiler 120 may be performed by a virtualized runtime environment 140, and vice versa. In addition, the functions may be separated or combined in blocks in various ways in various implementations of the systems and methods described herein or described using different terms.
[0200] These embodiments are intended to be illustrative and non-restrictive. Thus, multiple instances may be provided for components described herein as single instances. The boundaries between various components, operations, and data storage are arbitrary to some extent, and specific operations are shown in the context of specific illustrative configurations. Other allocations of functions are conceivable and may fall within the scope of subsequent examples. Finally, the structures and functions provided as discrete components in the exemplary configuration may be implemented as combined structures or components.
[0201] Although the embodiments have been described in detail above, numerous changes and modifications will become apparent once the above disclosure is fully appreciated. It is intended that the following claims be interpreted to encompass all such changes and modifications.< / boolean> < / boolean> < / boolean> < / t> < / long> < / int> < / int> < / t> < / t> < / t> < / boolean> < / int> < / double> < / int> < / t> < / k> < / long> < / int> < / int> < / int> < / int> < / int> < / int> < / int> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / t> < / int> < / int> < / int> < / int> < / t> < / t> < / t> < / t> < / t> < / int> < / int> < / t> < / t> < / int> < / t>
Claims
1. A method for specializing a generic class, comprising: Partially specializing, by the platform-independent object-oriented runtime environment implemented on one or more computing devices, a generic class within the platform-independent object-oriented runtime environment, wherein the generic class includes a plurality of specializable type variables, wherein each of the plurality of specializable type variables is specializable via a plurality of type parameterizations; as well as The partial specializations include: The platform-independent object-oriented runtime environment specializes the generic class for a specific type variable among the multiple specializable type variables via a specific type parameterization among the multiple type parameterizations, while not specializing the generic class for at least one other specializable type variable among the multiple specializable type variables, so that the partially specialized generic class is further specializable for the at least one other specializable type variable via each type parameterization among the multiple type parameterizations.
2. The method according to claim 1, further comprising: After the partial specialization, the partially specialized generic class is further specialized on the at least one other specializable type variable of the plurality of specializable type variables via another type parameterization of the plurality of type parameterizations, so that as a result of the further specialization, the partially specialized generic class becomes fully specialized.
3. The method of claim 1, wherein the partially specializing a generic class comprises: The generic class is specialized for particular type variables in the plurality of specializable type variables through a plurality of type parameterizations in the plurality of type parameterizations, and the generic class is specialized for at least one other specializable type variable in the plurality of specializable type variables without any type parameterization in the plurality of type parameterizations. 4 . The method of claim 1 , further comprising loading a class file comprising specialization metadata describing the plurality of specializable type variables.
5. The method according to claim 4, further comprising: Partially specializing the metadata so that the partially specialization metadata includes information describing that the partially specialization generic class is specialized for the specific type variable through the specific type parameterization; as well as The partial specialization metadata is stored in a class file corresponding to the partially specialized generic class.
6. The method according to claim 1, further comprising: Determining a dependency relationship between the generic class and another generic class; as well as In response to partially specializing the generic class, specializing the other generic class.
7. The method according to claim 1, further comprising: determining a refinement method of a generic class, the refinement method overriding another method of the generic class, wherein the refinement method corresponds to the specific type parameterization; as well as including the refined method in the partially specialized generic class; and The another method of the generic class is not included in the partially specialized generic class.
8. The method according to claim 1, further comprising: determining additional methods of the generic class that correspond to the specific type parameterization; as well as Include the additional method in the partially specialized generic class.
9. A computing device comprising a processor and a memory comprising program instructions, which, when executed on the processor, cause the processor to perform the corresponding steps of the method according to any one of claims 1 to 8.
10. One or more non-transitory computer-readable storage media storing program instructions, which, when executed on or across one or more processors, cause the processors to perform the corresponding steps of the method according to any one of claims 1-8.
11. A computer program product comprising program instructions, which, when executed by one or more processors, cause the one or more processors to perform corresponding steps of the method according to any one of claims 1 to 8.
12. A method for specializing a generic class, comprising: Executed by one or more computers: specializing, by a platform-independent object-oriented runtime environment implemented on one or more computing devices, a generic class for a particular type parameterization from a plurality of type parameterizations, wherein the generic class is based on a generic class declaration, wherein the generic class is specializable via the plurality of type parameterizations; The specializations include: loading, by a platform-independent object-oriented runtime environment, a class corresponding to a refined class declaration of the generic class declaration, wherein the refined class declaration is specific to the particular type parameterization, wherein the refined class declaration includes an alternative implementation of the generic class when the generic class is specialized for the particular type parameterization; and The generic class may be specialized for other type parameterizations in the plurality of type parameterizations without loading a class corresponding to the refined class declaration.
13. The method of claim 12, further comprising loading a class corresponding to the refined class declaration without loading a class corresponding to the generic class declaration.
14. The method of claim 12, further comprising loading, by a platform-independent object-oriented runtime environment, class files representing the generic class declaration and the refined class declaration.
15. The method of claim 14, wherein the class file includes information indicating that the refined class declaration corresponds to a specific specialization of the generic class declaration parameterized for the specific type. 16 . The method of claim 15 , further comprising generating, by a compiler implemented on the one or more computing devices, the class file based at least in part on source code including the generic class declaration and the refined class declaration.
17. The method of claim 12, wherein the refined method of the specialized generic class overrides an existing generic method of the generic class.
18. The method of claim 12, further comprising performing the specialization in response to a request to create an object based on the generic class parameterized for the specific type.
19. The method according to claim 12, further comprising: The refined class declaration is identified by a platform-independent object-oriented runtime environment based on information indicating that the refined class declaration corresponds to a specific specialization of the generic class declaration parameterized for the specific type, wherein the loading of the class corresponding to the refined class declaration is performed in response to the identification.
20. The method of claim 12, further comprising: specializing the generic class for different type parameterizations by a platform-independent object-oriented runtime environment, wherein a refinement method for specializing the generic class is not specific to the different type parameterizations; as well as The refined method is not included in the specialized generic class parameterized for the different types by the platform-independent object-oriented runtime environment.
21. A method as claimed in claim 20, wherein the class declaration of the generic class includes different methods specific to the different type parameterizations, and the method also includes the different methods being included in the specialized generic class for the different type parameterizations by a platform-independent object-oriented runtime environment.
22. A computing device comprising a processor and a memory comprising program instructions, which when executed on the processor cause the processor to perform the corresponding steps of the method according to any one of claims 12 to 21.
23. One or more non-transitory computer-readable storage media storing program instructions that, when executed on or across one or more processors, cause the processors to perform the corresponding steps of the method according to any one of claims 12-21.
24. A computer program product comprising program instructions, which, when executed by one or more processors, cause the one or more processors to perform the corresponding steps of the method according to any one of claims 12 to 21.
