Permalink
Browse files

Cache intermediate results for intrinsic widths on tables.

This fixes huges performance problems when a large table is split
across many pages.
  • Loading branch information...
1 parent 9834ccb commit 0ff73c49bc26ac3dc087a433f1d570c76b3efc12 @SimonSapin SimonSapin committed Sep 13, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 weasyprint/layout/preferred.py
@@ -12,6 +12,8 @@
from __future__ import division, unicode_literals
+import weakref
+
from ..formatting_structure import boxes
from .. import text
@@ -204,6 +206,8 @@ def inline_preferred_width(context, box, outer=True):
return adjust(box, outer, widest_line)
+TABLE_CACHE = weakref.WeakKeyDictionary()
+
def table_and_columns_preferred_widths(context, box, outer=True,
resolved_table_width=False):
"""Return preferred widths for the table and its columns.
@@ -217,6 +221,10 @@ def table_and_columns_preferred_widths(context, box, outer=True,
"""
table = box.get_wrapped_table()
+ result = TABLE_CACHE.get(table)
+ if result:
+ return result
+
if table.style.border_collapse == 'separate':
border_spacing_x, _ = table.style.border_spacing
else:
@@ -370,9 +378,11 @@ def table_and_columns_preferred_widths(context, box, outer=True,
if table_preferred_minimum_width > table_preferred_width:
table_preferred_width = table_preferred_minimum_width
- return (
+ result = (
table_preferred_minimum_width, table_preferred_width,
column_preferred_minimum_widths, column_preferred_widths)
+ TABLE_CACHE[table] = result
+ return result
def table_preferred_minimum_width(context, box, outer=True):

0 comments on commit 0ff73c4

Please sign in to comment.