Visitar URL original
bpo-46254: Better fitting type for iterating in the trace_init C function. by UriyaHarpeness · Pull Request #30394 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260624095335/https://github.com/python/cpython/pull/30394
Skip to content
/ cpython Public

bpo-46254: Better fitting type for iterating in the trace_init C function.#30394

Closed
UriyaHarpeness wants to merge 1 commit into
python:mainfrom
UriyaHarpeness:fix-issue-46254
Closed

bpo-46254: Better fitting type for iterating in the trace_init C function.#30394
UriyaHarpeness wants to merge 1 commit into
python:mainfrom
UriyaHarpeness:fix-issue-46254

Conversation

@UriyaHarpeness

@UriyaHarpeness UriyaHarpeness commented Jan 4, 2022 •

Copy link
Copy Markdown

@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@UriyaHarpeness

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the motivation for this change?

int is a more idiomatic type, and chances are it doesn't affect the compiled code either way.

@UriyaHarpeness

Copy link
Copy Markdown
Author

What is the motivation for this change?

int is a more idiomatic type, and chances are it doesn't affect the compiled code either way.

I want Python to be the best it can possibly be, meaning also using the most fitting types, I would want to open a following PR doing the same fitting for more place in the file Python/sysmodule.c, as well as other C files.
Why not optimize? The type is indeed a better fit...

@JelleZijlstra

@JelleZijlstra

Copy link
Copy Markdown
Member

Sorry, I'm not convinced. In a product as widely used as CPython, we shouldn't change anything unless it's a clear improvement, since there's always a risk that it will unexpectedly break something.

For this change, is it a common recommendation for C code (e.g., in widely used style guides) to avoid an int type if you only use values that fit in a smaller type? Does it actually affect the code output by the compiler?

@UriyaHarpeness

Copy link
Copy Markdown
Author

Sorry, I'm not convinced. In a product as widely used as CPython, we shouldn't change anything unless it's a clear improvement, since there's always a risk that it will unexpectedly break something.

For this change, is it a common recommendation for C code (e.g., in widely used style guides) to avoid an int type if you only use values that fit in a smaller type? Does it actually affect the code output by the compiler?

There is a slight change in the resulting binary (makes it a little bit smaller), it's not like the compiler optimizes the types in C.
And the reason for this change, although it is very minor (I have commented above that I would like to change more types to more fitting ones in general in a separate PR), it is not just about the impact.

Same reason that one would prefer holding a small number of unique values in a set and not in a list, although the change in performance would be unnoticeable, and the same reason why this in the first place was an int and not double or long long, although they would work perfectly fine.

The reason I want to change this code is because I stumbled on this C implementation while reading about Python tracing, and it was just so weird to me that although I'm not a Python master, I could see this wrong type very easily, and if there is a chance for me to make CPython a little bit better, I would do it.

@JelleZijlstra

@iritkatriel

Copy link
Copy Markdown
Member

There is a slight change in the resulting binary (makes it a little bit smaller),

Do you evidence/a reference for this?

@UriyaHarpeness

Copy link
Copy Markdown
Author

There is a slight change in the resulting binary (makes it a little bit smaller),

Do you evidence/a reference for this?

I have tried it, and it reduces the size of the function containing that type and loop, though it may not always change the final binary size because of size alignments.

@iritkatriel

@iritkatriel

Copy link
Copy Markdown
Member

There is a slight change in the resulting binary (makes it a little bit smaller),

Do you evidence/a reference for this?

I have tried it, and it reduces the size of the function containing that type and loop, though it may not always change the final binary size because of size alignments.

@iritkatriel

On which compiler/system?

We don’t commit changes without proper discussion, including sharing our evidence (in a reproducible way). The place for that is the issue on the bug tracker rather than the PR.

Many ‘obvious’ optimisations don’t work out in practice.

@UriyaHarpeness

Copy link
Copy Markdown
Author

Darwin20.6.0 Darwin Kernel Version 20.6.0: root:xnu-7195.141.8~1/RELEASE_X86_64 x86_64
gcc: Apple clang version 13.0.0

But the propose of the PR isn't binary changes, it's using a more correct type, doesn't matter if it adds or removes 4 bytes from the whole python executable.

@iritkatriel

@iritkatriel

Copy link
Copy Markdown
Member

Thank you for volunteering to improve python.

As I said in my previous message, we discuss changes on the bug tracker (more contentious or significant changes in the mailing lists). The discussion on the PR is for code review - discussing details of the implementation.

Cosmetic changes to the code are rarely approved when they don’t bring a demonstrable benefit. On this bases I am going to reject this change.

I would suggest that you try to contribute by resolving issues reported on the bug tracker. Begin with the ones labelled ‘easy’.

@UriyaHarpeness

Copy link
Copy Markdown
Author

I get that this PR isn't necessary to improve runtime or anything really critical, but I don't understand why cosmetic changes are rejected.

The code could be improved with this small PR...

Even if this PR is closed and will not be merged, I don't understand the reason to reject cosmetic changes.

@iritkatriel

@iritkatriel

Copy link
Copy Markdown
Member

Even if this PR is closed and will not be merged, I don't understand the reason to reject cosmetic changes.

There are two reasons:

  1. any change carries a risk of breaking something and that risk needs to come with some tangible benefit

  2. littering the commit logs with cosmetic changes makes it hard in the future to see the history behind changes to the code. This is something we do all the time when investigating bug reports.

@UriyaHarpeness

Copy link
Copy Markdown
Author

OK, thank you @iritkatriel, I hope to contribute in the future with something that would be merged.

@JelleZijlstra

Copy link
Copy Markdown
Member

I got curious and tried it on the https://godbolt.org/ compiler explorer. The assembly is slightly different with no compiler options, because the variable is on the stack and the compiler uses a differently sized mov to get it out depending on the type, but with -O the assembly is exactly the same because the variable just lives in a register.

Compilable code sample used:

Details
#include <stdint.h>

typedef struct PyObject {
    void *ob_type;
} PyObject;

extern PyObject *PyUnicode_InternFromString(const char *s);
extern const PyObject *whatstrings[];

int
trace_init(void)
{
    static const char * const whatnames[8] = {
        "call", "exception", "line", "return",
        "c_call", "c_exception", "c_return",
        "opcode"
    };
    PyObject *name;
    unsigned char i;
    for (i = 0; i < 8; ++i) {
        if (whatstrings[i] == (void *)0) {
            name = PyUnicode_InternFromString(whatnames[i]);
            if (name == (void *)0)
                return -1;
            whatstrings[i] = name;
        }
    }
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants