Skip to content

py/vm.c: Avoid heap-allocating slices for built-ins. #12518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jimmo
Copy link
Member

@jimmo jimmo commented Sep 26, 2023

This is an alternative to #10160 and implements a different variation on @damz's idea. It's still worth considering making the compiler do this (via a new SLICE_AND_SUBSCR opcode), but this way also has the benefit of handing the is-instance-type check in a simple way.

Unlike #10160 though it only helps for bytecode and not the native emitter.


Fast path optimisation for when a BUILD_SLICE is immediately followed by a LOAD/STORE_SUBSCR for a native type to avoid needing to allocate the slice on the heap.

In some cases (e.g. a[1:3] = x) this can result in no allocations at all.

We can't do this for instance types because the get/set/delattr implementation may keep a reference to the slice.

This work was funded through GitHub Sponsors.

@github-actions
Copy link

github-actions bot commented Sep 26, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +240 +0.028% standard
      stm32:  +120 +0.031% PYBV10
     mimxrt:  +128 +0.034% TEENSY40
        rp2:  +136 +0.015% RPI_PICO_W
       samd:  +128 +0.048% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:  +142 +0.031% VIRT_RV32

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Sep 27, 2023
@dpgeorge
Copy link
Member

Not sure why, but this is failing on unix CI.

@jimmo
Copy link
Member Author

jimmo commented Sep 27, 2023

Not sure why, but this is failing on unix CI.

It's failing the "doesn't heap allocate" when using emit-native. Need to add an exclusion for that test when using emit-native.

@jimmo
Copy link
Member Author

jimmo commented Sep 27, 2023

It's failing the "doesn't heap allocate" when using emit-native. Need to add an exclusion for that test when using emit-native.

Done.

@codecov
Copy link

codecov bot commented Sep 27, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.57%. Comparing base (1b0cdc0) to head (5512c22).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12518   +/-   ##
=======================================
  Coverage   98.56%   98.57%           
=======================================
  Files         169      169           
  Lines       21946    21958   +12     
=======================================
+ Hits        21632    21644   +12     
  Misses        314      314           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

py/vm.c Outdated
// this for instance types because the get/set/delattr
// implementation may keep a reference to the slice.
byte op = *ip++;
mp_obj_slice_t slice = { .base = { .type = &mp_type_slice }, .start = start, .stop = stop, .step = step };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This possibly increases C stack usage of this function. Will need to quantify that.

Copy link
Member Author

@jimmo jimmo Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed... +24 bytes of stack.

I've added a commit to address this (using alloca). Slight code size increase though (was +120, now +152).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted this for now... too complicated to figure out how this works with MICROPY_ENABLE_PYSTACK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit to factor the new code into a "no inline" function. And tweaked the code a little wrt to variable usage.

It now has the same C stack usage as prior to this PR.

Fast path optimisation for when a BUILD_SLICE is immediately followed by a
LOAD/STORE_SUBSCR for a native type to avoid needing to allocate the slice
on the heap.

In some cases (e.g. `a[1:3] = x`) this can result in no allocations at all.

We can't do this for instance types because the get/set/delattr
implementation may keep a reference to the slice.

Adds more tests to the basic slice tests to ensure that a stack-allocated
slice never makes it to Python, and also a heapalloc test that verifies
(when using bytecode) that assigning to a slice is no-alloc.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <[email protected]>
@dpgeorge
Copy link
Member

dpgeorge commented Jul 2, 2025

I've rebased this on latest master, to see how it goes after ~2 years.

Gets VM C stack usage back to its original value.

Signed-off-by: Damien George <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants