Hi @pelme, I know this is unsolicited, but I thought I'd put it out there for you to take a look.
I noticed in some benchmarking that the isinstance(x, str | _HasHtml) line was taking up a lot of the runtime on the benchmark. I replaced it with a hasattr call (to check for the __html__ attribute), which turned out to be faster.
There's definitely a tradeoff here, because now we've got this awkward hasattr in the if, elif, elif checks, whereas all the others are checking isinstance. I also am not 100% sure here on all of the ramifications of checking for the __html__ attribute.
I'll leave it up to you if you think the optimization here is worth it. I don't have a great feel for the usage of this library to understand how important this codepath is, or how much this would help in real world usage...just thought I'd put it out there for your look.
Thanks for making a great abstraction for html in python!
I need to do a little more homework on this... I'm not seeing the performance improvements I thought I saw. I must have had something different between the two tests.
Hi @pelme, I know this is unsolicited, but I thought I'd put it out there for you to take a look.
I noticed in some benchmarking that the
isinstance(x, str | _HasHtml)
line was taking up a lot of the runtime on the benchmark. I replaced it with ahasattr
call (to check for the__html__
attribute), which turned out to be faster.There's definitely a tradeoff here, because now we've got this awkward hasattr in the if, elif, elif checks, whereas all the others are checking
isinstance
. I also am not 100% sure here on all of the ramifications of checking for the__html__
attribute.I'll leave it up to you if you think the optimization here is worth it. I don't have a great feel for the usage of this library to understand how important this codepath is, or how much this would help in real world usage...just thought I'd put it out there for your look.
Thanks for making a great abstraction for html in python!
Benchmarks
Prior to change
After Change
Original Results from Line Profiler: