Skip to content

py/objset: Allow intersection() to take multiple sets as args. #15818

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 1 commit into
base: master
Choose a base branch
from

Conversation

AmirHmZz
Copy link
Contributor

@AmirHmZz AmirHmZz commented Sep 9, 2024

CPython allows set.intersection() to take multiple iterables as arguments and It seems CPython 2.6 implemented this mechanism. Micropython currently allows only one iterable as argument and this PR aims to increase compatibility with CPython.

Copy link

codecov bot commented Sep 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.43%. Comparing base (3ca01ec) to head (a71e394).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15818   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files         163      163           
  Lines       21294    21296    +2     
=======================================
+ Hits        20960    20962    +2     
  Misses        334      334           

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

Copy link

github-actions bot commented Sep 9, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   +40 +0.005% standard[incl +32(data)]
      stm32:   +16 +0.004% PYBV10
     mimxrt:    +8 +0.002% TEENSY40
        rp2:   +16 +0.002% RPI_PICO_W
       samd:    +8 +0.003% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@dpgeorge dpgeorge added py-core Relates to py/ directory in source board-definition New or updated board definition files. Combine with a port- label. labels Sep 10, 2024
@AmirHmZz
Copy link
Contributor Author

AmirHmZz commented Sep 11, 2024

I've modified the set_intersect_int() and avoided linear search too. Now this PR should not reduce the performance of the existing hash-based approach. I also tried to minimize code size as much as possible.

@AmirHmZz
Copy link
Contributor Author

I've also set minimum accepted arg count to 1 in order to make set.intersection() compatibe with CPython in that case.

@AmirHmZz AmirHmZz requested a review from projectgus September 12, 2024 06:05
Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

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

This looks good to me, the code size is similar to the current version and the temporary memory usage looks like it won't be significantly worse than calling the single argument version multiple times in a row.

@AmirHmZz
Copy link
Contributor Author

AmirHmZz commented Sep 26, 2024

If this case (having one arg) is the only problem, I can solve it easily without significant code size increase.

It seems a little bit more code is needed to avoid unnecessary copy when n_args == 2 than I expected, which may increase code size. So I think it's better to wait for @dpgeorge review.

@AmirHmZz AmirHmZz requested review from dpgeorge and dlech September 26, 2024 19:23
@dpgeorge dpgeorge removed the board-definition New or updated board definition files. Combine with a port- label. label Apr 8, 2025
@dpgeorge
Copy link
Member

It seems a little bit more code is needed to avoid unnecessary copy when n_args == 2 than I expected, which may increase code size

I think it's worth improving things so it doesn't need the unnecessary copy. The new implementation should match the existing implementation (in terms of memory usage) for update == false and n_args == 2, ie something like s.intersect({1, 2}).

And fixing that will actual improve the general case for update == false and n_args >= 2, because it won't need to copy the input set at all.

Please try to do that, and we'll see how much firmware size changes. It shouldn't be too bad.

@@ -1,7 +1,21 @@
s = {1, 2, 3, 4}
print(sorted(s))
print(sorted(s.intersection()))
print(sorted(s))
Copy link
Member

Choose a reason for hiding this comment

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

Needs an additional test to ensure that s.intersection() is a copy of s.

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.

4 participants