Skip to content

Commit 032adc9

Browse files
authored
Only serialize modified settings. NFC (#23648)
This means that when we run the compiler or the preprocessor we don't serialize all setting to JSON, only setting that were are set to something other than the default.
1 parent d6013f0 commit 032adc9

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

tools/settings.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def default_setting(name, new_default):
144144

145145
class SettingsManager:
146146
attrs: Dict[str, Any] = {}
147+
defaults: Dict[str, tuple] = {}
147148
types: Dict[str, Any] = {}
148149
allowed_settings: Set[str] = set()
149150
legacy_settings: Dict[str, tuple] = {}
@@ -153,6 +154,7 @@ class SettingsManager:
153154
def __init__(self):
154155
self.attrs.clear()
155156
self.legacy_settings.clear()
157+
self.defaults.clear()
156158
self.alt_names.clear()
157159
self.internal_settings.clear()
158160
self.allowed_settings.clear()
@@ -177,8 +179,9 @@ def read_js_settings(filename, attrs):
177179
self.attrs.update(internal_attrs)
178180
self.infer_types()
179181

182+
strict_override = False
180183
if 'EMCC_STRICT' in os.environ:
181-
self.attrs['STRICT'] = int(os.environ.get('EMCC_STRICT'))
184+
strict_override = int(os.environ.get('EMCC_STRICT'))
182185

183186
# Special handling for LEGACY_SETTINGS. See src/setting.js for more
184187
# details
@@ -194,10 +197,16 @@ def read_js_settings(filename, attrs):
194197
self.legacy_settings[name] = (fixed_values, err)
195198
default_value = fixed_values[0]
196199
assert name not in self.attrs, 'legacy setting (%s) cannot also be a regular setting' % name
197-
if not self.attrs['STRICT']:
200+
if not strict_override:
198201
self.attrs[name] = default_value
199202

200203
self.internal_settings.update(internal_attrs.keys())
204+
# Stash a deep copy of all settings in self.defaults. This allows us to detect which settings
205+
# have local mods.
206+
self.defaults.update(copy.deepcopy(self.attrs))
207+
208+
if strict_override:
209+
self.attrs['STRICT'] = strict_override
201210

202211
def infer_types(self):
203212
for key, value in self.attrs.items():
@@ -207,11 +216,15 @@ def dict(self):
207216
return self.attrs
208217

209218
def external_dict(self, skip_keys={}): # noqa
210-
external_settings = {k: v for k, v in self.dict().items() if k not in INTERNAL_SETTINGS and k not in skip_keys}
211-
# Only the names of the legacy settings are used by the JS compiler
212-
# so we can reduce the size of serialized json by simplifying this
213-
# otherwise complex value.
214-
external_settings['LEGACY_SETTINGS'] = [l[0] for l in external_settings['LEGACY_SETTINGS']]
219+
external_settings = {}
220+
for key, value in self.dict().items():
221+
if value != self.defaults.get(key) and key not in INTERNAL_SETTINGS and key not in skip_keys:
222+
external_settings[key] = value # noqa: PERF403
223+
if not self.attrs['STRICT']:
224+
# When not running in strict mode we also externalize all leggacy settings
225+
# (Since the external tools do process LEGACY_SETTINGS themselves)
226+
for key in self.legacy_settings:
227+
external_settings[key] = self.attrs[key]
215228
return external_settings
216229

217230
def keys(self):

0 commit comments

Comments
 (0)