forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
py/objint: Try to convert big-int back to small-int after binary op.
Before this change, long/mpz ints propagated into all future calculations, even if their value could fit in a small-int object. With this change, the result of a big-int binary op will now be converted to a small-int object if the value fits in a small-int. For example, a relatively common operation like `x = a * b // c` where a,b,c all small ints would always result in a long/mpz int, even if it didn't need to, and then this would impact all future calculations with x. This adds +24 bytes on PYBV11 but avoids heap allocations and potential surprises (e.g. `big-big` is now a small `0`, and can safely be accessed with MP_OBJ_SMALL_INT_VALUE). Performance tests are unchanged on PYBV10, except for `bm_pidigits.py` which makes heavy use of big-ints and gains about 8% in speed. Unix coverage tests have been updated to cover mpz code that is now unreachable by normal Python code (removing the unreachable code would lead to some surprising gaps in the internal C functions and the functionality may be needed in the future, so it is kept because it has minimal overhead). This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]>
- Loading branch information
Showing
6 changed files
with
80 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
try: | ||
import micropython | ||
micropython.heap_lock | ||
except: | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
# All less than small int max. | ||
for d in (0, 27, 1<<29, -1861, -(1<<29)): | ||
i = 1<<70 | ||
print(i) | ||
j = (1<<70) + d | ||
print(j) | ||
# k should now be a small int. | ||
k = j - i | ||
print(k) | ||
|
||
# Now verify that working with k doesn't allocate (i.e. it's a small int). | ||
micropython.heap_lock() | ||
print(k + 20) | ||
print(k // 20) | ||
micropython.heap_unlock() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
1180591620717411303424 | ||
1180591620717411303424 | ||
0 | ||
20 | ||
0 | ||
1180591620717411303424 | ||
1180591620717411303451 | ||
27 | ||
47 | ||
1 | ||
1180591620717411303424 | ||
1180591620717948174336 | ||
536870912 | ||
536870932 | ||
26843545 | ||
1180591620717411303424 | ||
1180591620717411301563 | ||
-1861 | ||
-1841 | ||
-94 | ||
1180591620717411303424 | ||
1180591620716874432512 | ||
-536870912 | ||
-536870892 | ||
-26843546 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters