Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(580)

Side by Side Diff: third_party/WebKit/Source/build/scripts/make_computed_style_base.py

Issue 2844223002: Add heuristic to order non bit fields in ComputedStyleBase. (Closed)
Patch Set: Assert Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2016 The Chromium Authors. All rights reserved. 2 # Copyright 2016 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import math 6 import math
7 import sys 7 import sys
8 8
9 import json5_generator 9 import json5_generator
10 import template_expander 10 import template_expander
11 import make_style_builder 11 import make_style_builder
12 12
13 from name_utilities import ( 13 from name_utilities import (
14 enum_for_css_keyword, enum_type_name, enum_value_name, class_member_name, me thod_name, 14 enum_for_css_keyword, enum_type_name, enum_value_name, class_member_name, me thod_name,
15 class_name, join_name 15 class_name, join_name
16 ) 16 )
17 from collections import defaultdict, OrderedDict 17 from collections import defaultdict, OrderedDict
18 from itertools import chain 18 from itertools import chain
19 19
20 # Heuristic ordering of types from largest to smallest, used to sort fields by t heir alignment sizes.
21 # Specifying the exact alignment sizes for each type is impossible because it's platform specific,
22 # so we define an ordering instead.
23 # The ordering comes from the data obtained in:
24 # https://codereview.chromium.org/2841413002
25 # TODO(shend): Put alignment sizes into code form, rather than linking to a CL w hich may disappear.
26 ALIGNMENT_ORDER = [
27 'double',
28 'FillLayer', 'BorderData', # Aligns like a void * (can be 32 or 64 bits)
29 'LengthBox', 'Length', 'float',
30 'StyleColor', 'Color', 'unsigned', 'int',
31 'short',
32 'char',
33 'bool'
34 ]
35
20 # TODO(shend): Improve documentation and add docstrings. 36 # TODO(shend): Improve documentation and add docstrings.
21 37
22 38
23 def _flatten_list(x): 39 def _flatten_list(x):
24 """Flattens a list of lists into a single list.""" 40 """Flattens a list of lists into a single list."""
25 return list(chain.from_iterable(x)) 41 return list(chain.from_iterable(x))
26 42
27 43
28 def _num_32_bit_words_for_bit_fields(bit_fields): 44 def _num_32_bit_words_for_bit_fields(bit_fields):
29 """Gets the number of 32 bit unsigned integers needed store a list of bit fi elds.""" 45 """Gets the number of 32 bit unsigned integers needed store a list of bit fi elds."""
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 281
266 # TODO(shend): Get rid of the property/nonproperty field roles. 282 # TODO(shend): Get rid of the property/nonproperty field roles.
267 # If the field has_custom_compare_and_copy, then it does not appear in 283 # If the field has_custom_compare_and_copy, then it does not appear in
268 # ComputedStyle::operator== and ComputedStyle::CopyNonInheritedFromC ached. 284 # ComputedStyle::operator== and ComputedStyle::CopyNonInheritedFromC ached.
269 field_role = 'nonproperty' if property_['has_custom_compare_and_copy '] else 'property' 285 field_role = 'nonproperty' if property_['has_custom_compare_and_copy '] else 'property'
270 fields.append(_create_field(field_role, property_)) 286 fields.append(_create_field(field_role, property_))
271 287
272 return fields 288 return fields
273 289
274 290
275 def _reorder_fields(fields): 291 def _reorder_bit_fields(bit_fields):
276 """
277 Returns a list of fields ordered to minimise padding.
278 """
279 # Separate out bit fields from non bit fields
280 bit_fields = [field for field in fields if field.is_bit_field]
281 non_bit_fields = [field for field in fields if not field.is_bit_field]
282
283 # Since fields cannot cross word boundaries, in order to minimize 292 # Since fields cannot cross word boundaries, in order to minimize
284 # padding, group fields into buckets so that as many buckets as possible 293 # padding, group fields into buckets so that as many buckets as possible
285 # are exactly 32 bits. Although this greedy approach may not always 294 # are exactly 32 bits. Although this greedy approach may not always
286 # produce the optimal solution, we add a static_assert to the code to 295 # produce the optimal solution, we add a static_assert to the code to
287 # ensure ComputedStyleBase results in the expected size. If that 296 # ensure ComputedStyleBase results in the expected size. If that
288 # static_assert fails, this code is falling into the small number of 297 # static_assert fails, this code is falling into the small number of
289 # cases that are suboptimal, and may need to be rethought. 298 # cases that are suboptimal, and may need to be rethought.
290 # For more details on packing bit fields to reduce padding, see: 299 # For more details on packing bit fields to reduce padding, see:
291 # http://www.catb.org/esr/structure-packing/#_bitfields 300 # http://www.catb.org/esr/structure-packing/#_bitfields
292 field_buckets = [] 301 field_buckets = []
293 # Consider fields in descending order of size to reduce fragmentation 302 # Consider fields in descending order of size to reduce fragmentation
294 # when they are selected. Ties broken in alphabetical order by name. 303 # when they are selected. Ties broken in alphabetical order by name.
295 for field in sorted(bit_fields, key=lambda f: (-f.size, f.name)): 304 for field in sorted(bit_fields, key=lambda f: (-f.size, f.name)):
296 added_to_bucket = False 305 added_to_bucket = False
297 # Go through each bucket and add this field if it will not increase 306 # Go through each bucket and add this field if it will not increase
298 # the bucket's size to larger than 32 bits. Otherwise, make a new 307 # the bucket's size to larger than 32 bits. Otherwise, make a new
299 # bucket containing only this field. 308 # bucket containing only this field.
300 for bucket in field_buckets: 309 for bucket in field_buckets:
301 if sum(f.size for f in bucket) + field.size <= 32: 310 if sum(f.size for f in bucket) + field.size <= 32:
302 bucket.append(field) 311 bucket.append(field)
303 added_to_bucket = True 312 added_to_bucket = True
304 break 313 break
305 if not added_to_bucket: 314 if not added_to_bucket:
306 field_buckets.append([field]) 315 field_buckets.append([field])
307 316
317 return _flatten_list(field_buckets)
318
319
320 def _reorder_non_bit_fields(non_bit_fields):
321 # A general rule of thumb is to sort members by their alignment requirement
322 # (from biggest aligned to smallest).
323 for field in non_bit_fields:
324 assert field.type_name in ALIGNMENT_ORDER, \
325 "Type {} has unknown alignment. Please update ALIGNMENT_ORDER to inc lude it.".format(field.type_name)
326 return list(sorted(non_bit_fields, key=lambda f: ALIGNMENT_ORDER.index(f.typ e_name)))
327
328
329 def _reorder_fields(fields):
330 """
331 Returns a list of fields ordered to minimise padding.
332 """
333 # Separate out bit fields from non bit fields
334 bit_fields = [field for field in fields if field.is_bit_field]
335 non_bit_fields = [field for field in fields if not field.is_bit_field]
336
308 # Non bit fields go first, then the bit fields. 337 # Non bit fields go first, then the bit fields.
309 return list(non_bit_fields) + _flatten_list(field_buckets) 338 return _reorder_non_bit_fields(non_bit_fields) + _reorder_bit_fields(bit_fie lds)
310 339
311 340
312 class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter): 341 class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter):
313 def __init__(self, json5_file_paths): 342 def __init__(self, json5_file_paths):
314 # Read CSS properties 343 # Read CSS properties
315 super(ComputedStyleBaseWriter, self).__init__([json5_file_paths[0]]) 344 super(ComputedStyleBaseWriter, self).__init__([json5_file_paths[0]])
316 345
317 # Ignore shorthand properties 346 # Ignore shorthand properties
318 for property_ in self._properties.values(): 347 for property_ in self._properties.values():
319 if property_['field_template'] is not None: 348 if property_['field_template'] is not None:
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 407
379 @template_expander.use_jinja('ComputedStyleBaseConstants.h.tmpl') 408 @template_expander.use_jinja('ComputedStyleBaseConstants.h.tmpl')
380 def generate_base_computed_style_constants(self): 409 def generate_base_computed_style_constants(self):
381 return { 410 return {
382 'properties': self._properties, 411 'properties': self._properties,
383 'enums': self._generated_enums, 412 'enums': self._generated_enums,
384 } 413 }
385 414
386 if __name__ == '__main__': 415 if __name__ == '__main__':
387 json5_generator.Maker(ComputedStyleBaseWriter).main() 416 json5_generator.Maker(ComputedStyleBaseWriter).main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698