Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9b9272a

Browse files
authored
[mypyc] Don't explicitly assignNULL values in setup functions (#15379)
While investigating something unrelated I stumbled across the `*_setup`functions, and I noticed that they contain a lot of code that looks likethis:```cself->abc = NULL;self->xyz = NULL;// ...```Something told me that assigning `NULL` to a bunch of fields is propablynot needed, and when looking at the docs for `tp_alloc()` I found thisreference 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 forthe 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 Cfile when self-compiling.
1 parent4aa18ea commit9b9272a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

‎mypyc/codegen/emitclass.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,12 @@ def generate_setup_for_class(
578578

579579
forbaseinreversed(cl.base_mro):
580580
forattr,rtypeinbase.attributes.items():
581-
emitter.emit_line(rf"self->{emitter.attr(attr)} ={emitter.c_undefined_value(rtype)};")
581+
value=emitter.c_undefined_value(rtype)
582+
583+
# We don't need to set this field to NULL since tp_alloc() already
584+
# zero-initializes `self`.
585+
ifvalue!="NULL":
586+
emitter.emit_line(rf"self->{emitter.attr(attr)} ={value};")
582587

583588
# Initialize attributes to default values, if necessary
584589
ifdefaults_fnisnotNone:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp