mirror of https://github.com/Textualize/rich.git
Refactor cache key in Text.render.
For a reason similar to that of 7fbcc6d7
, we need to use the style indices as cache keys here and not the styles themselves because styles that have the same link/meta will have different link IDs, but have the same hash, so there will be cache collisions that we need to avoid.
This commit is contained in:
parent
7fbcc6d7c7
commit
7eb7c929d5
|
@ -746,18 +746,19 @@ class Text(JupyterMixin):
|
|||
stack_append = stack.append
|
||||
stack_pop = stack.remove
|
||||
|
||||
style_cache: Dict[Tuple[Style, ...], Style] = {}
|
||||
style_cache: Dict[Tuple[int, ...], Style] = {}
|
||||
style_cache_get = style_cache.get
|
||||
combine = Style.combine
|
||||
|
||||
def get_current_style() -> Style:
|
||||
"""Construct current style from stack."""
|
||||
styles = tuple(style_map[_style_id] for _style_id in sorted(stack))
|
||||
cached_style = style_cache_get(styles)
|
||||
cache_key = tuple(sorted(stack))
|
||||
cached_style = style_cache_get(cache_key)
|
||||
if cached_style is not None:
|
||||
return cached_style
|
||||
styles = tuple(style_map[_style_id] for _style_id in sorted(stack))
|
||||
current_style = combine(styles)
|
||||
style_cache[styles] = current_style
|
||||
style_cache[cache_key] = current_style
|
||||
return current_style
|
||||
|
||||
for (offset, leaving, style_id), (next_offset, _, _) in zip(spans, spans[1:]):
|
||||
|
|
Loading…
Reference in New Issue