Skip to content
Snippets Groups Projects
Select Git revision
  • a3de776486396a4bf4f5233ce18143bd0fd81cac
  • master default protected
2 results

compile.c

Blame
    • Damien George's avatar
      a3de7764
      py/emitnative: Optimise and improve exception handling in native code. · a3de7764
      Damien George authored
      Prior to this patch, native code would use a full nlr_buf_t for each
      exception handler (try-except, try-finally, with).  For nested exception
      handlers this would use a lot of C stack and be rather inefficient.
      
      This patch changes how exceptions are handled in native code by setting up
      only a single nlr_buf_t context for the entire function, and then manages a
      state machine (using the PC) to work out which exception handler to run
      when an exception is raised by an nlr_jump.  This keeps the C stack usage
      at a constant level regardless of the depth of Python exception blocks.
      
      The patch also fixes an existing bug when local variables are written to
      within an exception handler, then their value was incorrectly restored if
      an exception was raised (since the nlr_jump would restore register values,
      back to the point of the nlr_push).
      
      And it also gets nested try-finally+with working with the viper emitter.
      
      Broadly speaking, efficiency of executing native code that doesn't use
      any exception blocks is unchanged, and emitted code size is only slightly
      increased for such function.  C stack usage of all native functions is
      either equal or less than before.  Emitted code size for native functions
      that use exception blocks is increased by roughly 10% (due in part to
      fixing of above-mentioned bugs).
      
      But, most importantly, this patch allows to implement more Python features
      in native code, like unwind jumps and yielding from within nested exception
      blocks.
      a3de7764
      History
      py/emitnative: Optimise and improve exception handling in native code.
      Damien George authored
      Prior to this patch, native code would use a full nlr_buf_t for each
      exception handler (try-except, try-finally, with).  For nested exception
      handlers this would use a lot of C stack and be rather inefficient.
      
      This patch changes how exceptions are handled in native code by setting up
      only a single nlr_buf_t context for the entire function, and then manages a
      state machine (using the PC) to work out which exception handler to run
      when an exception is raised by an nlr_jump.  This keeps the C stack usage
      at a constant level regardless of the depth of Python exception blocks.
      
      The patch also fixes an existing bug when local variables are written to
      within an exception handler, then their value was incorrectly restored if
      an exception was raised (since the nlr_jump would restore register values,
      back to the point of the nlr_push).
      
      And it also gets nested try-finally+with working with the viper emitter.
      
      Broadly speaking, efficiency of executing native code that doesn't use
      any exception blocks is unchanged, and emitted code size is only slightly
      increased for such function.  C stack usage of all native functions is
      either equal or less than before.  Emitted code size for native functions
      that use exception blocks is increased by roughly 10% (due in part to
      fixing of above-mentioned bugs).
      
      But, most importantly, this patch allows to implement more Python features
      in native code, like unwind jumps and yielding from within nested exception
      blocks.
    repl_basic.py.exp 85 B
    Micro Python v\.\+ version
    >>> # basic REPL tests
    >>> print(1)
    1
    >>> print(1)
    1
    >>>