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:
Rodrigo Girão Serrão 2023-09-26 13:32:30 +01:00
parent 7fbcc6d7c7
commit 7eb7c929d5
No known key found for this signature in database
GPG Key ID: 84116786F3295A35
1 changed files with 5 additions and 4 deletions

View File

@ -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:]):