#include "sort.h"
 #include <math.h>
 
+static bool hists__filter_entry_by_dso(struct hists *hists,
+                                      struct hist_entry *he);
+static bool hists__filter_entry_by_thread(struct hists *hists,
+                                         struct hist_entry *he);
+
 enum hist_filter {
        HIST_FILTER__DSO,
        HIST_FILTER__THREAD,
        return root;
 }
 
+static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
+{
+       hists__filter_entry_by_dso(hists, he);
+       hists__filter_entry_by_thread(hists, he);
+}
+
 static void __hists__collapse_resort(struct hists *hists, bool threaded)
 {
        struct rb_root *root;
                next = rb_next(&n->rb_node_in);
 
                rb_erase(&n->rb_node_in, root);
-               if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n))
+               if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
+                       /*
+                        * If it wasn't combined with one of the entries already
+                        * collapsed, we need to apply the filters that may have
+                        * been set by, say, the hist_browser.
+                        */
+                       hists__apply_filters(hists, n);
                        hists__inc_nr_entries(hists, n);
+               }
        }
 }
 
        hists__calc_col_len(hists, h);
 }
 
+
+static bool hists__filter_entry_by_dso(struct hists *hists,
+                                      struct hist_entry *he)
+{
+       if (hists->dso_filter != NULL &&
+           (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
+               he->filtered |= (1 << HIST_FILTER__DSO);
+               return true;
+       }
+
+       return false;
+}
+
 void hists__filter_by_dso(struct hists *hists)
 {
        struct rb_node *nd;
                if (symbol_conf.exclude_other && !h->parent)
                        continue;
 
-               if (hists->dso_filter != NULL &&
-                   (h->ms.map == NULL || h->ms.map->dso != hists->dso_filter)) {
-                       h->filtered |= (1 << HIST_FILTER__DSO);
+               if (hists__filter_entry_by_dso(hists, h))
                        continue;
-               }
 
                hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
        }
 }
 
+static bool hists__filter_entry_by_thread(struct hists *hists,
+                                         struct hist_entry *he)
+{
+       if (hists->thread_filter != NULL &&
+           he->thread != hists->thread_filter) {
+               he->filtered |= (1 << HIST_FILTER__THREAD);
+               return true;
+       }
+
+       return false;
+}
+
 void hists__filter_by_thread(struct hists *hists)
 {
        struct rb_node *nd;
        for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
                struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
 
-               if (hists->thread_filter != NULL &&
-                   h->thread != hists->thread_filter) {
-                       h->filtered |= (1 << HIST_FILTER__THREAD);
+               if (hists__filter_entry_by_thread(hists, h))
                        continue;
-               }
 
                hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
        }