Skip to content

Commit e3d9d41

Browse files
committed
Some more experiments
1 parent f3b37eb commit e3d9d41

File tree

5 files changed

+72
-68
lines changed

5 files changed

+72
-68
lines changed

mypy/build.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import time
2727
import types
2828
from collections.abc import Iterator, Mapping, Sequence, Set as AbstractSet
29-
from io import BytesIO
3029
from typing import (
3130
TYPE_CHECKING,
3231
Any,
@@ -41,6 +40,7 @@
4140
from typing_extensions import TypeAlias as _TypeAlias
4241

4342
import mypy.semanal_main
43+
from mypy.cache import BytesIO
4444
from mypy.checker import TypeChecker
4545
from mypy.error_formatter import OUTPUT_CHOICES, ErrorFormatter
4646
from mypy.errors import CompileError, ErrorInfo, Errors, report_internal_error
@@ -3443,7 +3443,6 @@ def process_fresh_modules(graph: Graph, modules: list[str], manager: BuildManage
34433443
for id in modules:
34443444
graph[id].fix_cross_refs()
34453445
t2 = time.time()
3446-
# touch 22 again
34473446
manager.add_stats(process_fresh_time=t2 - t0, load_tree_time=t1 - t0)
34483447

34493448

mypy/cache.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
from __future__ import annotations
22

33
from collections.abc import Sequence
4-
from io import BytesIO
54
from typing import Final
65

7-
INT_SIZE: Final = 2
8-
LONG_INT_SIZE: Final = 10
9-
FLOAT_LEN: Final = 32
106

7+
class BytesIO:
8+
def __init__(self, buffer: bytes | None = None) -> None:
9+
if buffer is None:
10+
self.write_buffer: bytearray | None = bytearray()
11+
self.read_buffer = None
12+
else:
13+
self.read_buffer = buffer
14+
self.write_buffer = None
15+
self.pos = 0
1116

12-
def read_int(data: BytesIO) -> int:
13-
return int.from_bytes(data.read(INT_SIZE), "big", signed=True)
17+
def read(self, size: int) -> bytes:
18+
assert self.read_buffer is not None
19+
pos = self.pos
20+
self.pos += size
21+
return self.read_buffer[pos : self.pos]
1422

23+
def write(self, chunk: bytes) -> None:
24+
assert self.write_buffer is not None
25+
self.write_buffer += chunk
26+
27+
def getvalue(self) -> bytes:
28+
assert self.write_buffer is not None
29+
return bytes(self.write_buffer)
1530

16-
def write_int(data: BytesIO, value: int) -> None:
17-
data.write(value.to_bytes(INT_SIZE, "big", signed=True))
1831

32+
INT_LEN: Final = 10
33+
FLOAT_LEN: Final = 24
1934

20-
def read_long_int(data: BytesIO) -> int:
21-
return int.from_bytes(data.read(LONG_INT_SIZE), "big", signed=True)
35+
36+
def read_int(data: BytesIO) -> int:
37+
return int(data.read(INT_LEN).decode())
2238

2339

24-
def write_long_int(data: BytesIO, value: int) -> None:
25-
data.write(value.to_bytes(LONG_INT_SIZE, "big", signed=True))
40+
def write_int(data: BytesIO, value: int) -> None:
41+
str_val = str(value)
42+
str_val = " " * (INT_LEN - len(str_val)) + str_val
43+
data.write(str_val.encode())
2644

2745

2846
def read_str(data: BytesIO) -> str:
@@ -67,7 +85,7 @@ def write_float(data: BytesIO, value: float) -> None:
6785

6886
def read_literal(data: BytesIO, marker: int) -> int | str | bool | float:
6987
if marker == LITERAL_INT:
70-
return read_long_int(data)
88+
return read_int(data)
7189
elif marker == LITERAL_STR:
7290
return read_str(data)
7391
elif marker == LITERAL_BOOL:
@@ -83,7 +101,7 @@ def write_literal(data: BytesIO, value: int | str | bool | float | complex | Non
83101
write_bool(data, value)
84102
elif isinstance(value, int):
85103
write_int(data, LITERAL_INT)
86-
write_long_int(data, value)
104+
write_int(data, value)
87105
elif isinstance(value, str):
88106
write_int(data, LITERAL_STR)
89107
write_str(data, value)
@@ -98,40 +116,32 @@ def write_literal(data: BytesIO, value: int | str | bool | float | complex | Non
98116
write_int(data, LITERAL_NONE)
99117

100118

101-
OPT_NO: Final = 0
102-
OPT_YES: Final = 1
103-
104-
105119
def read_int_opt(data: BytesIO) -> int | None:
106-
marker = read_int(data)
107-
if marker == OPT_YES:
120+
if read_bool(data):
108121
return read_int(data)
109-
assert marker == OPT_NO
110122
return None
111123

112124

113125
def write_int_opt(data: BytesIO, value: int | None) -> None:
114126
if value is not None:
115-
write_int(data, OPT_YES)
127+
write_bool(data, True)
116128
write_int(data, value)
117129
else:
118-
write_int(data, OPT_NO)
130+
write_bool(data, False)
119131

120132

121133
def read_str_opt(data: BytesIO) -> str | None:
122-
marker = read_int(data)
123-
if marker == OPT_YES:
134+
if read_bool(data):
124135
return read_str(data)
125-
assert marker == OPT_NO
126136
return None
127137

128138

129139
def write_str_opt(data: BytesIO, value: str | None) -> None:
130140
if value is not None:
131-
write_int(data, OPT_YES)
141+
write_bool(data, True)
132142
write_str(data, value)
133143
else:
134-
write_int(data, OPT_NO)
144+
write_bool(data, False)
135145

136146

137147
def read_int_list(data: BytesIO) -> list[int]:

mypy/nodes.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from collections import defaultdict
99
from collections.abc import Iterator, Sequence
1010
from enum import Enum, unique
11-
from io import BytesIO
1211
from typing import TYPE_CHECKING, Any, Callable, Final, Optional, TypeVar, Union, cast
1312
from typing_extensions import TypeAlias as _TypeAlias, TypeGuard
1413

@@ -18,8 +17,7 @@
1817
from mypy.cache import (
1918
LITERAL_COMPLEX,
2019
LITERAL_NONE,
21-
OPT_NO,
22-
OPT_YES,
20+
BytesIO,
2321
read_bool,
2422
read_float,
2523
read_int,
@@ -720,9 +718,9 @@ def write(self, data: BytesIO) -> None:
720718
mypy.types.write_type_opt(data, self.type)
721719
write_str(data, self._fullname)
722720
if self.impl is None:
723-
write_int(data, OPT_NO)
721+
write_bool(data, False)
724722
else:
725-
write_int(data, OPT_YES)
723+
write_bool(data, True)
726724
self.impl.write(data)
727725
write_flags(data, self, FUNCBASE_FLAGS)
728726
write_str_opt(data, self.deprecated)
@@ -738,7 +736,7 @@ def read(cls, data: BytesIO) -> OverloadedFuncDef:
738736
assert isinstance(typ, mypy.types.ProperType)
739737
res.type = typ
740738
res._fullname = read_str(data)
741-
if read_int(data) == OPT_YES:
739+
if read_bool(data):
742740
res.impl = cast(OverloadPart, read_symbol(data))
743741
# set line for empty overload items, as not set in __init__
744742
if len(res.items) > 0:
@@ -1035,9 +1033,9 @@ def write(self, data: BytesIO) -> None:
10351033
write_int_list(data, [int(ak.value) for ak in self.arg_kinds])
10361034
write_int(data, self.abstract_status)
10371035
if self.dataclass_transform_spec is None:
1038-
write_int(data, OPT_NO)
1036+
write_bool(data, False)
10391037
else:
1040-
write_int(data, OPT_YES)
1038+
write_bool(data, True)
10411039
self.dataclass_transform_spec.write(data)
10421040
write_str_opt(data, self.deprecated)
10431041
write_str_opt(data, self.original_first_arg)
@@ -1056,7 +1054,7 @@ def read(cls, data: BytesIO) -> FuncDef:
10561054
ret.arg_names = read_str_opt_list(data)
10571055
ret.arg_kinds = [ARG_KINDS[ak] for ak in read_int_list(data)]
10581056
ret.abstract_status = read_int(data)
1059-
if read_int(data) == OPT_YES:
1057+
if read_bool(data):
10601058
ret.dataclass_transform_spec = DataclassTransformSpec.read(data)
10611059
ret.deprecated = read_str_opt(data)
10621060
ret.original_first_arg = read_str_opt(data)
@@ -3931,16 +3929,16 @@ def write(self, data: BytesIO) -> None:
39313929
write_flags(data, self, TypeInfo.FLAGS)
39323930
write_str(data, json.dumps(self.metadata))
39333931
if self.slots is None:
3934-
write_int(data, OPT_NO)
3932+
write_bool(data, False)
39353933
else:
3936-
write_int(data, OPT_YES)
3934+
write_bool(data, True)
39373935
write_str_list(data, sorted(self.slots))
39383936
write_str_list(data, self.deletable_attributes)
39393937
mypy.types.write_type_opt(data, self.self_type)
39403938
if self.dataclass_transform_spec is None:
3941-
write_int(data, OPT_NO)
3939+
write_bool(data, False)
39423940
else:
3943-
write_int(data, OPT_YES)
3941+
write_bool(data, True)
39443942
self.dataclass_transform_spec.write(data)
39453943
write_str_opt(data, self.deprecated)
39463944

@@ -3974,30 +3972,32 @@ def read(cls, data: BytesIO) -> TypeInfo:
39743972
# rechecked, it can tell that the mro has changed.
39753973
ti._mro_refs = read_str_list(data)
39763974
ti._promote = cast(list[mypy.types.ProperType], mypy.types.read_type_list(data))
3977-
if read_int(data) == OPT_YES:
3975+
if read_bool(data):
39783976
assert read_int(data) == mypy.types.INSTANCE
39793977
ti.alt_promote = mypy.types.Instance.read(data)
3980-
if read_int(data) == OPT_YES:
3978+
if read_bool(data):
39813979
assert read_int(data) == mypy.types.INSTANCE
39823980
ti.declared_metaclass = mypy.types.Instance.read(data)
3983-
if read_int(data) == OPT_YES:
3981+
if read_bool(data):
39843982
assert read_int(data) == mypy.types.INSTANCE
39853983
ti.metaclass_type = mypy.types.Instance.read(data)
3986-
if read_int(data) == OPT_YES:
3984+
if read_bool(data):
39873985
assert read_int(data) == mypy.types.TUPLE_TYPE
39883986
ti.tuple_type = mypy.types.TupleType.read(data)
3989-
if read_int(data) == OPT_YES:
3987+
if read_bool(data):
39903988
assert read_int(data) == mypy.types.TYPED_DICT_TYPE
39913989
ti.typeddict_type = mypy.types.TypedDictType.read(data)
39923990
read_flags(data, ti, TypeInfo.FLAGS)
3993-
ti.metadata = json.loads(read_str(data))
3994-
if read_int(data) == OPT_YES:
3991+
metadata = read_str(data)
3992+
if metadata != "{}":
3993+
ti.metadata = json.loads(metadata)
3994+
if read_bool(data):
39953995
ti.slots = set(read_str_list(data))
39963996
ti.deletable_attributes = read_str_list(data)
3997-
if read_int(data) == OPT_YES:
3997+
if read_bool(data):
39983998
assert read_int(data) == mypy.types.TYPE_VAR_TYPE
39993999
ti.self_type = mypy.types.TypeVarType.read(data)
4000-
if read_int(data) == OPT_YES:
4000+
if read_bool(data):
40014001
ti.dataclass_transform_spec = DataclassTransformSpec.read(data)
40024002
ti.deprecated = read_str_opt(data)
40034003
return ti

mypy/test/testcheck.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
import sys
88
import tempfile
9-
from io import BytesIO
9+
from mypy.cache import BytesIO
1010
from pathlib import Path
1111

1212
from mypy import build
@@ -265,8 +265,7 @@ def verify_cache(
265265
data = BytesIO()
266266
tree.write(data)
267267
new_data = BytesIO()
268-
data.seek(0)
269-
new_tree = MypyFile.read(data)
268+
new_tree = MypyFile.read(BytesIO(data.getvalue()))
270269
fixup_module(new_tree, manager.modules, False)
271270
new_tree.write(new_data)
272271
assert data.getvalue() == new_data.getvalue()

mypy/types.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sys
66
from abc import abstractmethod
77
from collections.abc import Iterable, Sequence
8-
from io import BytesIO
98
from typing import (
109
TYPE_CHECKING,
1110
Any,
@@ -23,8 +22,7 @@
2322
import mypy.nodes
2423
from mypy.bogus_type import Bogus
2524
from mypy.cache import (
26-
OPT_NO,
27-
OPT_YES,
25+
BytesIO,
2826
read_bool,
2927
read_int,
3028
read_int_list,
@@ -1359,7 +1357,7 @@ def write(self, data: BytesIO) -> None:
13591357

13601358
@classmethod
13611359
def read(cls, data: BytesIO) -> AnyType:
1362-
if read_int(data) == OPT_YES:
1360+
if read_bool(data):
13631361
assert read_int(data) == ANY_TYPE
13641362
source_any = AnyType.read(data)
13651363
else:
@@ -1719,20 +1717,20 @@ def write(self, data: BytesIO) -> None:
17191717
write_type_list(data, self.args)
17201718
write_type_opt(data, self.last_known_value)
17211719
if self.extra_attrs is None:
1722-
write_int(data, OPT_NO)
1720+
write_bool(data, False)
17231721
else:
1724-
write_int(data, OPT_YES)
1722+
write_bool(data, True)
17251723
self.extra_attrs.write(data)
17261724

17271725
@classmethod
17281726
def read(cls, data: BytesIO) -> Instance:
17291727
type_ref = read_str(data)
17301728
inst = Instance(NOT_READY, read_type_list(data))
17311729
inst.type_ref = type_ref
1732-
if read_int(data) == OPT_YES:
1730+
if read_bool(data):
17331731
assert read_int(data) == LITERAL_TYPE
17341732
inst.last_known_value = LiteralType.read(data)
1735-
if read_int(data) == OPT_YES:
1733+
if read_bool(data):
17361734
inst.extra_attrs = ExtraAttrs.read(data)
17371735
return inst
17381736

@@ -4178,19 +4176,17 @@ def read_type(data: BytesIO) -> Type:
41784176

41794177

41804178
def read_type_opt(data: BytesIO) -> Type | None:
4181-
marker = read_int(data)
4182-
if marker == OPT_YES:
4179+
if read_bool(data):
41834180
return read_type(data)
4184-
assert marker == OPT_NO
41854181
return None
41864182

41874183

41884184
def write_type_opt(data: BytesIO, value: Type | None) -> None:
41894185
if value is not None:
4190-
write_int(data, OPT_YES)
4186+
write_bool(data, True)
41914187
value.write(data)
41924188
else:
4193-
write_int(data, OPT_NO)
4189+
write_bool(data, False)
41944190

41954191

41964192
def read_type_list(data: BytesIO) -> list[Type]:

0 commit comments

Comments
 (0)