user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* Re: [PATCH 02/30] httpd/async: support passing arg to callbacks
  2019-12-25  7:50  6% ` [PATCH 02/30] httpd/async: support passing arg to callbacks Eric Wong
@ 2019-12-26  7:53  7%   ` Eric Wong
  0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2019-12-26  7:53 UTC (permalink / raw)
  To: meta

Eric Wong <e@80x24.org> wrote:
> index eea59b6d..d59320bc 100644
> --- a/lib/PublicInbox/SolverGit.pm
> +++ b/lib/PublicInbox/SolverGit.pm
> @@ -363,18 +363,13 @@ sub do_step ($) {
>  	}
>  }
>  
> -sub step_cb ($) {
> -	my ($self) = @_;
> -	sub { do_step($self) };
> -}
> -
>  sub next_step ($) {
>  	my ($self) = @_;
>  	# if outside of public-inbox-httpd, caller is expected to be
> -	# looping step_cb, anyways
> +	# looping do_step, anyways
>  	my $async = $self->{psgi_env}->{'pi-httpd.async'} or return;
>  	# PublicInbox::HTTPD::Async->new
> -	$async->(undef, step_cb($self));
> +	$async->(undef, \&do_step, $self);
>  }
>  
>  sub mark_found ($$$) {
> @@ -598,12 +593,11 @@ sub solve ($$$$$) {
>  	$self->{tmp} = File::Temp->newdir("solver.$oid_want-XXXXXXXX", TMPDIR => 1);
>  
>  	dbg($self, "solving $oid_want ...");
> -	my $step_cb = step_cb($self);
>  	if (my $async = $env->{'pi-httpd.async'}) {
>  		# PublicInbox::HTTPD::Async->new
> -		$async->(undef, $step_cb);
> +		$async->(undef, \&do_step, $self);
>  	} else {
> -		$step_cb->() while $self->{user_cb};
> +		do_step($self) while $self->{user_cb};
>  	}
>  }
>  

Those calls to $async->(...) were wrong, given how
HTTPD::Async->new works in the '!defined($io)' path.
Failures were exposed using the async API:

  https://public-inbox.org/meta/20191226064804.23024-4-e@80x24.org/

So we'll rename do_step to event_step and rely on DS::requeue to
call it, instead:

diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm
index 70769bba..d182c118 100644
--- a/lib/PublicInbox/HTTPD/Async.pm
+++ b/lib/PublicInbox/HTTPD/Async.pm
@@ -23,7 +23,7 @@ sub new {
 	# no $io? call $cb at the top of the next event loop to
 	# avoid recursion:
 	unless (defined($io)) {
-		PublicInbox::DS::requeue($cb);
+		PublicInbox::DS::requeue($cb ? $cb : $arg);
 		die '$end unsupported w/o $io' if $end;
 		return;
 	}
diff --git a/lib/PublicInbox/SolverGit.pm b/lib/PublicInbox/SolverGit.pm
index d59320bc..b3fc5bef 100644
--- a/lib/PublicInbox/SolverGit.pm
+++ b/lib/PublicInbox/SolverGit.pm
@@ -327,7 +327,7 @@ sub do_finish ($$) {
 	$user_cb->(undef);
 }
 
-sub do_step ($) {
+sub event_step ($) {
 	my ($self) = @_;
 	eval {
 		# step 1: resolve blobs to patches in the todo queue
@@ -366,10 +366,10 @@ sub do_step ($) {
 sub next_step ($) {
 	my ($self) = @_;
 	# if outside of public-inbox-httpd, caller is expected to be
-	# looping do_step, anyways
+	# looping event_step, anyways
 	my $async = $self->{psgi_env}->{'pi-httpd.async'} or return;
 	# PublicInbox::HTTPD::Async->new
-	$async->(undef, \&do_step, $self);
+	$async->(undef, undef, $self);
 }
 
 sub mark_found ($$$) {
@@ -595,9 +595,9 @@ sub solve ($$$$$) {
 	dbg($self, "solving $oid_want ...");
 	if (my $async = $env->{'pi-httpd.async'}) {
 		# PublicInbox::HTTPD::Async->new
-		$async->(undef, \&do_step, $self);
+		$async->(undef, undef, $self);
 	} else {
-		do_step($self) while $self->{user_cb};
+		event_step($self) while $self->{user_cb};
 	}
 }
 

^ permalink raw reply related	[relevance 7%]

* [PATCH 02/30] httpd/async: support passing arg to callbacks
  2019-12-25  7:50  7% [PATCH 00/30] www: eliminate most per-request closures Eric Wong
@ 2019-12-25  7:50  6% ` Eric Wong
  2019-12-26  7:53  7%   ` Eric Wong
  0 siblings, 1 reply; 3+ results
From: Eric Wong @ 2019-12-25  7:50 UTC (permalink / raw)
  To: meta

Another step towards removing anonymous subs to eliminate
a possible source of memory leaks and high memory use.
---
 lib/PublicInbox/HTTPD/Async.pm |  6 ++++--
 lib/PublicInbox/Qspawn.pm      |  4 ++--
 lib/PublicInbox/SolverGit.pm   | 14 ++++----------
 3 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm
index d5628ee8..70769bba 100644
--- a/lib/PublicInbox/HTTPD/Async.pm
+++ b/lib/PublicInbox/HTTPD/Async.pm
@@ -10,7 +10,7 @@ package PublicInbox::HTTPD::Async;
 use strict;
 use warnings;
 use base qw(PublicInbox::DS);
-use fields qw(cb end);
+use fields qw(cb arg end end_arg);
 use Errno qw(EAGAIN);
 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
 
@@ -18,7 +18,7 @@ use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
 # $io is a read-only pipe ($rpipe) for now, but may be a
 # bidirectional socket in the future.
 sub new {
-	my ($class, $io, $cb, $end) = @_;
+	my ($class, $io, $cb, $arg, $end, $end_arg) = @_;
 
 	# no $io? call $cb at the top of the next event loop to
 	# avoid recursion:
@@ -32,7 +32,9 @@ sub new {
 	IO::Handle::blocking($io, 0);
 	$self->SUPER::new($io, EPOLLIN | EPOLLET);
 	$self->{cb} = $cb; # initial read callback, later replaced by main_cb
+	$self->{arg} = $arg; # arg for $cb
 	$self->{end} = $end; # like END {}, but only for this object
+	$self->{end_arg} = $end_arg; # arg for $end
 	$self;
 }
 
diff --git a/lib/PublicInbox/Qspawn.pm b/lib/PublicInbox/Qspawn.pm
index 651fa390..c2856609 100644
--- a/lib/PublicInbox/Qspawn.pm
+++ b/lib/PublicInbox/Qspawn.pm
@@ -185,7 +185,7 @@ reread:
 		($rpipe) = @_; # popen_rd result
 		if ($async) {
 		# PublicInbox::HTTPD::Async->new($rpipe, $cb, $end)
-			$async = $async->($rpipe, $cb, $end);
+			$async = $async->($rpipe, $cb, undef, $end);
 			# $cb will call ->async_pass or ->close
 		} else { # generic PSGI
 			$cb->() while $qx;
@@ -297,7 +297,7 @@ sub psgi_return {
 		($rpipe) = @_;
 		if ($async) {
 			# PublicInbox::HTTPD::Async->new($rpipe, $cb, $end)
-			$async = $async->($rpipe, $cb, $end);
+			$async = $async->($rpipe, $cb, undef, $end);
 			# $cb will call ->async_pass or ->close
 		} else { # generic PSGI
 			$cb->() while $rd_hdr;
diff --git a/lib/PublicInbox/SolverGit.pm b/lib/PublicInbox/SolverGit.pm
index eea59b6d..d59320bc 100644
--- a/lib/PublicInbox/SolverGit.pm
+++ b/lib/PublicInbox/SolverGit.pm
@@ -363,18 +363,13 @@ sub do_step ($) {
 	}
 }
 
-sub step_cb ($) {
-	my ($self) = @_;
-	sub { do_step($self) };
-}
-
 sub next_step ($) {
 	my ($self) = @_;
 	# if outside of public-inbox-httpd, caller is expected to be
-	# looping step_cb, anyways
+	# looping do_step, anyways
 	my $async = $self->{psgi_env}->{'pi-httpd.async'} or return;
 	# PublicInbox::HTTPD::Async->new
-	$async->(undef, step_cb($self));
+	$async->(undef, \&do_step, $self);
 }
 
 sub mark_found ($$$) {
@@ -598,12 +593,11 @@ sub solve ($$$$$) {
 	$self->{tmp} = File::Temp->newdir("solver.$oid_want-XXXXXXXX", TMPDIR => 1);
 
 	dbg($self, "solving $oid_want ...");
-	my $step_cb = step_cb($self);
 	if (my $async = $env->{'pi-httpd.async'}) {
 		# PublicInbox::HTTPD::Async->new
-		$async->(undef, $step_cb);
+		$async->(undef, \&do_step, $self);
 	} else {
-		$step_cb->() while $self->{user_cb};
+		do_step($self) while $self->{user_cb};
 	}
 }
 

^ permalink raw reply related	[relevance 6%]

* [PATCH 00/30] www: eliminate most per-request closures
@ 2019-12-25  7:50  7% Eric Wong
  2019-12-25  7:50  6% ` [PATCH 02/30] httpd/async: support passing arg to callbacks Eric Wong
  0 siblings, 1 reply; 3+ results
From: Eric Wong @ 2019-12-25  7:50 UTC (permalink / raw)
  To: meta

Closures (aka "anonymous subs") tack several KB of memory onto
every WWW request/response, decreasing scalability and
performance of our WWW endpoints.  They also increase human
review time to check for reference cycles.

Similar changes to -nntpd and the generic parts of -httpd were
also done recently:
https://public-inbox.org/meta/20191221235319.27082-1-e@80x24.org/
https://public-inbox.org/meta/20191221080007.27810-1-e@80x24.org/

These could still use some naming improvements, and it's been
pretty tiring writing the same-ish commit message over and over.

All these changes around eliminating closures also make it
easier to port our codebase to languages which lack closures.

Fwiw, I've been brainstorming ideas to create a new, refcounted
language where cyclic references are impossible by design.  Such
a design would not be possible if closures were implemented; but
doable otherwise by taking a hint from *nix FSes.

Eric Wong (30):
  git: allow async_cat to pass arg to callback
  httpd/async: support passing arg to callbacks
  qspawn: remove some anonymous subs for psgi_qx
  qspawn: disambiguate command vs PSGI env
  qspawn: replace anonymous $end callbacks w/ event_step
  msg_iter: provide means to stop using anonymous subs
  qspawn: reduce local vars, de-anonymize rd_hdr
  httpd/async: get rid of ephemeral main_cb
  qspawn: psgi_return: initial cb can be named
  qspawn: psgi_return_start: hoist out from psgi_return
  qspawn: psgi_qx: eliminate anonymous subs
  qspawn: drop "qspawn.filter" support, for now
  qspawn: psgi_return: allow non-anon parse_hdr callback
  githttpbackend: split out wwwstatic
  www: lazy load Plack::Util
  mboxgz: pass $ctx to callback to avoid anon subs
  feed: avoid anonymous subs
  config: each_inbox: pass user arg to callback
  view: avoid anon sub in stream_thread
  view: msg_html: stop using an anonymous sub
  contentid: no anonymous sub
  wwwtext: avoid anonymous sub in response
  searchview: pass named subs to Www*Stream
  view: thread_html: pass named sub to WwwStream
  searchview: remove anonymous sub when sorting threads by relevance
  view: msg_iter calls add_body_text directly
  wwwattach: avoid anonymous sub for msg_iter
  viewvcs: avoid anonymous sub for HTML response
  solvergit: allow passing arg to user-supplied callback
  search: retry_reopen passes user arg to callback

 MANIFEST                          |   1 +
 lib/PublicInbox/Cgit.pm           |  19 +-
 lib/PublicInbox/Config.pm         |  11 +-
 lib/PublicInbox/ContentId.pm      |  53 +++---
 lib/PublicInbox/ExtMsg.pm         |  58 +++---
 lib/PublicInbox/Feed.pm           |  51 +++--
 lib/PublicInbox/GetlineBody.pm    |  12 +-
 lib/PublicInbox/Git.pm            |  14 +-
 lib/PublicInbox/GitHTTPBackend.pm |  99 +---------
 lib/PublicInbox/HTTPD/Async.pm    |  56 +++---
 lib/PublicInbox/Mbox.pm           | 131 +++++++------
 lib/PublicInbox/MboxGz.pm         |   2 +-
 lib/PublicInbox/MsgIter.pm        |   8 +-
 lib/PublicInbox/NewsWWW.pm        |  16 +-
 lib/PublicInbox/Qspawn.pm         | 296 +++++++++++++++---------------
 lib/PublicInbox/Search.pm         |  16 +-
 lib/PublicInbox/SearchMsg.pm      |   9 +-
 lib/PublicInbox/SearchView.pm     | 100 +++++-----
 lib/PublicInbox/SolverGit.pm      | 149 ++++++++-------
 lib/PublicInbox/View.pm           | 187 ++++++++++---------
 lib/PublicInbox/ViewVCS.pm        | 111 ++++++-----
 lib/PublicInbox/WWW.pm            |   2 +-
 lib/PublicInbox/WwwAtomStream.pm  |   2 +-
 lib/PublicInbox/WwwAttach.pm      |  49 ++---
 lib/PublicInbox/WwwListing.pm     |  37 ++--
 lib/PublicInbox/WwwStatic.pm      | 105 +++++++++++
 lib/PublicInbox/WwwText.pm        |  20 +-
 t/git.t                           |  21 +++
 t/qspawn.t                        |  19 +-
 29 files changed, 882 insertions(+), 772 deletions(-)
 create mode 100644 lib/PublicInbox/WwwStatic.pm


^ permalink raw reply	[relevance 7%]

Results 1-3 of 3 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2019-12-25  7:50  7% [PATCH 00/30] www: eliminate most per-request closures Eric Wong
2019-12-25  7:50  6% ` [PATCH 02/30] httpd/async: support passing arg to callbacks Eric Wong
2019-12-26  7:53  7%   ` Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).