Skip to content

Avoid setattr/getattr with fixed format cache#20826

Merged
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:explicit-flags
Feb 16, 2026
Merged

Avoid setattr/getattr with fixed format cache#20826
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:explicit-flags

Conversation

@ilevkivskyi
Copy link
Member

This is a small optimization, and something that bothered me for a while. I write bit packing in Python because we have mypyc and we should use it (it should write relatively efficient code for this). Implementation is really basic, I only allow at most 26 flags, which will hopefully be enough for few years. Btw Var has 21 flags, I am quite sure some of those can be cleaned up.

@github-actions

This comment has been minimized.

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, the new code should be significantly more efficient when compiled.

mypy/nodes.py Outdated
setattr(node, flag, True)
def read_flags(data: ReadBuffer, num_flags: int) -> list[bool]:
packed = read_int(data)
return [packed & 1 << i != 0 for i in range(num_flags)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add parenthesees, since the precedence is otherwise a bit hard to see.

mypy/nodes.py Outdated
setattr(node, flag, True)
def read_flags(data: ReadBuffer, num_flags: int) -> list[bool]:
packed = read_int(data)
return [packed & 1 << i != 0 for i in range(num_flags)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use explicit parentheses to make precedence more obvious?

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi merged commit 694f767 into python:master Feb 16, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the explicit-flags branch February 16, 2026 18:17
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