Skip to content

Commit dbcbb3f

Browse files
authored
[mypyc] Use tabs instead of spaces in emitted C code (#14016)
By using tabs instead of spaces for indentation in the emitted C code we are able to reduce the file size by almost 9%: | File | Size | |------|------| | `build/__native_74cdc94b2b24dafac2a2.c` (spaces) | 86.5MB | | `build/__native_74cdc94b2b24dafac2a2.c` (tabs) | 79.6MB | For this particular file we save about 6.9MB, or 8.7%. I checked, and this has no effect on the compilation speed, which is to be expected. At the very least opening these auto generated files inside an editor will be faster, even if the compilation isn't any faster. I am interested in making mypy/mypyc faster, and this seemed like a low-hanging fruit that could be beneficial.
1 parent fa22a49 commit dbcbb3f

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

mypyc/codegen/emit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def __init__(
176176
# Low-level operations
177177

178178
def indent(self) -> None:
179-
self._indent += 4
179+
self._indent += 1
180180

181181
def dedent(self) -> None:
182-
self._indent -= 4
182+
self._indent -= 1
183183
assert self._indent >= 0
184184

185185
def label(self, label: BasicBlock) -> str:
@@ -194,7 +194,7 @@ def attr(self, name: str) -> str:
194194
def emit_line(self, line: str = "") -> None:
195195
if line.startswith("}"):
196196
self.dedent()
197-
self.fragments.append(self._indent * " " + line + "\n")
197+
self.fragments.append(self._indent * "\t" + line + "\n")
198198
if line.endswith("{"):
199199
self.indent()
200200

mypyc/test/test_emit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ def test_emit_line(self) -> None:
2828
emitter.emit_line("a {")
2929
emitter.emit_line("f();")
3030
emitter.emit_line("}")
31-
assert emitter.fragments == ["line;\n", "a {\n", " f();\n", "}\n"]
31+
assert emitter.fragments == ["line;\n", "a {\n", "\tf();\n", "}\n"]

mypyc/test/test_emitfunc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ def assert_emit(
833833

834834
op.accept(visitor)
835835
frags = declarations.fragments + emitter.fragments
836-
actual_lines = [line.strip(" ") for line in frags]
836+
actual_lines = [line.strip(" \t") for line in frags]
837837
assert all(line.endswith("\n") for line in actual_lines)
838838
actual_lines = [line.rstrip("\n") for line in actual_lines]
839839
if not expected.strip():
@@ -900,7 +900,7 @@ def test_simple(self) -> None:
900900
" return cpy_r_arg;\n",
901901
"}\n",
902902
],
903-
result,
903+
[line.replace("\t", 4 * " ") for line in result],
904904
msg="Generated code invalid",
905905
)
906906

@@ -927,6 +927,6 @@ def test_register(self) -> None:
927927
" CPy_Unreachable();\n",
928928
"}\n",
929929
],
930-
result,
930+
[line.replace("\t", 4 * " ") for line in result],
931931
msg="Generated code invalid",
932932
)

mypyc/test/test_emitwrapper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ def test_check_int(self) -> None:
5656
)
5757

5858
def assert_lines(self, expected: list[str], actual: list[str]) -> None:
59-
actual = [line.rstrip("\n") for line in actual]
59+
actual = [line.rstrip("\n").replace(4 * " ", "\t") for line in actual]
60+
expected = [line.replace(4 * " ", "\t") for line in expected]
6061
assert_string_arrays_equal(expected, actual, "Invalid output")

0 commit comments

Comments
 (0)