-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[mypyc] Use optimized implementation for builtins.sum #10268
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
Merged
97littleleaf11
merged 20 commits into
python:master
from
sinback:sinback/mypyc-796-sum-impl
Nov 10, 2021
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
da0166f
[mypyc] add test for builtins.sum
sinback 624b26e
[mypyc] start optimized implementation for builtins.sum
sinback 2d7424f
[mypyc] add test case for IR for builtins.sum over ints
sinback 2672bfe
[mypyc] extend specialized sum implementation
sinback 9d6dfac
[mypyc] support integer 'start' arg to sum()
sinback b340a55
[mypyc] extend coverage of test for sum()
sinback 8e09f5f
[mypyc] support all literal expressions for 'start' arg to sum()
sinback 8983938
[mypyc] simplify logic in sum implementation
sinback 0f36a74
[mypyc] update test for sum
sinback 9e774ae
[mypyc] more general / correct detection of sum retval storage type
sinback 1acb0e6
[mypyc] simplify/correct implementation of sum a lot
sinback 1244b35
[mypyc] add more tests for sum
sinback 9b9e804
[mypyc] cleaner initialization for sum total
sinback b509447
[mypyc] remove start= argument from sum tests
sinback 1824715
[mypyc] make sum tests actually run
sinback 6119d17
[mypyc] lint
sinback e0e2a68
[mypyc] correct typo
sinback 0945e54
Merge from master
97littleleaf11 b09bca0
Fix tests
97littleleaf11 a1c65cf
Minor refactor
97littleleaf11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -831,6 +831,51 @@ assert call_all(mixed_110) == 1 | |
assert call_any_nested([[1, 1, 1], [1, 1], []]) == 1 | ||
assert call_any_nested([[1, 1, 1], [0, 1], []]) == 0 | ||
|
||
[case testSum] | ||
[typing fixtures/typing-full.pyi] | ||
from typing import Any, List | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding |
||
|
||
def test_sum_of_numbers() -> None: | ||
assert sum(x for x in [1, 2, 3]) == 6 | ||
assert sum(x for x in [0.0, 1.2, 2]) == 6.2 | ||
assert sum(x for x in [1, 1j]) == 1 + 1j | ||
|
||
def test_sum_callables() -> None: | ||
assert sum((lambda x: x == 0)(x) for x in []) == 0 | ||
assert sum((lambda x: x == 0)(x) for x in [0]) == 1 | ||
assert sum((lambda x: x == 0)(x) for x in [0, 0, 0]) == 3 | ||
assert sum((lambda x: x == 0)(x) for x in [0, 1, 0]) == 2 | ||
assert sum((lambda x: x % 2 == 0)(x) for x in range(2**10)) == 2**9 | ||
|
||
def test_sum_comparisons() -> None: | ||
assert sum(x == 0 for x in []) == 0 | ||
assert sum(x == 0 for x in [0]) == 1 | ||
assert sum(x == 0 for x in [0, 0, 0]) == 3 | ||
assert sum(x == 0 for x in [0, 1, 0]) == 2 | ||
assert sum(x % 2 == 0 for x in range(2**10)) == 2**9 | ||
|
||
def test_sum_multi() -> None: | ||
assert sum(i + j == 0 for i, j in zip([0, 0, 0], [0, 1, 0])) == 2 | ||
|
||
def test_sum_misc() -> None: | ||
# misc cases we do optimize (note, according to sum's helptext, we don't need to support | ||
# non-numeric cases, but CPython and mypyc both do anyway) | ||
assert sum(c == 'd' for c in 'abcdd') == 2 | ||
# misc cases we do not optimize | ||
assert sum([0, 1]) == 1 | ||
assert sum([0, 1], 1) == 2 | ||
|
||
def test_sum_start_given() -> None: | ||
a = 1 | ||
assert sum((x == 0 for x in [0, 1]), a) == 2 | ||
assert sum(((lambda x: x == 0)(x) for x in []), 1) == 1 | ||
assert sum(((lambda x: x == 0)(x) for x in [0]), 1) == 2 | ||
assert sum(((lambda x: x == 0)(x) for x in [0, 0, 0]), 1) == 4 | ||
assert sum(((lambda x: x == 0)(x) for x in [0, 1, 0]), 1) == 3 | ||
assert sum(((lambda x: x % 2 == 0)(x) for x in range(2**10)), 1) == 2**9 + 1 | ||
assert sum((x for x in [1, 1j]), 2j) == 1 + 3j | ||
assert sum((c == 'd' for c in 'abcdd'), 1) == 3 | ||
|
||
[case testNoneStuff] | ||
from typing import Optional | ||
class A: | ||
|
@@ -845,7 +890,6 @@ def none() -> None: | |
def arg(x: Optional[A]) -> bool: | ||
return x is None | ||
|
||
|
||
[file driver.py] | ||
import native | ||
native.lol(native.A()) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this will need to take a start argument. It looks like that is the cause of a bunch of the test failures.