|
1 | 1 | import asyncio
|
2 | 2 | import os
|
| 3 | +from multiprocessing import Pool, cpu_count |
3 | 4 | import tempfile
|
4 | 5 |
|
5 | 6 | from fontTools.ttLib import TTFont
|
|
13 | 14 | from fdiff.utils import get_file_modtime
|
14 | 15 |
|
15 | 16 |
|
| 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 | + |
16 | 23 | 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, |
18 | 30 | ):
|
19 | 31 | """Performs a unified diff on a TTX serialized data format dump of font binary data using
|
20 | 32 | a modified version of the Python standard libary difflib module.
|
@@ -105,20 +117,30 @@ def u_diff(
|
105 | 117 | fromdate = get_file_modtime(prepath)
|
106 | 118 | todate = get_file_modtime(postpath)
|
107 | 119 |
|
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) |
118 | 140 |
|
119 |
| - with open(os.path.join(tmpdirname, "left.ttx")) as ff: |
| 141 | + with open(left_ttxpath) as ff: |
120 | 142 | fromlines = ff.readlines()
|
121 |
| - with open(os.path.join(tmpdirname, "right.ttx")) as tf: |
| 143 | + with open(right_ttxpath) as tf: |
122 | 144 | tolines = tf.readlines()
|
123 | 145 |
|
124 | 146 | return unified_diff(
|
|
0 commit comments