skip to main content

Collapsible contracts: fixing a pathology of gradual typing

Published:24 October 2018Publication History
Skip Abstract Section

Abstract

The promise of gradual typing is that programmers should get the best of both worlds: the static guarantees of static types, and the dynamic flexibility of untyped programming. This is an enticing benefit, but one that, in practice, may carry significant costs. Significant enough, in fact, to threaten the very practicality of gradual typing; slowdowns as high as 120x are reported as arising from gradual typing.

If one examines these results closely, though, it becomes clear that the costs of gradual typing are not evenly distributed. Indeed, while mixing typed and untyped code almost invariably carries non-trivial costs, many truly deal-breaking slowdowns exhibit pathological performance. Unfortunately, the very presence of these pathological cases---and therefore the possibility of hitting them during development---makes gradual typing a risky proposition in any setting that even remotely cares about performance.

This work attacks one source of large overheads in these pathological cases: an accumulation of contract wrappers that perform redundant checks. The work introduces a novel strategy for contract checking---collapsible contracts---which eliminates this redundancy for function and vector contracts and drastically reduces the overhead of contract wrappers.

We implemented this checking strategy as part of the Racket contract system, which is used in the Typed Racket gradual typing system. Our experiments show that our strategy successfully brings a class of pathological cases in line with normal cases, while not introducing an undue overhead to any of the other cases. Our results also show that the performance of gradual typing in Racket remains prohibitive for many programs, but that collapsible contracts are one essential ingredient in reducing the cost of gradual typing.

References

  1. Esteban Allende, Oscar Callaú, Johan Fabry, Éric Tanter, and Marcus Denker. Gradual typing for Smalltalk. Science of Computer Programming 96(1), pp. 52–69, 2013. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Matthew Arnold, Stephen Fink, David Grove, Michael Hind, and Peter F. Sweeney. Adaptive optimization in the Jalapeño JVM. In Proc. ACM Conf. Object-Oriented Programming, Systems, Languages and Applications, pp. 47–65, 2000. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Spenser Bauman, Carl Friedrich Bolz-Tereick, Jeremy Siek, and Sam Tobin-Hochstadt. Sound Gradual Typing: Only Mostly Dead. Proceedings of the ACM on Programming Languages 1(OOPSLA), pp. 54:1–54:24, 2017. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Gavin Bierman, Martin Abadi, and Mads Torgersen. Understanding TypeScript. In Proc. European Conf. Object-Oriented Programming, pp. 257–281, 2014. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Carl Friedrich Bolz, Antonio Cuni, Maciej Fijalkowski, and Armin Rigo. Tracing the meta-level: PyPy’s tracing JIT compiler. In Proc. Wksp. on the Implementation, Compilation, Optimization of Object-Oriented Languages and Programming Systems, pp. 18–25, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Gilad Bracha and David Griswold. Strongtalk: Typechecking Smalltalk in a Production Environment. In Proc. ACM Conf. Object-Oriented Programming, Systems, Languages and Applications, pp. 215–230, 1993. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Avik Chaudhuri, Panagiotis Vekris, Sam Goldman, Marshall Roch, and Gabriel Levy. Fast and Precise Type Checking for JavaScript. Proceedings of the ACM on Programming Languages 1(OOPSLA), pp. 48:1–48:30, 2017. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Benjamin W. Chung, Paley Li, Francesco Zappa Nardelli, and Jan Vitek. KafKa: Gradual Typing for Objects. In Proc. European Conf. Object-Oriented Programming, pp. 12:1–12:23, 2018.Google ScholarGoogle Scholar
  9. Christos Dimoulas, Sam Tobin-Hochstadt, and Matthias Felleisen. Complete Monitors for Behavioral Contracts. In Proc. European Sym. on Programming, pp. 214–233, 2012. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Robert Bruce Findler and Matthias Felleisen. Contracts for Higher-Order Functions. In Proc. ACM Intl. Conf. Functional Programming, pp. 48–59, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Matthew Flatt and PLT. Reference: Racket. PLT Design Inc., PLT-TR-2010-1, 2010. https://racket-lang.org/tr1/Google ScholarGoogle Scholar
  12. Michael Greenberg. Space-Efficient Manifest Contracts. In Proc. ACM Sym. Principles of Programming Languages, pp. 181–194, 2015. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Michael Greenberg. Space-Efficient Latent Contracts. In Proc. Sym. Trends in Functional Programming, 2016.Google ScholarGoogle Scholar
  14. Ben Greenman and Matthias Felleisen. A Spectrum of Type Soundness and Performance. Proceedings of the ACM on Programming Languages 2(ICFP), pp. 71:1–71:32, 2018. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Ben Greenman and Zeina Migeed. On the Cost of Type-Tag Soundness. In Proc. Wksp. on Partial Evaluation and Program Manipulation, pp. 30–39, 2018. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Jessica Gronski, Kenneth Knowles, Aaron Tomb, Stephen N. Freund, and Cormac Flanagan. Sage: Hybrid Checking for Flexible Specifications. In Proc. Wksp. Scheme and Functional Programming, pp. 93–104, 2006.Google ScholarGoogle Scholar
  17. Fritz Henglein. Dynamic typing: syntax and proof theory. Science of Computer Programming 22(3), pp. 197–230, 1994. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. David Herman, Aaron Tomb, and Cormac Flanagan. Space-efficient Gradual Typing. Higher-Order and Symbolic Computation 23(2), pp. 167–189, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Andre Kuhlenschmidt, Deyaaeldeen Almahallawi, and Jeremy G. Siek. Efficient Gradual Typing. arXiv, 1802.06375v1, 2018.Google ScholarGoogle Scholar
  20. Jacob Matthews and Robert Bruce Findler. Operational Semantics for Multi-Language Programs. Trans. Programming Languages and Systems 31(3), pp. 12:1–12:44, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. David A. Moon. MACLISP Reference Manual. MIT, Revision 0, 1974.Google ScholarGoogle Scholar
  22. Fabian Muehlboeck and Ross Tate. Sound Gradual Typing is Nominally Alive and Well. Proceedings of the ACM on Programming Languages 1(OOPSLA), pp. 56:1–56:30, 2017. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Aseem Rastogi, Nikhil Swamy, Cédric Fournet, Gavin Bierman, and Panagiotis Vekris. Safe & Efficient Gradual Typing for TypeScript. In Proc. ACM Sym. Principles of Programming Languages, pp. 167–180, 2015. Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Jakob Rehof. Polymorphic Dynamic Typing: Aspects of proof theory and inference. DIKU, 95/9, 1995.Google ScholarGoogle Scholar
  25. Gregor Richards, Francesco Zappa Nardelli, and Jan Vitek. Concrete Types for TypeScript. In Proc. European Conf. Object-Oriented Programming, 2015.Google ScholarGoogle Scholar
  26. Richard Roberts, Michael Homer, Stefan Marr, and James Noble. Shallow Types for Insightful Programs: Grace is Optional, Performance is Not. arXiv:1807.00661v1, 2018.Google ScholarGoogle Scholar
  27. Jeremy Siek, Peter Thiemann, and Philip Wadler. Blame and coercion: Together again for the first time. In Proc. ACM Conf. Programming Language Design and Implementation, pp. 425–435, 2015. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Jeremy G. Siek and Walid Taha. Gradual Typing for Functional Languages. In Proc. Wksp. Scheme and Functional Programming, 2006.Google ScholarGoogle Scholar
  29. Guy L. Steele. Common Lisp the Language. 2nd edition. Digital Press, 1990. Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. T. Stephen Strickland, Sam Tobin-Hochstadt, Robert Bruce Findler, and Matthew Flatt. Chaperones and Impersonators: Run-time Support for Reasonable Interposition. In Proc. ACM Conf. Object-Oriented Programming, Systems, Languages and Applications, pp. 943–962, 2012. Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. Asumu Takikawa, Daniel Feltey, Earl Dean, Robert Bruce Findler, Matthew Flatt, Sam Tobin-Hochstadt, and Matthias Felleisen. Towards Practical Gradual Typing. In Proc. European Conf. Object-Oriented Programming, 2015.Google ScholarGoogle Scholar
  32. Asumu Takikawa, Daniel Feltey, Ben Greenman, Max New, Jan Vitek, and Matthias Felleisen. Is Sound Gradual Typing Dead? In Proc. ACM Sym. Principles of Programming Languages, pp. 456–468, 2016. Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. Sam Tobin-Hochstadt and Matthias Felleisen. Interlanguage Migration: from Scripts to Programs. In Proc. Dynamic Languages Symposium, pp. 964–974, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  34. Sam Tobin-Hochstadt, Matthias Felleisen, Robert Bruce Findler, Matthew Flatt, Ben Greenman, Andrew M. Kent, Vincent St-Amour, T. Stephen Strickland, and Asumu Takikawa. Migratory Typing: Ten Years Later. In Proc. Summit on Advances in Programming Languages, pp. 17:1–17:17, 2017.Google ScholarGoogle Scholar
  35. Michael M. Vitousek, Cameron Swords, and Jeremy G. Siek. Big Types in Little Runtime: Open-World Soundness and Collaborative Blame for Gradual Type Systems. In Proc. ACM Sym. Principles of Programming Languages, pp. 762–774, 2017. Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. Jack Williams, J. Garrett Morris, Philip Wadler, and Jakub Zalewski. Mixed Messages: Measuring Conformance and Non-Interference in TypeScript. In Proc. European Conf. Object-Oriented Programming, pp. 28:1–28:29, 2017.Google ScholarGoogle Scholar
  37. Tobias Wrigstad, Francesco Zappa Nardelli, Sylvain Lebresne, Johan Östlund, and Jan Vitek. Integrating Typed and Untyped Code in a Scripting Language. In Proc. ACM Sym. Principles of Programming Languages, pp. 377–388, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Collapsible contracts: fixing a pathology of gradual typing

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!