Skip to content

Speed up unpacking incoming packet data - #1076

Merged
bdraco merged 5 commits into
python-zeroconf:masterfrom
bdraco:speed_up_unpacks
Jun 14, 2022
Merged

bdraco merged 5 commits into
python-zeroconf:masterfrom
bdraco:speed_up_unpacks

Conversation

@bdraco

@bdraco bdraco commented Jun 14, 2022 •

Copy link
Copy Markdown
Member

use struct.Struct to avoid building the unpacks each call as it saves a non-trivial amount of work especially with small values.

This makes the most difference if the rest of the program is doing > 100 different unpacks since it will avoid the case were the cache is exhausted

https://github.com/python/cpython/blob/f9585e2adc36fb2886f96ef86a5aa14215fc1151/Modules/_struct.c#L2103

bdraco added 2 commits June 13, 2022 21:37
use struct.Struct to avoid building the unpacks each call
use struct.Struct to avoid building the unpacks each call
@codecov-commenter

codecov-commenter commented Jun 14, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #1076 (3cd3871) into master (1aa7842) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1076   +/-   ##
=======================================
  Coverage   99.92%   99.92%           
=======================================
  Files          23       23           
  Lines        2557     2559    +2     
  Branches      477      477           
=======================================
+ Hits         2555     2557    +2     
  Misses          1        1           
  Partials        1        1           
Impacted Files Coverage Δ
zeroconf/_protocol/incoming.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1aa7842...3cd3871. Read the comment docs.

@bdraco
bdraco marked this pull request as draft June 14, 2022 08:08
@bdraco

bdraco commented Jun 14, 2022 •

Copy link
Copy Markdown
Member Author

The 3 unsigned short reads in SRV could be a single unpack call

        if type_ == _TYPE_SRV:
            return DNSService(
                domain,
                type_,
                class_,
                ttl,
                self.read_unsigned_short(),
                self.read_unsigned_short(),
                self.read_unsigned_short(),
                self.read_name(),
                self.now,
            )

@bdraco

bdraco commented Jun 14, 2022 •

Copy link
Copy Markdown
Member Author

This reduced parsing by ~19% overall. incoming.py is unfortunately still the number 1 file in the event loop on a mostly idle Home Assistant instance with ~4926 calls to read_name per minute

@bdraco

bdraco commented Jun 14, 2022

Copy link
Copy Markdown
Member Author

This worked out to a 21% speed up overnight, but were still 7.3 million calls to read_name and thats the hot spot here.

@bdraco

bdraco commented Jun 14, 2022

Copy link
Copy Markdown
Member Author

I don't see any other opportunities to improve this. Python 3.11 is really going to be the solution here since its showing almost a 45% speed up in this area.

@bdraco
bdraco marked this pull request as ready for review June 14, 2022 17:38
@bdraco
bdraco merged commit 533ad10 into python-zeroconf:master Jun 14, 2022
@bdraco
bdraco deleted the speed_up_unpacks branch June 14, 2022 17:38
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request Jun 26, 2022
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.

2 participants