Описание тега specialization
This tag can refer to specialization in C++ or GHC, a Haskell compiler.
C++
A powerful feature of C++'s templates is template specialization
. This allows alternative implementations to be provided based on certain characteristics of the parameterized type that is being instantiated.
Template specialization has two purposes:
- to allow certain forms of optimization
- to reduce code bloat.
For example, consider a sort()
template function. One of the primary activities that such a function does is to swap or exchange the values in two of the container's positions.
If the values are large (in terms of the number of bytes it takes to store each of them), then it is often quicker to first build a separate list of pointers to the objects, sort those pointers, and then build the final sorted sequence.
If the values are quite small however it is usually fastest to just swap the values in-place as needed.
Furthermore if the parameterized type is already of some pointer-type, then there is no need to build a separate pointer array.
Template specialization allows the template creator to write different implementations and to specify the characteristics that the parameterized type(s) must have for each implementation to be used.
GHC
GHC provides a mechanism to specialize the type of polymorphic functions. This has the effect of removing dictionary lookups, and thus can improve code performance.
GHC performs some specializations automatically, but also provides a SPECIALIZE
pragma that directs the compiler to specialize a function to the given type signature.