KEMBAR78
[mypyc] Don't explicitly assign `NULL` values in setup functions by dosisod · Pull Request #15379 · python/mypy · GitHub
Skip to content

Conversation

dosisod
Copy link
Contributor

@dosisod dosisod commented Jun 6, 2023

While investigating something unrelated I stumbled across the *_setup functions, and I noticed that they contain a lot of code that looks like this:

self->abc = NULL;
self->xyz = NULL;
// ...

Something told me that assigning NULL to a bunch of fields is propably not needed, and when looking at the docs for tp_alloc() I found this reference to allocfunc, the typedef for tp_alloc():

It should return a pointer to a block of memory of adequate length for the instance, suitably aligned, and initialized to zeros, ...

Emphasis is mine.

Basically I added a simple check that removes lines that assigns NULL values in setup functions. This removes about ~4100 lines from the C file when self-compiling.

While investigating something unrelated I stumbled across the `*_setup`
functions, and I noticed that they contain a lot of code that looks like this:

```c
self->abc = NULL;
self->xyz = NULL;
// ...
```

Something told me that assigning `NULL` to a bunch of fields is propably not
needed, and when looking at the docs for `tp_alloc()` I found this reference to
[`allocfunc`](https://docs.python.org/3/c-api/typeobj.html#c.allocfunc), the
typedef for `tp_alloc()`:

> It should return a pointer to a block of memory of adequate length for the
> instance, suitably aligned, **and initialized to zeros**, ...

Emphasis is mine.

Basically I added a simple check that removes lines similar to above which
assigns `NULL` values in setup functions. This removes about ~4100 lines from
the C file when self-compiling.
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Makes sense. Thanks!

@JukkaL JukkaL merged commit 9b9272a into python:master Jun 12, 2023
@dosisod dosisod deleted the cleanup-setup branch June 17, 2023 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants