Skip to content

Commit 6a0f26c

Browse files
committed
[fdiff.diff] add default parallel TTFont.saveXML execution
...on multi CPU systems, single CPU defaults to sequential execution
1 parent cb30ed4 commit 6a0f26c

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

lib/fdiff/diff.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import os
3+
from multiprocessing import Pool, cpu_count
34
import tempfile
45

56
from fontTools.ttLib import TTFont
@@ -13,8 +14,19 @@
1314
from fdiff.utils import get_file_modtime
1415

1516

17+
def _ttfont_save_xml(ttf, filepath, include_tables, exclude_tables):
18+
"""Writes TTX specification formatted XML to disk on filepath."""
19+
ttf.saveXML(filepath, tables=include_tables, skipTables=exclude_tables)
20+
return True
21+
22+
1623
def u_diff(
17-
filepath_a, filepath_b, context_lines=3, include_tables=None, exclude_tables=None
24+
filepath_a,
25+
filepath_b,
26+
context_lines=3,
27+
include_tables=None,
28+
exclude_tables=None,
29+
use_multiprocess=True,
1830
):
1931
"""Performs a unified diff on a TTX serialized data format dump of font binary data using
2032
a modified version of the Python standard libary difflib module.
@@ -105,20 +117,30 @@ def u_diff(
105117
fromdate = get_file_modtime(prepath)
106118
todate = get_file_modtime(postpath)
107119

108-
tt_left.saveXML(
109-
os.path.join(tmpdirname, "left.ttx"),
110-
tables=include_tables,
111-
skipTables=exclude_tables,
112-
)
113-
tt_right.saveXML(
114-
os.path.join(tmpdirname, "right.ttx"),
115-
tables=include_tables,
116-
skipTables=exclude_tables,
117-
)
120+
left_ttxpath = os.path.join(tmpdirname, "left.ttx")
121+
right_ttxpath = os.path.join(tmpdirname, "right.ttx")
122+
123+
if use_multiprocess and cpu_count() > 1:
124+
# Use parallel fontTools.ttLib.TTFont.saveXML dump
125+
# by default on multi CPU systems. This is a performance
126+
# optimization. Profiling demonstrates that this can reduce
127+
# execution time by up to 30% for some fonts
128+
mp_args_list = [
129+
(tt_left, left_ttxpath, include_tables, exclude_tables),
130+
(tt_right, right_ttxpath, include_tables, exclude_tables),
131+
]
132+
with Pool(processes=2) as pool:
133+
pool.starmap(_ttfont_save_xml, mp_args_list)
134+
else:
135+
# use sequential fontTools.ttLib.TTFont.saveXML dumps
136+
# when use_multiprocess is False or single CPU system
137+
# detected
138+
_ttfont_save_xml(tt_left, left_ttxpath, include_tables, exclude_tables)
139+
_ttfont_save_xml(tt_right, right_ttxpath, include_tables, exclude_tables)
118140

119-
with open(os.path.join(tmpdirname, "left.ttx")) as ff:
141+
with open(left_ttxpath) as ff:
120142
fromlines = ff.readlines()
121-
with open(os.path.join(tmpdirname, "right.ttx")) as tf:
143+
with open(right_ttxpath) as tf:
122144
tolines = tf.readlines()
123145

124146
return unified_diff(

0 commit comments

Comments
 (0)