Description
Closing windows is slow. Example:
import time
import vanilla
class Test:
def __init__(self):
count = 24
self.w = vanilla.Window(
(200, 10 + (30 * count)),
"Test"
)
y = 10
for i in range(count):
button = vanilla.Button(
(10, y, -10, 20),
"w.close",
callback=self.button1Callback
)
setattr(self.w, f"button{i}", button)
y += 30
self.w.open()
def button1Callback(self, sender):
s = time.time()
self.w.close()
t = time.time() - s
print("w.close", t)
try:
import mojo
Test()
except:
from vanilla.test.testTools import executeVanillaTest
executeVanillaTest(Test)
This is based on the "Align and Distribute" window in my Pop Up Tools extension, so this is a real world example. This takes about 0.7 seconds to close on my old iMac. This makes the UI feel very laggy.
I've tracked this down to the use of hasattr
in the ultimate _breakCycles
function. This is called once for every single view in a window, thus the more views a window has, the slower it becomes. I'm trying to find a way to speed this up but the best I'm coming up with are super hacky things like:
Edit: This code isn't correct. I'm sketching some new options.
# def _breakCycles(view):
# """
# Break cyclic references by deleting _target attributes.
# """
# if "setVanillaWrapper_" in view.__class__.__dict__:
# try:
# view._breakCycles()
# except AttributeError:
# pass
# for view in view.subviews():
# _breakCycles(view)
This reduces the _breakCycles time by about 40%, so it's still not great. It seems that a lot of time is wasted when the try:
fails. I wonder if we could leverage getNSSubclass to give us a faster way to determine if _breakCycles
needs to be called. Maybe we'd keep a set of class names that have gone through getNSSubclass
that has a _breakCycles
?