question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Speed] Redundant generic and protocol checks

See original GitHub issue

uhohs. @beartype currently exhibits crippling less-than-ideal performance for generics (i.e., user-defined typing.Generic subclasses) and protocols (i.e., user-defined typing.Protocol subclasses). As @posita publicly exposed in this hard-hitting journalist expose, this is because @beartype currently generates redundant isinstance() checks for these subclasses: e.g.,

if not (
    isinstance(1, numerary.types.SupportsComplexOps)  # <-- this is fine
    and
    isinstance(1, numerary.types._SupportsComplexOps)  # <-- this is badness
):

While technically fine (for various hand-wavy definitions of fine), this redundancy reduces runtime efficiency for generics and protocols involved in complex inheritance hierarchies that make my head spin with dysphoric nausea like last year’s eggnog bender. Just like Santa hopped up on too much Christmas hopium, beartype 0.10.0 promises to fix all the badness.

Let’s keep our promises, @beartype. The reindeer with the blinking nose is depending on us! 🎅

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
positacommented, Dec 24, 2021

OMG, this is so much better! I noticed one teensy weensy little issue, though. 👌😬 For some reason, the update is getting confused where there there are variadic keyword arguments. Consider:

from beartype import beartype

@beartype
def okay(okay_a, okay_b: int = 0, okay_c: str = "") -> None:
    pass

@beartype
def okay_too(okay_too_a, okay_too_b: int = 0, okay_too_c: str = "", *args) -> None:
    pass

@beartype
def also_okay(also_okay_a, also_okay_b: int = 0, *, also_okay_c: str = "") -> None:
    pass

@beartype
def whoops(whoops_a, whoops_b: int = 0, whoops_c: str = "", **kw) -> None:
    pass

@beartype
def whoops_too(whoops_too_a, whoops_too_b: int, whoops_too_c: str, **kw) -> None:
    pass

def test_nothing() -> None:
    assert True, f"Nothing to see here!"

The @beartype decorator will choke when decorating whoops and whoops_too.

That being said, with some casual testing (selecting from some of dyce’s unit tests), I got the following before and after your change:

No Bear Old Bear Merry Christmas Bear
3.5875 s 10.3075 s 4.92 s 💨💥

Nice! I now have a hope of landing my no-more-short-circuiting-unions branch!

1reaction
leyceccommented, Dec 28, 2021

Awwwwwwwww! What a wonderfully exuberant outpouring of goodwill, festive cheer, and gr8-m8-i-r8-8/8 humour. I really kinda needed this, too. My stepmom was just diagnosed with Stage IV Stomach Cancer (so: terminal, debilitating, and excruciating) the day before Christmas Eve. She’s a delightful woman and I love her to bits. My wife is… well, devastated.

I’m trying not to think about physical reality for a bit by instead beating up manscaped yakuza in Yakuza. But you can only punch so many digital criminal underworld cretins in their plasticky uncanny-valley faces before Ugly Ol’ Reality starts peering over your shoulder and whispering an unintelligible (yet predictably disturbing) catechism in a creepy monotone. And I’m like:

Come on. Fess up. Who invited Reality to the party, guys?

Thank you for the cuddly chuckles. And you’re most welcome, of course! @beartype is here to service numerary’s every need – especially that need for blistering speed that transgresses every local codebase traffic law. Clearly, we’re not quite there yet; we’d love @beartype-checked calls to quietly inch within 1.25× or even 1.10× of unchecked calls.

We’re confident that our next optimization sprint will lift us up to those lofty heights. Sadly, we didn’t get there for the beartype 0.10.0 release cycle. I hope to push that out the ice-encrusted cabin door by New Year’s – even though the codebase is mewling with big round eyes for attention, chocolate cookies, and warm hugs. Too bad, bear! You must leave this den.

Urgh! So close to perfection. Yet so far from destiny. 🪐

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redundant conformance of 'Generic' to protocol 'Equatable' in ...
So declaring Object: Equatable is redundant. However, the conformance does not mean that it has been implemented properly ( NSObject just checks ......
Read more >
First Hop Redundancy Protocols Configuration Guide, Cisco ...
BFD provides fast BFD peer failure detection times independently of all media types, encapsulations, topologies, and routing protocols such as, ...
Read more >
Selection of Cyclic Redundancy Code and Checksum ... - FAA
Another common selection criterion is computational performance, with checksums often presumed to be calculated much faster than CRCs. However, research into ...
Read more >
Protocol-oriented programming & generics | Swift for TensorFlow
This tutorial will go over protocol-oriented programming, and different examples of how they can be used with generics in day-to-day ...
Read more >
Formalizing Swift generics as a term rewriting system
The full set of supported queries is the following: Checking if a type conforms to a protocol. Listing all protocols a type conforms...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found