-
-
Notifications
You must be signed in to change notification settings - Fork 3k
More efficient (fixed-format) serialization #19668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ac77da1
Add PoC implementation
ilevkivskyi f3b37eb
Port ArgKind hack
ilevkivskyi e3d9d41
Some more experiments
ilevkivskyi 08bec72
Clean-up/optimize some case reads
ilevkivskyi ab6e6a6
Merge remote-tracking branch 'upstream/master' into ff-cache
ilevkivskyi bfafc83
Some more experiments
ilevkivskyi 1865600
Add support for ints
ilevkivskyi d6b3068
Move closer to the real deal
ilevkivskyi d1bc106
Repurpose lib-rt/setup.py
ilevkivskyi 8e7cdcb
Some touches
ilevkivskyi 418fd1f
More touches
ilevkivskyi 705ffe4
Merge remote-tracking branch 'upstream/master' into ff-cache
ilevkivskyi c9712ee
Remove string size limit; Fix Windows
ilevkivskyi 45d3ec4
Hide new cache flag for now
ilevkivskyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
from typing import TYPE_CHECKING, Final | ||
|
||
try: | ||
from native_internal import ( | ||
Buffer as Buffer, | ||
read_bool as read_bool, | ||
read_float as read_float, | ||
read_int as read_int, | ||
read_str as read_str, | ||
write_bool as write_bool, | ||
write_float as write_float, | ||
write_int as write_int, | ||
write_str as write_str, | ||
) | ||
except ImportError: | ||
# TODO: temporary, remove this after we publish mypy-native on PyPI. | ||
if not TYPE_CHECKING: | ||
|
||
class Buffer: | ||
def __init__(self, source: bytes = b"") -> None: | ||
raise NotImplementedError | ||
|
||
def getvalue(self) -> bytes: | ||
raise NotImplementedError | ||
|
||
def read_int(data: Buffer) -> int: | ||
raise NotImplementedError | ||
|
||
def write_int(data: Buffer, value: int) -> None: | ||
raise NotImplementedError | ||
|
||
def read_str(data: Buffer) -> str: | ||
raise NotImplementedError | ||
|
||
def write_str(data: Buffer, value: str) -> None: | ||
raise NotImplementedError | ||
|
||
def read_bool(data: Buffer) -> bool: | ||
raise NotImplementedError | ||
|
||
def write_bool(data: Buffer, value: bool) -> None: | ||
raise NotImplementedError | ||
|
||
def read_float(data: Buffer) -> float: | ||
raise NotImplementedError | ||
|
||
def write_float(data: Buffer, value: float) -> None: | ||
raise NotImplementedError | ||
|
||
|
||
LITERAL_INT: Final = 1 | ||
LITERAL_STR: Final = 2 | ||
LITERAL_BOOL: Final = 3 | ||
LITERAL_FLOAT: Final = 4 | ||
LITERAL_COMPLEX: Final = 5 | ||
LITERAL_NONE: Final = 6 | ||
|
||
|
||
def read_literal(data: Buffer, marker: int) -> int | str | bool | float: | ||
if marker == LITERAL_INT: | ||
return read_int(data) | ||
elif marker == LITERAL_STR: | ||
return read_str(data) | ||
elif marker == LITERAL_BOOL: | ||
return read_bool(data) | ||
elif marker == LITERAL_FLOAT: | ||
return read_float(data) | ||
assert False, f"Unknown literal marker {marker}" | ||
|
||
|
||
def write_literal(data: Buffer, value: int | str | bool | float | complex | None) -> None: | ||
if isinstance(value, bool): | ||
write_int(data, LITERAL_BOOL) | ||
write_bool(data, value) | ||
elif isinstance(value, int): | ||
write_int(data, LITERAL_INT) | ||
write_int(data, value) | ||
elif isinstance(value, str): | ||
write_int(data, LITERAL_STR) | ||
write_str(data, value) | ||
elif isinstance(value, float): | ||
write_int(data, LITERAL_FLOAT) | ||
write_float(data, value) | ||
elif isinstance(value, complex): | ||
write_int(data, LITERAL_COMPLEX) | ||
write_float(data, value.real) | ||
write_float(data, value.imag) | ||
else: | ||
write_int(data, LITERAL_NONE) | ||
|
||
|
||
def read_int_opt(data: Buffer) -> int | None: | ||
if read_bool(data): | ||
return read_int(data) | ||
return None | ||
|
||
|
||
def write_int_opt(data: Buffer, value: int | None) -> None: | ||
if value is not None: | ||
write_bool(data, True) | ||
write_int(data, value) | ||
else: | ||
write_bool(data, False) | ||
|
||
|
||
def read_str_opt(data: Buffer) -> str | None: | ||
if read_bool(data): | ||
return read_str(data) | ||
return None | ||
|
||
|
||
def write_str_opt(data: Buffer, value: str | None) -> None: | ||
if value is not None: | ||
write_bool(data, True) | ||
write_str(data, value) | ||
else: | ||
write_bool(data, False) | ||
|
||
|
||
def read_int_list(data: Buffer) -> list[int]: | ||
size = read_int(data) | ||
return [read_int(data) for _ in range(size)] | ||
|
||
|
||
def write_int_list(data: Buffer, value: list[int]) -> None: | ||
write_int(data, len(value)) | ||
for item in value: | ||
write_int(data, item) | ||
|
||
|
||
def read_str_list(data: Buffer) -> list[str]: | ||
size = read_int(data) | ||
return [read_str(data) for _ in range(size)] | ||
|
||
|
||
def write_str_list(data: Buffer, value: Sequence[str]) -> None: | ||
write_int(data, len(value)) | ||
for item in value: | ||
write_str(data, item) | ||
|
||
|
||
def read_str_opt_list(data: Buffer) -> list[str | None]: | ||
size = read_int(data) | ||
return [read_str_opt(data) for _ in range(size)] | ||
|
||
|
||
def write_str_opt_list(data: Buffer, value: list[str | None]) -> None: | ||
write_int(data, len(value)) | ||
for item in value: | ||
write_str_opt(data, item) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hide the flag from
--help
output (help=argparse.SUPPRESS
) until we've figured out how to distribute this properly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hoped we will do this before next mypy release, but sure, we can expose this easily when ready.