Skip to content

Commit

Permalink
Reduced amount of memory for AnonymousStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
ssjkamei committed Aug 31, 2023
1 parent c3002f9 commit adeb358
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
71 changes: 56 additions & 15 deletions weasyprint/css/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def _get_cascaded_style_indexes(self):
def get_computed_styles(self):
return self._style_rel.get_computed_styles()

def get_style_rel(self):
return self._style_rel

@staticmethod
def _page_type_match(selector_page_type, page_type):
if selector_page_type.side not in (None, page_type.side):
Expand Down Expand Up @@ -649,31 +652,68 @@ def declaration_precedence(origin, importance):
return 5


class AnonymousStyle(dict):
class AnonymousStyle():
"""Computed style used for anonymous boxes."""
def __init__(self, parent_style):
# border-*-style is none, so border-width computes to zero.
# Other than that, properties that would need computing are
# border-*-color, but they do not apply.
self.update({
'border_top_width': 0,
'border_bottom_width': 0,
'border_left_width': 0,
'border_right_width': 0,
'outline_width': 0,
})
def __init__(self, parent_style, style_rel):
self.parent_style = parent_style
self.specified = self
self.style_rel = style_rel
if parent_style:
self.cache = parent_style.cache
else:
self.cache = {'ratio_ch': {}, 'ratio_ex': {}}

style_rel.set_computed_style(self, None, self, {})

# border-*-style is none, so border-width computes to zero.
# Other than that, properties that would need computing are
# border-*-color, but they do not apply.
self['border_top_width'] = 0
self['border_bottom_width'] = 0
self['border_left_width'] = 0
self['border_right_width'] = 0
self['outline_width'] = 0

def copy(self):
copy = AnonymousStyle(self.parent_style)
copy.update(self)
copy = AnonymousStyle(self.parent_style, self.style_rel)
computed_keys = self.get_style_keys()
copy_computed_keys = copy.get_style_keys()
copy_computed_keys.update(computed_keys)
return copy

def get_style_keys(self):
return self.style_rel.get_computed_styles()[(self, None)]["properties"]

def get(self, key, default=None):
computed_keys = self.get_style_keys()
if key in computed_keys:
return computed_keys[key]
else:
if default:
computed_keys[key] = default
return default
else:
return None

def __getitem__(self, key):
computed_keys = self.get_style_keys()
if key in computed_keys:
return computed_keys[key]
else:
return self.__missing__(key)

def __setitem__(self, key, value):
computed_keys = self.get_style_keys()
computed_keys_copy = computed_keys.copy()
computed_keys_copy[key] = value
self.style_rel.set_computed_style_key(self, None, computed_keys_copy)

def __delitem__(self, key):
computed_keys = self.get_style_keys()
computed_keys_copy = computed_keys.copy()
del computed_keys_copy[key]
self.style_rel.set_computed_style_key(self, None, computed_keys_copy)

def __missing__(self, key):
if key in INHERITED or key[:2] == '__':
value = self[key] = self.parent_style[key]
Expand Down Expand Up @@ -710,6 +750,7 @@ def __init__(self, parent_style, cascaded, element, pseudo_type,
style_rel.set_computed_style(element, pseudo_type, self, {})

def copy(self):
# TODO: Need to modify for flex_layout(Consider sharing "properties")
copy = ComputedStyle(
self.parent_style, self.cascaded, self.element, self.pseudo_type,
self.root_style, self.base_url, self.style_rel)
Expand Down Expand Up @@ -832,7 +873,7 @@ def computed_from_cascaded(element, cascaded, parent_style, pseudo_type=None,
target_collector=None, style_rel=None):
"""Get a dict of computed style mixed from parent and cascaded styles."""
if not cascaded and parent_style is not None:
style = AnonymousStyle(parent_style)
style = AnonymousStyle(parent_style, style_rel)

else:
style = ComputedStyle(
Expand Down
3 changes: 2 additions & 1 deletion weasyprint/formatting_structure/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def __repr__(self):
def anonymous_from(cls, parent, *args, **kwargs):
"""Return an anonymous box that inherits from ``parent``."""
style = computed_from_cascaded(
cascaded={}, parent_style=parent.style, element=None)
cascaded={}, parent_style=parent.style, element=None,
style_rel=parent.style.style_rel)
return cls(parent.element_tag, style, parent.element, *args, **kwargs)

def copy(self):
Expand Down
3 changes: 2 additions & 1 deletion weasyprint/layout/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ def make_box(at_keyword, containing_block):
if style is None:
# doesn't affect counters
style = computed_from_cascaded(
element=None, cascaded={}, parent_style=page.style)
element=None, cascaded={}, parent_style=page.style,
style_rel=context.style_for.get_style_rel())
_standardize_page_based_counters(style, at_keyword)
box = boxes.MarginBox(at_keyword, style)
# Empty boxes should not be generated, but they may be needed for
Expand Down

0 comments on commit adeb358

Please sign in to comment.