Skip to content

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

Merged
dpgeorge merged 1 commit into
micropython:masterfrom
jimmo:slice-peek
Jul 15, 2025
Merged

dpgeorge merged 1 commit into
micropython:masterfrom
jimmo:slice-peek

Conversation

@jimmo

@jimmo jimmo commented Sep 26, 2023

Copy link
Copy Markdown
Member

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

github-actions Bot commented Sep 26, 2023 •

Copy link
Copy Markdown

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
Copy Markdown
Member

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

@jimmo

jimmo commented Sep 27, 2023

Copy link
Copy Markdown
Member Author

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

jimmo commented Sep 27, 2023

Copy link
Copy Markdown
Member Author

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

codecov Bot commented Sep 27, 2023 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.44%. Comparing base (aa2362d) to head (5e9189d).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12518   +/-   ##
=======================================
  Coverage   98.44%   98.44%           
=======================================
  Files         171      171           
  Lines       22192    22204   +12     
=======================================
+ Hits        21847    21859   +12     
  Misses        345      345           

☔ 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.

Comment thread py/vm.c Outdated
@dpgeorge

dpgeorge commented Jul 2, 2025

Copy link
Copy Markdown
Member

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

@dpgeorge

dpgeorge commented Jul 4, 2025

Copy link
Copy Markdown
Member

This is looking good now. It enables basic slices to work without allocating on the heap, and only increases C stack usage when that feature is used. I think it should be merged.

This commit adds a 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]>
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.

3 participants