console: improve flowlist performance (#5228)

Co-authored-by: BkPHcgQL3V <BkPHcgQL3V@gmx.com>
This commit is contained in:
Maximilian Hils 2022-03-29 19:32:40 +02:00 committed by GitHub
parent 66dd158560
commit 31add1a7c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 12 deletions

View File

@ -2,6 +2,8 @@
## Unreleased: mitmproxy next ## Unreleased: mitmproxy next
* Console Performance Improvements
([#3427](https://github.com/mitmproxy/mitmproxy/issues/3427), @BkPHcgQL3V)
* Add flatpak support to the browser addon * Add flatpak support to the browser addon
([#5200](https://github.com/mitmproxy/mitmproxy/issues/5200), @pauloromeira) ([#5200](https://github.com/mitmproxy/mitmproxy/issues/5200), @pauloromeira)
* Add example addon to dump contents to files based on a filter expression * Add example addon to dump contents to files based on a filter expression

View File

@ -1,8 +1,11 @@
import typing
from functools import lru_cache
import urwid import urwid
import mitmproxy.tools.console.master
from mitmproxy.tools.console import common from mitmproxy.tools.console import common
from mitmproxy.tools.console import layoutwidget from mitmproxy.tools.console import layoutwidget
import mitmproxy.tools.console.master
class FlowItem(urwid.WidgetWrap): class FlowItem(urwid.WidgetWrap):
@ -40,6 +43,7 @@ class FlowItem(urwid.WidgetWrap):
class FlowListWalker(urwid.ListWalker): class FlowListWalker(urwid.ListWalker):
master: "mitmproxy.tools.console.master.ConsoleMaster"
def __init__(self, master): def __init__(self, master):
self.master = master self.master = master
@ -47,13 +51,14 @@ class FlowListWalker(urwid.ListWalker):
def positions(self, reverse=False): def positions(self, reverse=False):
# The stub implementation of positions can go once this issue is resolved: # The stub implementation of positions can go once this issue is resolved:
# https://github.com/urwid/urwid/issues/294 # 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: if reverse:
return reversed(ret) return reversed(ret)
return ret return ret
def view_changed(self): def view_changed(self):
self._modified() self._modified()
self._get.cache_clear()
def get_focus(self): def get_focus(self):
if not self.master.view.focus.flow: 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): if self.master.commands.execute("view.properties.inbounds %d" % index):
self.master.view.focus.index = index self.master.view.focus.index = index
def get_next(self, pos): @lru_cache(maxsize=None)
pos = pos + 1 def _get(self, pos: int) -> typing.Tuple[typing.Optional[FlowItem], typing.Optional[int]]:
if not self.master.commands.execute("view.properties.inbounds %d" % pos): if not self.master.view.inbounds(pos):
return None, None return None, None
f = FlowItem(self.master, self.master.view[pos]) return FlowItem(self.master, self.master.view[pos]), pos
return f, pos
def get_next(self, pos):
return self._get(pos + 1)
def get_prev(self, pos): def get_prev(self, pos):
pos = pos - 1 return self._get(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
class FlowListBox(urwid.ListBox, layoutwidget.LayoutWidget): class FlowListBox(urwid.ListBox, layoutwidget.LayoutWidget):