Chromium Code Reviews| Index: third_party/WebKit/Source/build/scripts/make_computed_style_base.py |
| diff --git a/third_party/WebKit/Source/build/scripts/make_computed_style_base.py b/third_party/WebKit/Source/build/scripts/make_computed_style_base.py |
| index 38c6d0b336eba5f24bb3cd89812be0faea8c87df..bad92c327d6713add4691ec588bde43ce5739e6b 100755 |
| --- a/third_party/WebKit/Source/build/scripts/make_computed_style_base.py |
| +++ b/third_party/WebKit/Source/build/scripts/make_computed_style_base.py |
| @@ -12,9 +12,10 @@ import make_style_builder |
| from name_utilities import ( |
| enum_for_css_keyword, enum_type_name, enum_value_name, class_member_name, method_name, |
| - join_name |
| + class_name, join_name |
| ) |
| -from collections import OrderedDict |
| +from collections import defaultdict, OrderedDict |
| +from itertools import chain |
| # Temporary hard-coded list of fields that are not CSS properties. |
| @@ -69,6 +70,36 @@ NONPROPERTIES = [ |
| ] |
| +def _flatten_list(x): |
| + """Flattens a list of lists into a single list.""" |
| + return list(chain.from_iterable(x)) |
| + |
| + |
| +def _num_bit_field_buckets(bit_fields): |
|
meade_UTC10
2017/04/06 03:29:57
would this be better as something like
_num_32_bi
shend
2017/04/06 04:39:39
Done.
|
| + """Gets the number of 32 bit unsigned integers needed store a list of bit fields.""" |
| + num_buckets, cur_bucket = 0, 0 |
| + for field in bit_fields: |
| + if field.size + cur_bucket > 32: |
| + num_buckets += 1 |
| + cur_bucket = 0 |
| + cur_bucket += field.size |
| + return num_buckets + (cur_bucket > 0) |
| + |
| + |
| +class Group(object): |
| + def __init__(self, name, subgroups, fields): |
| + """Represents a group of fields, which may contain subgroups that are dynamically allocated.""" |
|
meade_UTC10
2017/04/06 03:29:57
Could you please document the types of the paramet
shend
2017/04/06 04:39:39
Agreed, the documentation in this file could be si
|
| + self.name = name |
| + self.subgroups = subgroups |
| + self.fields = fields |
| + self.type_name = class_name(join_name('style', name, ' data')) |
| + self.member_name = class_member_name(name) |
| + self.num_bit_field_buckets = _num_bit_field_buckets(field for field in fields if field.is_bit_field) |
| + |
| + # Recursively get all the fields in the subgroups as well |
| + self.all_fields = _flatten_list(subgroup.all_fields for subgroup in subgroups) + fields |
| + |
| + |
| class Field(object): |
| """ |
| The generated ComputedStyle object is made up of a series of Fields. |
| @@ -99,12 +130,13 @@ class Field(object): |
| """ |
| def __init__(self, field_role, name_for_methods, property_name, type_name, |
| - field_template, size, default_value, **kwargs): |
| + field_template, field_group, size, default_value, **kwargs): |
| """Creates a new field.""" |
| self.name = class_member_name(name_for_methods) |
| self.property_name = property_name |
| self.type_name = type_name |
| self.field_template = field_template |
| + self.field_group = field_group |
| self.size = size |
| self.default_value = default_value |
| @@ -146,6 +178,17 @@ def _get_include_paths(properties): |
| return list(sorted(include_paths)) |
| +def _group_fields(fields): |
| + """Groups a list of fields by their field_group and returns the root group.""" |
| + groups = defaultdict(list) |
| + for field in fields: |
| + groups[field.field_group].append(field) |
| + |
| + no_group = groups.pop(None) |
| + subgroups = [Group(field_group, [], _reorder_fields(fields)) for field_group, fields in groups.items()] |
| + return Group('', subgroups=subgroups, fields=_reorder_fields(no_group)) |
| + |
| + |
| def _create_enums(properties): |
| """ |
| Returns an OrderedDict of enums to be generated, enum name -> [list of enum values] |
| @@ -209,6 +252,7 @@ def _create_field(field_role, property_): |
| independent=property_['independent'], |
| type_name=type_name, |
| field_template=property_['field_template'], |
| + field_group=property_['field_group'], |
| size=size, |
| default_value=default_value, |
| ) |
| @@ -225,6 +269,7 @@ def _create_inherited_flag_field(property_): |
| property_name=property_['name'], |
| type_name='bool', |
| field_template='flag', |
| + field_group=property_['field_group'], |
| size=1, |
| default_value='true', |
| ) |
| @@ -248,11 +293,14 @@ def _create_fields(field_role, properties): |
| return fields |
| -def _pack_fields(fields): |
| +def _reorder_fields(fields): |
|
meade_UTC10
2017/04/06 03:29:57
You're still doing packing though? I don't think t
shend
2017/04/06 04:39:39
For me, packing means the compiler "packing" the s
|
| """ |
| - Group a list of fields into buckets to minimise padding. |
| - Returns a list of buckets, where each bucket is a list of Field objects. |
| + Returns a list of fields ordered to minimise padding. |
| """ |
| + # Separate out bit fields from normal fields |
| + bit_fields = [field for field in fields if field.is_bit_field] |
| + normal_fields = [field for field in fields if not field.is_bit_field] |
| + |
| # Since fields cannot cross word boundaries, in order to minimize |
| # padding, group fields into buckets so that as many buckets as possible |
| # are exactly 32 bits. Although this greedy approach may not always |
| @@ -265,7 +313,7 @@ def _pack_fields(fields): |
| field_buckets = [] |
| # Consider fields in descending order of size to reduce fragmentation |
| # when they are selected. Ties broken in alphabetical order by name. |
| - for field in sorted(fields, key=lambda f: (-f.size, f.name)): |
| + for field in sorted(bit_fields, key=lambda f: (-f.size, f.name)): |
| added_to_bucket = False |
| # Go through each bucket and add this field if it will not increase |
| # the bucket's size to larger than 32 bits. Otherwise, make a new |
| @@ -278,7 +326,8 @@ def _pack_fields(fields): |
| if not added_to_bucket: |
| field_buckets.append([field]) |
| - return field_buckets |
| + # Normal fields go first, then the bit fields. |
| + return list(normal_fields) + _flatten_list(field_buckets) |
| class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter): |
| @@ -299,6 +348,8 @@ class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter): |
| property_['field_type_path'] = None |
| if 'type_name' not in property_: |
| property_['type_name'] = 'E' + enum_type_name(property_['name_for_methods']) |
| + if 'field_group' not in property_: |
| + property_['field_group'] = None |
| property_values = self._properties.values() |
| @@ -309,46 +360,14 @@ class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter): |
| self._generated_enums = _create_enums(property_values + NONPROPERTIES) |
| - all_fields = (_create_fields('property', property_values) + |
| - _create_fields('nonproperty', NONPROPERTIES)) |
| - |
| - # Separate the normal fields from the bit fields |
| - bit_fields = [field for field in all_fields if field.is_bit_field] |
| - normal_fields = [field for field in all_fields if not field.is_bit_field] |
| - |
| - # Pack bit fields into buckets |
| - field_buckets = _pack_fields(bit_fields) |
| - |
| - # The expected size of ComputedStyleBase is equivalent to as many words |
| - # as the total number of buckets. |
| - self._expected_bit_field_bytes = len(field_buckets) |
| - |
| - # The most optimal size of ComputedStyleBase is the total sum of all the |
| - # field sizes, rounded up to the nearest word. If this produces the |
| - # incorrect value, either the packing algorithm is not optimal or there |
| - # is no way to pack the fields such that excess padding space is not |
| - # added. |
| - # If this fails, increase extra_padding_bytes by 1, but be aware that |
| - # this also increases ComputedStyleBase by 1 word. |
| - # We should be able to bring extra_padding_bytes back to 0 from time to |
| - # time. |
| - extra_padding_bytes = 0 |
| - optimal_bit_field_bytes = int(math.ceil(sum(f.size for f in bit_fields) / 32.0)) |
| - real_bit_field_bytes = optimal_bit_field_bytes + extra_padding_bytes |
| - assert self._expected_bit_field_bytes == real_bit_field_bytes, \ |
| - ('The field packing algorithm produced %s bytes, optimal is %s bytes' % |
| - (self._expected_bit_field_bytes, real_bit_field_bytes)) |
| - |
| - # Normal fields go first, then the bit fields. |
| - self._fields = list(normal_fields) |
| - |
| - # Order the fields so fields in each bucket are adjacent. |
| - for bucket in field_buckets: |
| - for field in bucket: |
| - self._fields.append(field) |
| + fields = (_create_fields('property', property_values) + |
| + _create_fields('nonproperty', NONPROPERTIES)) |
| - self._include_paths = _get_include_paths(property_values + NONPROPERTIES) |
| + # Separate fields into groups |
| + self._base_group = _group_fields(fields) |
| + # Get include paths |
|
meade_UTC10
2017/04/06 03:29:57
Is this really a helpful comment? :p
shend
2017/04/06 04:39:40
lol, removed.
|
| + self._include_paths = _get_include_paths(property_values) |
| @template_expander.use_jinja('ComputedStyleBase.h.tmpl') |
| def generate_base_computed_style_h(self): |
| @@ -356,7 +375,7 @@ class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter): |
| 'properties': self._properties, |
| 'enums': self._generated_enums, |
| 'include_paths': self._include_paths, |
| - 'fields': self._fields, |
| + 'computed_style': self._base_group, |
| } |
| @template_expander.use_jinja('ComputedStyleBase.cpp.tmpl') |
| @@ -364,8 +383,7 @@ class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter): |
| return { |
| 'properties': self._properties, |
| 'enums': self._generated_enums, |
| - 'fields': self._fields, |
| - 'expected_bit_field_bytes': self._expected_bit_field_bytes, |
| + 'computed_style': self._base_group, |
| } |
| @template_expander.use_jinja('ComputedStyleBaseConstants.h.tmpl') |
| @@ -373,7 +391,6 @@ class ComputedStyleBaseWriter(make_style_builder.StyleBuilderWriter): |
| return { |
| 'properties': self._properties, |
| 'enums': self._generated_enums, |
| - 'fields': self._fields, |
| } |
| @template_expander.use_jinja('CSSValueIDMappingsGenerated.h.tmpl') |