Efficient run-time dispatching in generic programming with minimal code bloat

  • Authors:
  • Lubomir Bourdev;Jaakko Järvi

  • Affiliations:
  • Adobe Systems Inc., 345 Park Avenue, San Jose, CA 95110, USA and EECS U.C. Berkeley, Berkeley, CA 94720, USA;Texas A&M University, TAMU 3112, College Station, TX 77843, USA

  • Venue:
  • Science of Computer Programming
  • Year:
  • 2011

Quantified Score

Hi-index 0.00

Visualization

Abstract

Generic programming with C++ templates results in efficient but inflexible code: efficient, because the exact types of inputs to generic functions are known at compile time; inflexible because they must be known at compile time. We show how to achieve run-time polymorphism without compromising performance by instantiating the generic algorithm with a comprehensive set of possible parameter types, and choosing the appropriate instantiation at run time. Applying this approach naively can result in excessive template bloat: a large number of template instantiations, many of which are identical at the assembly level. We show practical examples of this approach quickly approaching the limits of the compiler. Consequently, we combine this method of run-time polymorphism for generic programming, with a strategy for reducing the number of necessary template instantiations. We report on using our approach in GIL, Adobe's open source Generic Image Library. We observed a notable reduction, up to 70% at times, in executable sizes of our test programs. This was the case even with compilers that perform aggressive template hoisting at the compiler level, due to significantly smaller dispatching code. The framework draws from both the generic and generative programming paradigms, using static metaprogramming to fine tune the compilation of a generic library. Our test bed, GIL, is deployed in a real world industrial setting, where code size is often an important factor.