invert QTransform using adjoint() and determinant()#1493
invert QTransform using adjoint() and determinant()#1493j9ac9k merged 2 commits intopyqtgraph:masterfrom
Conversation
Well, that's interesting ... do you suggest effectively undoing #1216 ? EDIT: I may attempt to profile on my couple of year old macbook air, it's the slowest PC I have easy access to setup a development environment to. We probably should verify those benchmarks with slower hardware. |
|
I get the following results with the script included below: import fileinput
import numpy as np
import PySide2
from PySide2 import QtGui
from pyqtgraph.util.lru_cache import LRUCache
from pyqtgraph.debug import Profiler
_mapRectFromViewGlobalCache = LRUCache(100, 70)
def invertQTransform(tr):
try:
det = tr.determinant()
detr = 1.0 / det
inv = tr.adjoint()
inv *= detr
return inv
except ZeroDivisionError:
raise
def cache_lookup(vt):
cache = _mapRectFromViewGlobalCache
k = (
vt.m11(), vt.m12(), vt.m13(),
vt.m21(), vt.m22(), vt.m23(),
vt.m31(), vt.m32(), vt.m33(),
)
try:
inv_vt = cache[k]
except KeyError:
inv_vt = invertQTransform(vt)
cache[k] = inv_vt
return inv_vt
transforms = [eval(line.strip()) for line in fileinput.input("transforms.txt")]
# use a size that results in always getting a cache hit
transforms = transforms[:50]
# populate cache
for tr in transforms:
cache_lookup(tr)
profiler = Profiler(disabled=False, delayed=False)
for _ in range(100):
[cache_lookup(tr) for tr in transforms]
profiler("Finished cache lookup")
for _ in range(100):
[invertQTransform(tr) for tr in transforms]
profiler("Finished direct compute") |
|
...hard to believe the unpacking of the QTransform takes so long....thanks for the code above, I'll try and benchmark on the macbook air tomorrow. |
|
Ok, that was unexpected; I think this PR should strip the cache lookup functionality unless we can identify some circumstance where it is beneficial. |
|
Maybe that should be a separate PR, ideally by the author (@erikmansson) of #1216. |
|
I'll give @erikmansson some time to respond, but I think removing the cache hit can be in the same PR, provided it's in a different commit (and we don't squash on merge); of course alternatively it can just be a different PR. |
|
Please feel free to remove the cache again if the new method is quicker. I have no objections. 😊 |
This reverts commit 55e1f2c.
|
@pijyoi I was right about to merge, you pushed the commit when I was ~5 seconds away from pushing the button :P no matter... thanks for updating the PR ... I'll merge when CI finishes up. |
|
Thanks for the PR @pijyoi thanks for chiming in @erikmansson , merging |
This PR is a result of discussions made in #1418.
It speeds up QTransform inversion using Qt routines adjoint() and determinant() rather than inverted().
The issue with inverted() is that it uses fuzzy floating point to classify matrix types in order to take faster inversion codepaths.
In addition, this PR would make #1216 redundant..
With the speedup in inversion, cache lookup runs slower than always computing the inversion.