Skip to content

Commit

Permalink
[mypyc] Use tabs instead of spaces in emitted C code (#14016)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dosisod authored Nov 9, 2022
1 parent fa22a49 commit dbcbb3f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions mypyc/codegen/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ def __init__(
# Low-level operations

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

def dedent(self) -> None:
self._indent -= 4
self._indent -= 1
assert self._indent >= 0

def label(self, label: BasicBlock) -> str:
Expand All @@ -194,7 +194,7 @@ def attr(self, name: str) -> str:
def emit_line(self, line: str = "") -> None:
if line.startswith("}"):
self.dedent()
self.fragments.append(self._indent * " " + line + "\n")
self.fragments.append(self._indent * "\t" + line + "\n")
if line.endswith("{"):
self.indent()

Expand Down
2 changes: 1 addition & 1 deletion mypyc/test/test_emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def test_emit_line(self) -> None:
emitter.emit_line("a {")
emitter.emit_line("f();")
emitter.emit_line("}")
assert emitter.fragments == ["line;\n", "a {\n", " f();\n", "}\n"]
assert emitter.fragments == ["line;\n", "a {\n", "\tf();\n", "}\n"]
6 changes: 3 additions & 3 deletions mypyc/test/test_emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ def assert_emit(

op.accept(visitor)
frags = declarations.fragments + emitter.fragments
actual_lines = [line.strip(" ") for line in frags]
actual_lines = [line.strip(" \t") for line in frags]
assert all(line.endswith("\n") for line in actual_lines)
actual_lines = [line.rstrip("\n") for line in actual_lines]
if not expected.strip():
Expand Down Expand Up @@ -900,7 +900,7 @@ def test_simple(self) -> None:
" return cpy_r_arg;\n",
"}\n",
],
result,
[line.replace("\t", 4 * " ") for line in result],
msg="Generated code invalid",
)

Expand All @@ -927,6 +927,6 @@ def test_register(self) -> None:
" CPy_Unreachable();\n",
"}\n",
],
result,
[line.replace("\t", 4 * " ") for line in result],
msg="Generated code invalid",
)
3 changes: 2 additions & 1 deletion mypyc/test/test_emitwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ def test_check_int(self) -> None:
)

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

0 comments on commit dbcbb3f

Please sign in to comment.