From 31add1a7c001f940e960479ffab229f4e53487a8 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 29 Mar 2022 19:32:40 +0200 Subject: [PATCH] console: improve flowlist performance (#5228) Co-authored-by: BkPHcgQL3V --- CHANGELOG.md | 2 ++ mitmproxy/tools/console/flowlist.py | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71a8c3ac2..b858091a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased: mitmproxy next +* Console Performance Improvements + ([#3427](https://github.com/mitmproxy/mitmproxy/issues/3427), @BkPHcgQL3V) * Add flatpak support to the browser addon ([#5200](https://github.com/mitmproxy/mitmproxy/issues/5200), @pauloromeira) * Add example addon to dump contents to files based on a filter expression diff --git a/mitmproxy/tools/console/flowlist.py b/mitmproxy/tools/console/flowlist.py index 431870d97..8b3d9551d 100644 --- a/mitmproxy/tools/console/flowlist.py +++ b/mitmproxy/tools/console/flowlist.py @@ -1,8 +1,11 @@ +import typing +from functools import lru_cache + import urwid +import mitmproxy.tools.console.master from mitmproxy.tools.console import common from mitmproxy.tools.console import layoutwidget -import mitmproxy.tools.console.master class FlowItem(urwid.WidgetWrap): @@ -40,6 +43,7 @@ class FlowItem(urwid.WidgetWrap): class FlowListWalker(urwid.ListWalker): + master: "mitmproxy.tools.console.master.ConsoleMaster" def __init__(self, master): self.master = master @@ -47,13 +51,14 @@ class FlowListWalker(urwid.ListWalker): def positions(self, reverse=False): # The stub implementation of positions can go once this issue is resolved: # https://github.com/urwid/urwid/issues/294 - ret = range(self.master.commands.execute("view.properties.length")) + ret = range(self.master.view.get_length()) if reverse: return reversed(ret) return ret def view_changed(self): self._modified() + self._get.cache_clear() def get_focus(self): if not self.master.view.focus.flow: @@ -65,19 +70,17 @@ class FlowListWalker(urwid.ListWalker): if self.master.commands.execute("view.properties.inbounds %d" % index): self.master.view.focus.index = index - def get_next(self, pos): - pos = pos + 1 - if not self.master.commands.execute("view.properties.inbounds %d" % pos): + @lru_cache(maxsize=None) + def _get(self, pos: int) -> typing.Tuple[typing.Optional[FlowItem], typing.Optional[int]]: + if not self.master.view.inbounds(pos): return None, None - f = FlowItem(self.master, self.master.view[pos]) - return f, pos + return FlowItem(self.master, self.master.view[pos]), pos + + def get_next(self, pos): + return self._get(pos + 1) def get_prev(self, pos): - pos = pos - 1 - if not self.master.commands.execute("view.properties.inbounds %d" % pos): - return None, None - f = FlowItem(self.master, self.master.view[pos]) - return f, pos + return self._get(pos - 1) class FlowListBox(urwid.ListBox, layoutwidget.LayoutWidget):