diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
index 0823f0983eaa7a0ba1ee3456e0fd363c97dda64e_bWVyY3VyaWFsL2xvY2FscmVwby5weQ==..7e89bd0cfb8665de1b490f20ae2d1902b8f12465_bWVyY3VyaWFsL2xvY2FscmVwby5weQ== 100644
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -430,6 +430,9 @@
         # post-dirstate-status hooks
         self._postdsstatus = []
 
+        # Cache of types representing filtered repos.
+        self._filteredrepotypes = weakref.WeakKeyDictionary()
+
         # generic mapping between names and nodes
         self.names = namespaces.namespaces()
 
@@ -539,11 +542,21 @@
 
     def filtered(self, name):
         """Return a filtered version of a repository"""
-        # build a new class with the mixin and the current class
-        # (possibly subclass of the repo)
-        class filteredrepo(repoview.repoview, self.unfiltered().__class__):
-            pass
-        return filteredrepo(self, name)
+        # Python <3.4 easily leaks types via __mro__. See
+        # https://bugs.python.org/issue17950. We cache dynamically
+        # created types so this method doesn't leak on every
+        # invocation.
+
+        key = self.unfiltered().__class__
+        if key not in self._filteredrepotypes:
+            # Build a new type with the repoview mixin and the base
+            # class of this repo. Give it a name containing the
+            # filter name to aid debugging.
+            bases = (repoview.repoview, key)
+            cls = type('%sfilteredrepo' % name, bases, {})
+            self._filteredrepotypes[key] = cls
+
+        return self._filteredrepotypes[key](self, name)
 
     @repofilecache('bookmarks', 'bookmarks.current')
     def _bookmarks(self):
diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py
index 0823f0983eaa7a0ba1ee3456e0fd363c97dda64e_bWVyY3VyaWFsL3N0YXRpY2h0dHByZXBvLnB5..7e89bd0cfb8665de1b490f20ae2d1902b8f12465_bWVyY3VyaWFsL3N0YXRpY2h0dHByZXBvLnB5 100644
--- a/mercurial/statichttprepo.py
+++ b/mercurial/statichttprepo.py
@@ -165,6 +165,8 @@
         self.encodepats = None
         self.decodepats = None
         self._transref = None
+        # Cache of types representing filtered repos.
+        self._filteredrepotypes = {}
 
     def _restrictcapabilities(self, caps):
         caps = super(statichttprepository, self)._restrictcapabilities(caps)