Skip to content

invert QTransform using adjoint() and determinant()#1493

Merged
j9ac9k merged 2 commits intopyqtgraph:masterfrom
pijyoi:fastinv
Jan 13, 2021
Merged

invert QTransform using adjoint() and determinant()#1493
j9ac9k merged 2 commits intopyqtgraph:masterfrom
pijyoi:fastinv

Conversation

@pijyoi
Copy link
Copy Markdown
Contributor

@pijyoi pijyoi commented Jan 9, 2021

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.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jan 9, 2021

With the speedup in inversion, cache lookup runs slower than always computing the inversion.

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.

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Jan 9, 2021

I get the following results with the script included below:
Finished cache lookup: 16.0494 ms
Finished direct compute: 7.2663 ms

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")

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jan 9, 2021

...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.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jan 9, 2021

Ok, that was unexpected;

  Finished cache lookup: 8.2791 ms
  Finished direct compute: 6.0091 ms

I think this PR should strip the cache lookup functionality unless we can identify some circumstance where it is beneficial.

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Jan 9, 2021

Maybe that should be a separate PR, ideally by the author (@erikmansson) of #1216.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jan 9, 2021

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.

@erikmansson
Copy link
Copy Markdown
Contributor

Please feel free to remove the cache again if the new method is quicker. I have no objections. 😊

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jan 13, 2021

@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.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Jan 13, 2021

Thanks for the PR @pijyoi thanks for chiming in @erikmansson , merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants