]> Sergey Matveev's repositories - public-inbox.git/commitdiff
over: cull unneeded fields for get_thread
authorEric Wong <e@80x24.org>
Tue, 8 Jan 2019 11:13:33 +0000 (11:13 +0000)
committerEric Wong <e@80x24.org>
Tue, 8 Jan 2019 19:28:02 +0000 (19:28 +0000)
On a certain ugly /$INBOX/$MESSAGE_ID/T/ endpoint with 1000
messages in the thread, this cuts memory usage from 2.5M to 1.9M
(which still isn't great, but it's a start).

lib/PublicInbox/Over.pm
lib/PublicInbox/SearchMsg.pm

index dda1e6d008ca4eb08439681f94fc09ce77d65c8c..598c9fcba18f53cd0e5a2e11c25f268eaede369c 100644 (file)
@@ -40,13 +40,16 @@ sub disconnect { $_[0]->{dbh} = undef }
 
 sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
 
-sub load_from_row {
-       my ($smsg) = @_;
+sub load_from_row ($;$) {
+       my ($smsg, $cull) = @_;
        bless $smsg, 'PublicInbox::SearchMsg';
        if (defined(my $data = delete $smsg->{ddd})) {
                $data = uncompress($data);
                utf8::decode($data);
-               $smsg->load_from_data($data);
+               PublicInbox::SearchMsg::load_from_data($smsg, $data);
+
+               # saves over 600K for 1000+ message threads
+               PublicInbox::SearchMsg::psgi_cull($smsg) if $cull;
        }
        $smsg
 }
@@ -57,7 +60,8 @@ sub do_get {
        my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
        $sql .= "LIMIT $lim";
        my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
-       load_from_row($_) for @$msgs;
+       my $cull = $opts->{cull};
+       load_from_row($_, $cull) for @$msgs;
        $msgs
 }
 
@@ -82,6 +86,7 @@ sub nothing () { wantarray ? (0, []) : [] };
 sub get_thread {
        my ($self, $mid, $prev) = @_;
        my $dbh = $self->connect;
+       my $opts = { cull => 1 };
 
        my $id = $dbh->selectrow_array(<<'', undef, $mid);
 SELECT id FROM msgid WHERE mid = ? LIMIT 1
@@ -109,7 +114,7 @@ SELECT tid,sid FROM over WHERE num = ? LIMIT 1
 
        my $cols = 'num,ts,ds,ddd';
        unless (wantarray) {
-               return do_get($self, <<"", {}, $tid, $sid, $num);
+               return do_get($self, <<"", $opts, $tid, $sid, $num);
 SELECT $cols FROM over WHERE $cond_all
 ORDER BY $sort_col ASC
 
@@ -123,14 +128,14 @@ SELECT COUNT(num) FROM over WHERE $cond_all
 
        # giant thread, prioritize strict (tid) matches and throw
        # in the loose (sid) matches at the end
-       my $msgs = do_get($self, <<"", {}, $tid, $num);
+       my $msgs = do_get($self, <<"", $opts, $tid, $num);
 SELECT $cols FROM over WHERE tid = ? AND num > ?
 ORDER BY $sort_col ASC
 
        # do we have room for loose matches? get the most recent ones, first:
        my $lim = DEFAULT_LIMIT - scalar(@$msgs);
        if ($lim > 0) {
-               my $opts = { limit => $lim };
+               $opts->{limit} = $lim;
                my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
 ORDER BY $sort_col DESC
index 292efceed7c012bc36cee85fd79a5eb5795c6399..722a1b9428fba760591452f67a044a1af8ba812d 100644 (file)
@@ -82,18 +82,21 @@ sub load_expand {
        $self;
 }
 
+sub psgi_cull ($) {
+       my ($self) = @_;
+       from_name($self); # fill in {from_name} so we can delete {from}
+
+       # drop NNTP-only fields which aren't relevant to PSGI results:
+       # saves ~80K on a 200 item search result:
+       delete @$self{qw(from ts to cc bytes lines)};
+       $self;
+}
+
 # Only called by PSGI interface, not NNTP
 sub load_doc {
        my ($class, $doc) = @_;
        my $self = bless {}, $class;
-       my $smsg = load_expand($self, $doc);
-
-       from_name($smsg); # fill in {from_name} so we can delete {from}
-
-       # drop NNTP-only fields which aren't relevant to PSGI results:
-       # saves ~80K on a 200 item search result:
-       delete @$smsg{qw(from ts to cc bytes lines)};
-       $smsg;
+       psgi_cull(load_expand($self, $doc));
 }
 
 # :bytes and :lines metadata in RFC 3977