unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Cc: "Robin H. Johnson" <robbat2@gentoo.org>
Subject: [PATCH 2/5] www: don't reread CSS files
Date: Thu, 26 Sep 2024 00:55:02 +0000	[thread overview]
Message-ID: <20240926005506.3703216-3-e@80x24.org> (raw)
In-Reply-To: <20240926005506.3703216-1-e@80x24.org>

With preloading, we usually read our CSS files in quick
succession so they are unlikely to change in between loads.
Thus we can save some syscalls and memory (assuming newer
Perl with CoW scalars).

Since our normal Perl code is only loaded once and ignores
changes on the FS after startup, we'll treat our CSS the
same way and assume they don't change after startup.
---
 lib/PublicInbox/WWW.pm | 48 ++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 20 deletions(-)

diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index 32d0410c..6f7c30b9 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -565,6 +565,20 @@ sub get_attach {
 # <pre>-formatted anyways.
 our $STYLE = 'pre{white-space:pre-wrap}*{font-size:100%;font-family:monospace}';
 
+sub _read_css ($$$) {
+	my ($fh, $mini, $fn) = @_;
+	my $ctime = 0;
+	my $local = PublicInbox::IO::read_all $fh; # sets _
+	if ($local =~ /\S/) {
+		$ctime = sprintf('%x',(stat(_))[10]);
+		$local = $mini->($local);
+	}
+	# do not let BOFHs override userContent.css:
+	return ($local, $ctime) if $local !~ /!\s*important\b/i;
+	warn "W: ignoring $fn since it uses `!important'\n";
+	();
+}
+
 sub stylesheets_prepare ($$) {
 	my ($self, $upfx) = @_;
 	my $mini = eval {
@@ -575,7 +589,7 @@ sub stylesheets_prepare ($$) {
 		sub { CSS::Minifier::XS::minify($_[0]) };
 	} || sub { $_[0] };
 
-	my $css_map = {};
+	my $css_map = $self->{-css_map} //= {};
 	my $stylesheets = $self->{pi_cfg}->{css} || [];
 	my $links = [];
 	my $inline_ok = 1;
@@ -598,24 +612,17 @@ sub stylesheets_prepare ($$) {
 				warn "ignoring $fn, non-ASCII word character\n";
 				next;
 			}
-			open(my $fh, '<', $fn) or do {
+			my ($local, $ctime);
+			if (my $rec = $css_map->{$key}) { # already loaded
+				($local, $ctime) = @$rec;
+			} elsif (open(my $fh, '<', $fn)) {
+				($local, $ctime) = _read_css $fh, $mini, $fn;
+				$css_map->{$key} = [ $local, $ctime ];
+			} else {
 				warn "failed to open $fn: $!\n";
 				next;
-			};
-			my $ctime = 0;
-			my $local = PublicInbox::IO::read_all $fh; # sets _
-			if ($local =~ /\S/) {
-				$ctime = sprintf('%x',(stat(_))[10]);
-				$local = $mini->($local);
-			}
-
-			# do not let BOFHs override userContent.css:
-			if ($local =~ /!\s*important\b/i) {
-				warn "ignoring $fn since it uses `!important'\n";
-				next;
 			}
 
-			$css_map->{$key} = $local;
 			$attr->{href} = "$upfx$key.css?$ctime";
 			if (defined($attr->{title})) {
 				$inline_ok = 0;
@@ -654,7 +661,7 @@ sub stylesheets_prepare ($$) {
 	} else {
 		$self->{-style_inline} = $buf;
 	}
-	$self->{-css_map} = $css_map;
+	$css_map;
 }
 
 # returns an HTML fragment with <style> or <link> tags in them
@@ -679,11 +686,12 @@ sub get_css ($$$) {
 	my $self = $ctx->{www};
 	my $css_map = $self->{-css_map} ||
 		stylesheets_prepare($self, defined($inbox) ? '' : '+/');
-	my $css = $css_map->{$key};
-	if (!defined($css) && defined($inbox) && $key eq 'userContent') {
-		$css = PublicInbox::UserContent::sample($ctx);
+	my $rec = $css_map->{$key};
+	if (!defined($rec) && defined($inbox) && $key eq 'userContent') {
+		$rec = [ PublicInbox::UserContent::sample($ctx) ];
 	}
-	defined $css or return r404();
+	$rec // return r404();
+	my ($css, undef) = @$rec; # TODO: Last-Modified
 	my $h = [ 'Content-Length', length($css), 'Content-Type', 'text/css' ];
 	PublicInbox::GitHTTPBackend::cache_one_year($h);
 	[ 200, $h, [ $css ] ];

  parent reply	other threads:[~2024-09-26  0:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-01  5:07 CSS behaviors bug report: dark/light switch non-functional Robin H. Johnson
2024-09-18  0:04 ` Eric Wong
2024-09-26  0:55 ` [PATCH 0/5] css improvements (hopefully) Eric Wong
2024-09-26  0:55   ` [PATCH 1/5] user_content: simplify internal API and use v5.12 Eric Wong
2024-09-26  0:55   ` Eric Wong [this message]
2024-09-26  0:55   ` [PATCH 3/5] www: load CSS files in same dir if @import is in use Eric Wong
2024-09-26  0:55   ` [PATCH 4/5] www: allow specifying CSS @import or <link> tags Eric Wong
2024-09-26 18:34     ` Štěpán Němec
2024-09-28 18:39       ` [PATCH v2] " Eric Wong
2024-09-28 19:29         ` Štěpán Němec
2024-09-28 20:42           ` Eric Wong
2024-09-29  0:22             ` Eric Wong
2024-09-26  0:55   ` [PATCH 5/5] www: use mtime as CSS cache-buster instead of ctime Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240926005506.3703216-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --cc=robbat2@gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).