Procházet zdrojové kódy

browser: cache fallback-table column widths per layout pass

Rows of a table laid out through the per-row fallback (a table nested in
another table's cell) each recomputed the whole table's column widths,
measuring the text of every cell of every row: O(rows^2) text
measurement.  A Hacker News comment page (one row per comment) took
~1.75 s per measure and per render pass on the host, many times that on
the ION, and froze the browser.  table_columns() now keeps the result for
the current pass in an 8-slot cache keyed by table and width (one slot
per nesting level, since each comment row holds its own nested table);
inline_box_pass_id changes every pass, so DOM or style changes are never
served stale widths.  The item page's passes drop to ~0.25-0.3 s.
Johannes Zellner před 23 hodinami
rodič
revize
06acbc67d3
1 změnil soubory, kde provedl 54 přidání a 4 odebrání
  1. 54 4
      user/apps/browser/browser.c

+ 54 - 4
user/apps/browser/browser.c

@@ -12236,10 +12236,10 @@ static void layout_grid_auto(struct lay *l, unsigned int parent,
 /* Column widths are the widest cell in each column across every row, so all
  * rows line up.  The table model is still deliberately basic: no colspan,
  * border-collapse or percentage columns. */
-static void table_columns(struct lay *l, unsigned int table,
-			  const struct br_style *pst, int avail,
-			  int *raw, int maxcols, int *out_cols,
-			  int *out_total)
+static void table_columns_compute(struct lay *l, unsigned int table,
+				  const struct br_style *pst, int avail,
+				  int *raw, int maxcols, int *out_cols,
+				  int *out_total)
 {
 	unsigned int r, c;
 	int cols = 0, total = 0, fixed_sum = 0, free_raw = 0, free_cols = 0;
@@ -12409,6 +12409,56 @@ static void table_columns(struct lay *l, unsigned int table,
 	*out_total = total;
 }
 
+/* Every row of a fallback table asks for the same column widths, and each
+ * computation measures the text of every cell of every row: a Hacker News
+ * comment page (one row per comment) spent ~1.7 s per layout pass in this
+ * O(rows^2) walk.  The result is cached per table, width and layout pass
+ * (inline_box_pass_id changes with every measure/render pass, so a DOM or
+ * style change is never served stale widths). */
+#define TCOL_CACHE 8      /* nested tables: one slot per nesting level */
+static struct {
+	unsigned int table, pass;
+	int avail, maxcols, cols, total;
+	int raw[12];
+	int valid;
+} tcol_cache[TCOL_CACHE];
+static unsigned int tcol_next;
+
+static void table_columns(struct lay *l, unsigned int table,
+			  const struct br_style *pst, int avail,
+			  int *raw, int maxcols, int *out_cols,
+			  int *out_total)
+{
+	int i, k;
+
+	for (k = 0; k < TCOL_CACHE && maxcols <= 12; k++) {
+		if (!tcol_cache[k].valid || tcol_cache[k].table != table ||
+		    tcol_cache[k].pass != inline_box_pass_id ||
+		    tcol_cache[k].avail != avail ||
+		    tcol_cache[k].maxcols != maxcols)
+			continue;
+		for (i = 0; i < maxcols; i++)
+			raw[i] = tcol_cache[k].raw[i];
+		*out_cols = tcol_cache[k].cols;
+		*out_total = tcol_cache[k].total;
+		return;
+	}
+	table_columns_compute(l, table, pst, avail, raw, maxcols, out_cols,
+			      out_total);
+	if (maxcols > 12)
+		return;
+	k = (int)(tcol_next++ % TCOL_CACHE);
+	tcol_cache[k].table = table;
+	tcol_cache[k].pass = inline_box_pass_id;
+	tcol_cache[k].avail = avail;
+	tcol_cache[k].maxcols = maxcols;
+	tcol_cache[k].cols = *out_cols;
+	tcol_cache[k].total = *out_total;
+	for (i = 0; i < maxcols; i++)
+		tcol_cache[k].raw[i] = raw[i];
+	tcol_cache[k].valid = 1;
+}
+
 static void layout_table_row(struct lay *l, unsigned int row,
 			     const struct br_style *pst, int depth,
 			     unsigned short link)