Explorar el Código

Cache remote stylesheets for the current boot

Profiling a warm cloudron.io load showed the five stylesheet fetches took
most of the wait (about 1.3 s of 1.9 s to first paint), while the HTML fetch
and paint were short.  Keep successful remote stylesheet responses under
/share/bcss-* (256 KB each, 16 entries) and reuse them on a reload or a new
browser launch.  The cache is bounded, skipped when full and cleared by
reboot/kexec.

Verified on Cloudron: all five sheets hit the cache on reload.
Johannes Zellner hace 1 semana
padre
commit
fc9a58ea38
Se han modificado 3 ficheros con 113 adiciones y 5 borrados
  1. 6 4
      AGENTS.md
  2. 106 0
      browser.c
  3. 1 1
      docs/userland.html

+ 6 - 4
AGENTS.md

@@ -713,11 +713,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   (`cloudron.io`) is treated as a hostname and skips the local-file stat;
   origin-level redirects are remembered per process and in `/share/borigins`,
   so later visits to a bare domain go straight to its canonical scheme/host.
+  Remote stylesheets are cached under `/share/bcss-*` for the current boot, so
+  a reload or a fresh browser launch does not fetch the same CSS sheets again.
   Images are fetched one per main-loop iteration (so text and scrolling stay
-  responsive) and only when their measured
-  position is within the viewport plus 160 px; deferred images are loaded as
-  the page scrolls.  Network work runs in a headless `curl __fetch` helper
-  process (`ucmd_curl.c`): it writes the raw body and a small metadata file
+  responsive) and only when their measured position is within the viewport
+  plus 160 px; deferred images are loaded as the page scrolls.  Network work
+  runs in a headless `curl __fetch` helper process (`ucmd_curl.c`): it writes
+  the raw body and a small metadata file
   under `/share`, while the browser keeps drawing and scrolling its own
   window.  The helper asks for identity encoding because inflating multi-MB
   pages in the browser process stalled the UI; if a server still sends gzip,

+ 106 - 0
browser.c

@@ -33,6 +33,10 @@
 #define BR_IMG_LAYOUT_W 1000
 #define BR_CSS_MAX      (832u * 1024u)
 #define BR_CSS_COMP_MAX (192u * 1024u)
+/* Session cache for remote stylesheets (per launch; cleared at startup). */
+#define CSS_CACHE_MAX      16
+#define CSS_CACHE_FILE_MAX (256u * 1024u)
+#define CSS_CACHE_PREFIX   "bcss-"
 #define BR_MAX_SHEETS   6
 #define BR_HISTORY      16
 #define BR_URL_MAX      512
@@ -1261,6 +1265,104 @@ static int fetch_image(const char *url, unsigned int cap,
 }
 
 
+/* Remote stylesheets are immutable enough for the current boot: keep
+ * successful responses under /share so a reload, a second visit or a fresh
+ * browser launch does not fetch the same five sheets over the network again.
+ * /share is cleared by reboot/kexec; when the cache fills up it is reset. */
+static int css_cache_count;
+
+static unsigned int css_cache_hash(const char *s)
+{
+	unsigned int h = 2166136261u;
+
+	while (*s) {
+		h ^= (unsigned char)*s++;
+		h *= 16777619u;
+	}
+	return h;
+}
+
+static void css_cache_path(const char *url, char *out, unsigned int cap)
+{
+	unsigned int n = 0;
+
+	s_append(out, &n, "/share/" CSS_CACHE_PREFIX, cap);
+	s_append_dec(out, &n, css_cache_hash(url), cap);
+}
+
+static void css_cache_clear(void)
+{
+	struct sys_dirent d;
+	char path[96];
+
+	for (;;) {
+		int i, found = 0;
+
+		for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
+				       (int)&d) == 1; i++) {
+			unsigned int n = 0;
+
+			if (!starts_with(d.name, CSS_CACHE_PREFIX))
+				continue;
+			s_append(path, &n, "/share/", sizeof(path));
+			s_append(path, &n, d.name, sizeof(path));
+			if (u_syscall1(SYS_UNLINK, (int)path) < 0)
+				return;
+			found = 1;
+			break;
+		}
+		if (!found)
+			break;
+	}
+	css_cache_count = 0;
+}
+
+static void css_cache_init(void)
+{
+	struct sys_dirent d;
+	int i, count = 0;
+
+	for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
+			       (int)&d) == 1; i++)
+		if (starts_with(d.name, CSS_CACHE_PREFIX))
+			count++;
+	if (count >= CSS_CACHE_MAX) {
+		css_cache_clear();
+		return;
+	}
+	css_cache_count = count;
+}
+
+static int css_cache_get(const char *url, unsigned char **out,
+			 unsigned int *outlen)
+{
+	char path[96];
+	int rn;
+
+	css_cache_path(url, path, sizeof(path));
+	rn = u_syscall3(SYS_READ_FILE, (int)path, (int)image_io_buf,
+			(int)CSS_CACHE_FILE_MAX);
+	if (rn <= 0 || (unsigned int)rn >= CSS_CACHE_FILE_MAX)
+		return 0;
+	*out = image_io_buf;
+	*outlen = (unsigned int)rn;
+	return 1;
+}
+
+static void css_cache_put(const char *url, const unsigned char *data,
+			  unsigned int len)
+{
+	char path[96];
+
+	if (css_cache_count >= CSS_CACHE_MAX || len == 0 ||
+	    len >= CSS_CACHE_FILE_MAX)
+		return;
+	css_cache_path(url, path, sizeof(path));
+	if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)data,
+		       (int)len) >= 0)
+		css_cache_count++;
+}
+
 /* Read one stylesheet into the shared static buffer.  CSS is parsed
  * immediately, so it does not need the page heap; this leaves the heap for
  * the 2 MB page body, the rules and the scaled image bitmaps. */
@@ -1284,6 +1386,8 @@ static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
 		*outlen = (unsigned int)rv;
 		return 0;
 	}
+	if (css_cache_get(url, out, outlen))
+		return 0;
 	rv = fetch_remote(url, BR_CSS_MAX, image_io_buf, BR_CSS_MAX,
 			  image_io_buf + BR_CSS_MAX, BR_CSS_COMP_MAX,
 			  &fetch_meta, &tr, err, errcap);
@@ -1298,6 +1402,7 @@ static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
 	}
 	*out = image_io_buf;
 	*outlen = len;
+	css_cache_put(url, image_io_buf, len);
 	return 0;
 }
 
@@ -4387,6 +4492,7 @@ void _start(const char *arg)
 		s_copy(cwd, "/", sizeof(cwd));
 	heap_clear();
 	origin_cache_load();
+	css_cache_init();
 	page_scroll.offset = 0;
 	page_scroll.content_h = 0;
 

+ 1 - 1
docs/userland.html

@@ -359,7 +359,7 @@
 			<li><strong>Not supported.</strong> JavaScript, forms, pseudo-classes, border-radius clipping of descendants, gradients/box-shadow/transitions and CSS <code>color-mix</code>. Flex rows, card grids, basic tables, inline SVG and CSS variables are supported.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
 			<li><strong>Fetching.</strong> Each network fetch starts a one-shot helper process and a fresh TCP/TLS connection; images load one per loop within the viewport plus 160 px.</li>
-			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. Origin-level redirects (the common <code>http://host</code> → <code>https://host</code> → <code>https://www.host</code> chain) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin.</li>
+			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. Origin-level redirects (the common <code>http://host</code> → <code>https://host</code> → <code>https://www.host</code> chain) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
 			</ul>
 		<p>
 			The browser's default canvas is dark, matching the ngui theme. Pages