Skip to content

Searching for inittab in create_builtin is needlessly exhaustive #140641

@itamaro

Description

@itamaro

Feature or enhancement

Proposal:

In create_builtin, the loop for searching a match in the inittab array is needlessly exhaustive:

cpython/Python/import.c

Lines 2357 to 2367 in 9d34623

struct _inittab *found = NULL;
for (struct _inittab *p = INITTAB; p->name != NULL; p++) {
if (_PyUnicode_EqualToASCIIString(info.name, p->name)) {
found = p;
}
}
if (found == NULL) {
// not found
mod = Py_NewRef(Py_None);
goto finally;
}

Specifically, once a match is found, there's no point to continue looping the entire array.

This is fairly minor and unnoticeable with a small number of inittab extensions, but can become noticeable with thousands of inittab extensions, and it's also a trivial optimization (just add break, PR incoming), so why not.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions