-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] Pre-allocate space in list comprehensions #10295
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
Conversation
Encounter segment fault locally when i want to import module compiled by this branch |
Can you share the example which is failing for you? I can see if I can spot the issue. |
Here is my local test file def f(x: str) -> str:
return x
def test() -> None:
source = ["a", "b", "c"]
a = list(f(x) for x in source)
print(a)
test() compile and run:
If I comment line 96 - 101 of |
Did you try to find the cause in gdb? |
Hmm we should probably document how to debug C-level crashes on different platforms. It's pretty easy on Linux but I'm actually not sure what's the best way to do it on macOS or Windows. I'll create an issue. |
I am using
Since it's a newly created list, obviously there is no element in it. Thus |
I was wondering whether this could lead to reading from uninitialized memory, but https://docs.python.org/3/c-api/list.html#c.PyList_New says the list is initialized to NULL. Changing this code to XDECREF would incur a small performance hit for the additional check. If that matters, you could write separate setitem functions for existing lists (can use DECREF) and new lists (don't need to do anything). |
@JelleZijlstra Thanks for your advice! I will add a new primitive later. Btw, should I check list size every time or just add items without checking? |
To be clear I'm not sure the performance hit is big enough to be worth the additional complexity. You could try some benchmarks, or maybe someone else reading here has a stronger intuition. If you created a new list and you're filling it up, you shouldn't need to do bounds checking. |
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.
I tried this with a microbenchmark that does lots of list comprehensions it was 50% faster, which is great!
Looks good overall. Left various minor comments.
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.
Thanks for the updates! Some more minor comments, but this should be almost ready to merge.
@JukkaL Thanks for detailed suggestions! There remains only one unsolved. It seems that |
Add a |
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.
Looks good now! I like how this gives a significant speedup to list comprehensions, which are very common operations.
Description
Closes mypyc/mypyc#473
tuple_creation_from_generator_helper
topreallocate_space_helper
new_tuple_with_length
. Now we need only to pass in length.Test Plan