Optimisation of Generic Programs Through Inlining

  • José Pedro MagalhãesEmail author
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 8241)


It is known that datatype-generic programs often run slower than type-specific variants, and this factor can prevent adoption of generic programming altogether. There can be multiple reasons for the performance penalty, but often it is caused by conversions to and from representation types that do not get eliminated during compilation. However, it is also known that generic functions can be specialised to specific datatypes, removing any overhead from the use of generic programming. In this paper, we investigate compilation techniques to specialise generic functions and remove the performance overhead of generic programs in Haskell. We pick a representative generic programming library and look at the generated code for a number of example generic functions. After understanding the necessary compiler optimisations for producing efficient generic code, we benchmark the runtime of our generic functions against handwritten variants, and conclude that the overhead can indeed be removed automatically by the compiler.


Representation Type Generic Programming Conversion Function Core Language Core Code 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


  1. 1.
    Alimarine, A., Smetsers, S.: Optimizing generic functions. In: Kozen, D. (ed.) MPC 2004. LNCS, vol. 3125, pp. 16–31. Springer, Heidelberg (2004)Google Scholar
  2. 2.
    Alimarine, A., Smetsers, S.: Improved fusion for optimizing generics. In: Hermenegildo, M.V., Cabeza, D. (eds.) PADL 2004. LNCS, vol. 3350, pp. 203–218. Springer, Heidelberg (2005)Google Scholar
  3. 3.
    Chakravarty, M.M.T., Ditu, G.C., Leshchinskiy, R.: Instant generics: fast and easy. (2009)
  4. 4.
    Hinze, R., Jeuring, J., Löh, A.: Comparing approaches to generic programming in Haskell. In: Backhouse, R., Gibbons, J., Hinze, R., Jeuring, J. (eds.) SSDGP 2006. LNCS, vol. 4719, pp. 72–149. Springer, Heidelberg (2007)Google Scholar
  5. 5.
    Lämmel, R., Peyton Jones, S.: Scrap your boilerplate: a practical design pattern for generic programming. In: Proceedings of the 2003 ACM SIGPLAN International Workshop on Types in Languages Design and Implementation, pp. 26–37. ACM (2003). doi: 10.1145/604174.604179
  6. 6.
    Lämmel, R., Peyton Jones, S.: Scrap more boilerplate: reflection, zips, and generalised casts. In: Proceedings of the 9th ACM SIGPLAN International Conference on Functional Programming, pp. 244–255. ACM (2004). doi: 10.1145/1016850.1016883
  7. 7.
    Magalhães, J.P.: Less is more: generic programming theory and practice. Ph.D. thesis, Universiteit Utrecht (2012)Google Scholar
  8. 8.
    Magalhães, J.P., Holdermans, S., Jeuring, J., Löh, A.: Optimizing generics is easy! In: Proceedings of the 2010 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, pp. 33–42. ACM (2010). doi: 10.1145/1706356.1706366
  9. 9.
    Van Noort, T., Rodriguez Yakushev, A., Holdermans, S., Jeuring, J., Heeren, B.: A lightweight approach to datatype-generic rewriting. In: Proceedings of the ACM SIGPLAN Workshop on Generic Programming, pp. 13–24. ACM (2008). doi: 10.1145/1411318.1411321
  10. 10.
    Van Noort, T., Rodriguez Yakushev, A., Holdermans, S., Jeuring, J., Heeren, B., Magalhães, J.P.: A lightweight approach to datatype-generic rewriting. J. Funct. Program. 20(3–4), 375–413 (2010). doi: 10.1017/S0956796810000183 CrossRefzbMATHGoogle Scholar
  11. 11.
    Peyton Jones, S., Marlow, S.: Secrets of the Glasgow Haskell Compiler inliner. J. Funct. Program. 12(4&5), 393–433 (2002). doi: 10.1017/S0956796802004331 MathSciNetzbMATHGoogle Scholar
  12. 12.
    Peyton Jones, S., Santos, A.L.M.: A transformation-based optimiser for Haskell. Sci. Comput. Program. 32, 3–47 (1998). doi: 10.1016/S0167-6423(97)00029-4 CrossRefzbMATHGoogle Scholar
  13. 13.
    Jones, P., Tolmach, A., Hoare, T.: Playing by the rules: rewriting as a practical optimisation technique in GHC. In: Haskell Workshop 2001, pp. 203–233 (2001)Google Scholar
  14. 14.
    Rodriguez Yakushev, A., Jeuring, J., Jansson, P., Gerdes, A., Kiselyov, O., Oliveira, B.C.d.S.: Comparing libraries for generic programming in Haskell. In: Proceedings of the 1st ACM SIGPLAN Symposium on Haskell, pp. 111–122. ACM (2008). doi: 10.1145/1411286.1411301
  15. 15.
    Sands, D.: Improvement theory and its applications. In: Gordon, A.D., Pitts, A.M. (eds.) Higher Order Operational Techniques in Semantics, Publications of the Newton Institute, pp. 275–306. Cambridge University Press, Cambridge (1998)Google Scholar
  16. 16.
    Wadler, P.: Theorems for free! In: Proceedings of the 4th International Conference on Functional Programming Languages and Computer Architecture, pp. 347–359. ACM (1989). doi: 10.1145/99370.99404
  17. 17.
    Yorgey, B.A., Weirich, S., Cretin, J., Peyton Jones, S., Vytiniotis, D., Magalhães, J.P.: Giving Haskell a promotion. In: Proceedings of the 8th ACM SIGPLAN Workshop on Types in Language Design and Implementation, pp. 53–66. ACM (2012). doi: 10.1145/2103786.2103795

Copyright information

© Springer-Verlag Berlin Heidelberg 2013

Authors and Affiliations

  1. 1.Department of Computer ScienceUniversity of OxfordOxfordUK

Personalised recommendations