skip to main content
10.1145/1534530.1534550acmotherconferencesArticle/Chapter ViewAbstractPublication PagessystorConference Proceedingsconference-collections
research-article

The effect of unrolling and inlining for Python bytecode optimizations

Published: 04 May 2009 Publication History

Abstract

In this study, we consider bytecode optimizations for Python, a programming language which combines object-oriented concepts with features of scripting languages, such as dynamic dictionaries. Due to its design nature, Python is relatively slow compared to other languages. It operates through compiling the code into powerful bytecode instructions that are executed by an interpreter. Python's speed is limited due to its interpreter design, and thus there is a significant need to optimize the language. In this paper, we discuss one possible approach and limitations in optimizing Python based on bytecode transformations. In the first stage of the proposed optimizer, the bytecode is expanded using function inline and loop unrolling. The second stage of transformations simplifies the bytecode by applying a complete set of data-flow optimizations, including constant propagation, algebraic simplifications, dead code elimination, copy propagation, common sub expressions elimination, loop invariant code motion and strength reduction. While these optimizations are known and their implementation mechanism (data flow analysis) is well developed, they have not been successfully implemented in Python due to its dynamic features which prevent their use. In this work we attempt to understand the dynamic features of Python and how these features affect and limit the implementation of these optimizations. In particular, we consider the significant effects of first unrolling and then inlining on the ability to apply the remaining optimizations. The results of our experiments indicate that these optimizations can indeed be implemented and dramatically improve execution times.

References

[1]
The Yield instruction. http://www.python.org/dev/peps/pep-0255/.
[2]
Ole Agesen. Concrete type inference: delivering object-oriented applications. PhD thesis, Stanford, CA, USA, 1996.
[3]
John Aycock. Converting python virtual machine code to c. In 8th International Python Conference, 1999.
[4]
David F. Bacon, Susan L. Graham, and Oliver J. Sharp. Compiler transformations for high-performance computing. ACM Computing Surveys, 26(4):345--420, 1994.
[5]
Henry G. Baker. Inlining semantics for subroutines which are recursive. SIGPLAN Notices, 27(12):39--46, 1992.
[6]
P. P. Chang and W.-W. Hwu. Inline function expansion for compiling C programs. In PLDI '89: Proceedings of the ACM SIGPLAN 1989 Conference on Programming language design and implementation, pages 246--257, 1989.
[7]
Wesley J. Chun. Keeping up with python: the 2.2 release. Linux J., 2002(99):5, 2002.
[8]
M. Dufour. Shed skin: An optimizing python-to-c++ compiler. In Master Thesis, Delft University of Technology, 2006.
[9]
Gary A. Kildall. A unified approach to global program optimization. In POPL '73: Proceedings of the 1st annual ACM SIGACT-SIGPLAN symposium on Principles of programming languages, pages 194--206, New York, NY, USA, 1973. ACM Press.
[10]
Jens Knoop, Oliver Ruthing, and Bernhard Steffen. Partial dead code elimination. In SIGPLAN Conference on Programming Language Design and Implementation, pages 147--158, 1994.
[11]
S. Montanaro. A peephole optimizer for python. In 7th International Python Conference, 1998.
[12]
S. Muchnick. Advanced Compiler Design and Implementation. Morgan Kaufmann Publishers, 1997.
[13]
Armin Rigo. Representation-based just-in-time specialization and the psyco prototype for python. In PEPM '04: Proceedings of the 2004 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation, pages 15--26, 2004.
[14]
M. Salib. Starkiller. A static type inferencer and compiler for python. In In Proceedings of the EuroPython conference, 2004.

Cited By

View all
  • (2022)RegCPython: A Register-based Python Interpreter for Better PerformanceACM Transactions on Architecture and Code Optimization10.1145/356897320:1(1-25)Online publication date: 16-Dec-2022
  • (2022)On the Security of Python Virtual Machines: An Empirical Study2022 IEEE International Conference on Software Maintenance and Evolution (ICSME)10.1109/ICSME55016.2022.00028(223-234)Online publication date: Oct-2022
  • (2022)Quantifying the interpretation overhead of PythonScience of Computer Programming10.1016/j.scico.2021.102759215:COnline publication date: 1-Mar-2022

Index Terms

  1. The effect of unrolling and inlining for Python bytecode optimizations

    Recommendations

    Reviews

    R. Clayton

    Programs written in the Python scripting language are compiled into bytecode and executed by an interpreter. Runtime performance is influenced by bytecode quality. This paper examines how bytecode transformations can improve bytecode quality. The transformations preprocess bytecode, using function inlining and loop unrolling to expose more code, and then apply dataflow optimizations, such as constant propagation and loop-invariant code motion. Python is dynamically typed, making the traditional compiler-based static type analysis difficult; in addition, static analysis runs counter to Python's interactive development style. This paper replaces static type analysis with filtering rules that dynamically identify code segments to which transformations can be safely applied. Loop unrolling is augmented with array index substitution, creating more opportunities for dataflow transformations after unrolling, such as replacing redundant array references with fast immediate variables. Experiments show that the transformations provide spectacular improvements (100 to 1,000 percent) on benchmarks and microkernel code, and much more modest-but still good-improvements (20 to 30 percent) on difficult cryptographic code. This paper requires a modest knowledge of either Python or language optimization. The general theory behind the transformations is described in detail, but no implementation details are presented or cited. The prose is awkward, using clumsy passive-voice constructions and sentences. The bibliography serves the paper, if not the large and rather haphazard field of scripting language optimization. Online Computing Reviews Service

    Access critical reviews of Computing literature here

    Become a reviewer for Computing Reviews.

    Comments

    Information & Contributors

    Information

    Published In

    cover image ACM Other conferences
    SYSTOR '09: Proceedings of SYSTOR 2009: The Israeli Experimental Systems Conference
    May 2009
    191 pages
    ISBN:9781605586236
    DOI:10.1145/1534530
    Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

    Sponsors

    • Hebrew University of Jerusalem
    • Melanox Technologies
    • IBM: IBM

    Publisher

    Association for Computing Machinery

    New York, NY, United States

    Publication History

    Published: 04 May 2009

    Permissions

    Request permissions for this article.

    Check for updates

    Author Tags

    1. Python
    2. bytecode
    3. dynamic languages
    4. optimizations

    Qualifiers

    • Research-article

    Conference

    SYSTOR '09
    Sponsor:
    • IBM

    Acceptance Rates

    Overall Acceptance Rate 108 of 323 submissions, 33%

    Contributors

    Other Metrics

    Bibliometrics & Citations

    Bibliometrics

    Article Metrics

    • Downloads (Last 12 months)23
    • Downloads (Last 6 weeks)3
    Reflects downloads up to 04 Oct 2024

    Other Metrics

    Citations

    Cited By

    View all
    • (2022)RegCPython: A Register-based Python Interpreter for Better PerformanceACM Transactions on Architecture and Code Optimization10.1145/356897320:1(1-25)Online publication date: 16-Dec-2022
    • (2022)On the Security of Python Virtual Machines: An Empirical Study2022 IEEE International Conference on Software Maintenance and Evolution (ICSME)10.1109/ICSME55016.2022.00028(223-234)Online publication date: Oct-2022
    • (2022)Quantifying the interpretation overhead of PythonScience of Computer Programming10.1016/j.scico.2021.102759215:COnline publication date: 1-Mar-2022

    View Options

    Get Access

    Login options

    View options

    PDF

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader

    Media

    Figures

    Other

    Tables

    Share

    Share

    Share this Publication link

    Share on social media