- my $self = shift;
- $self->_setup();
- $self->{rootset} = [
- grep { !$_->{parent} } values %{$self->{id_table}} ];
- $self->_finish();
-}
-
-sub _finish {
- my $self = shift;
- delete $self->{id_table};
- delete $self->{seen};
-}
-
-sub _get_cont_for_id ($$) {
- my ($self, $mid) = @_;
- $self->{id_table}{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
-}
-
-sub _setup {
- my ($self) = @_;
-
- _add_message($self, $_) foreach @{$self->{messages}};
-}
-
-sub _add_message ($$) {
- my ($self, $smsg) = @_;
-
- # A. if id_table...
- my $this = _get_cont_for_id($self, $smsg->{mid});
- $this->{smsg} = $smsg;
-
- # B. For each element in the message's References field:
- my $prev;
- if (defined(my $refs = $smsg->{references})) {
- foreach my $ref ($refs =~ m/<([^>]+)>/g) {
+ my ($msgs, $ordersub, $ctx) = @_;
+ my (%id_table, @imposters);
+ keys(%id_table) = scalar @$msgs; # pre-size
+
+ # A. put all current non-imposter $msgs (non-ghosts) into %id_table
+ # (imposters are messages with reused Message-IDs)
+ # Sadly, we sort here anyways since the fill-in-the-blanks References:
+ # can be shakier if somebody used In-Reply-To with multiple, disparate
+ # messages. So, take the client Date: into account since we can't
+ # always determine ordering when somebody uses multiple In-Reply-To.
+ my @kids = sort { $a->{ds} <=> $b->{ds} } grep {
+ # this delete saves around 4K across 1K messages
+ # TODO: move this to a more appropriate place, breaks tests
+ # if we do it during psgi_cull
+ delete $_->{num};
+
+ PublicInbox::SearchThread::Msg::cast($_);
+ if (exists $id_table{$_->{mid}}) {
+ $_->{children} = [];
+ push @imposters, $_; # we'll deal with them later
+ undef;
+ } else {
+ $id_table{$_->{mid}} = $_;
+ defined($_->{references});
+ }
+ } @$msgs;
+ for my $smsg (@kids) {
+ # This loop exists to help fill in gaps left from missing
+ # messages. It is not needed in a perfect world where
+ # everything is perfectly referenced, only the last ref
+ # matters.
+ my $prev;
+ for my $ref ($smsg->{references} =~ m/$MID_EXTRACT/go) {