René's URL Explorer Experiment


Title: Quadratic performance of h2.connection.H2Connection._open_streams · Issue #1184 · python-hyper/h2 · GitHub

Open Graph Title: Quadratic performance of h2.connection.H2Connection._open_streams · Issue #1184 · python-hyper/h2

X Title: Quadratic performance of h2.connection.H2Connection._open_streams · Issue #1184 · python-hyper/h2

Description: First let me say this is an excellent library! Thanks for taking the time to maintain/update it. While writing an http2 client library for tornado (I can hopefully share it soon ;) ), I ran into very poor performance while benchmarking t...

Open Graph Description: First let me say this is an excellent library! Thanks for taking the time to maintain/update it. While writing an http2 client library for tornado (I can hopefully share it soon ;) ), I ran into ve...

X Description: First let me say this is an excellent library! Thanks for taking the time to maintain/update it. While writing an http2 client library for tornado (I can hopefully share it soon ;) ), I ran into ve...

Opengraph URL: https://github.com/python-hyper/h2/issues/1184

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.114.3


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Quadratic performance of h2.connection.H2Connection._open_streams","articleBody":"First let me say this is an excellent library! Thanks for taking the time to maintain/update it.\r\n\r\nWhile writing an http2 client library for tornado (I can hopefully share it soon ;) ), I ran into very poor performance while benchmarking the client with many concurrently open streams (5000+). Here's a cProfile output for starting/finishing 5000 concurrent requests:\r\n\r\n```\r\n19128647 function calls (19069074 primitive calls) in 18.635 seconds\r\n   Ordered by: internal time\r\n11113314    4.002    0.000    4.002    0.000 stream.py:826(open)\r\n5101    3.571    0.001    9.150    0.002 connection.py:392(_open_streams)\r\n21279    1.580    0.000    1.580    0.000 {method 'items' of 'dict' objects}\r\n34357    0.464    0.000    0.464    0.000 {method 'write' of '_ssl._SSLSocket' objects}\r\n    ...\r\n```\r\n\r\nAs you can see, almost half of the run time is spent in `_open_streams`\r\n\r\nHere's the function code for reference:\r\n\r\n  ```\r\n  def _open_streams(self, remainder):\r\n        \"\"\"\r\n        A common method of counting number of open streams. Returns the number\r\n        of streams that are open *and* that have (stream ID % 2) == remainder.\r\n        While it iterates, also deletes any closed streams.\r\n        \"\"\"\r\n        count = 0\r\n        to_delete = []\r\n\r\n        for stream_id, stream in self.streams.items():\r\n            if stream.open and (stream_id % 2 == remainder):\r\n                count += 1\r\n            elif stream.closed:\r\n                to_delete.append(stream_id)\r\n\r\n        for stream_id in to_delete:\r\n            stream = self.streams.pop(stream_id)\r\n            self._closed_streams[stream_id] = stream.closed_by\r\n\r\n        return count\r\n```\r\n\r\nThe function loops through all streams, and returns the count of all open remote/local streams. As self.max_outbound_streams is called on each stream open in `send_headers`, the performance of this function is quadratic in the number of concurrently open streams over the lifetime of the program.\r\n\r\nAnd to check the numbers, `stream.py's` `open()` is called per element in `self.streams`. It was called `11,113,314` times. The benchmark was run with 5000 concurrent streams, and since `open()` is called on opening a stream, the runtime is `O(1 + 2 + ... + n) =~ (n * (n-1)) / 2` in the worst case if all streams remain open during this time. If we insert 5000 into that formula we get `12,497,500` which is within 10% of the function calls we got from the cProfile output.\r\n\r\nProposal: Update H2Connection to incrementally update the count of open local/remote streams, to make the function call O(1).\r\n\r\nThe logic here being that we are processing all events, so we know when we're processing the event if the stream should be closed/open, and can update counts incrementally when there is a state change. I'm not super familiar with the code base, but we should also be able to clean up `closed` streams on state change as well, or else make some other process for cleaning up close streams from `self.streams`.\r\n\r\nI'm happy to implement the change, just let me know if this is a path you'd be willing to go down, or if there is anything that makes this infeasible (totally possible! Started using this library two days ago :) )\r\n\r\n","author":{"url":"https://github.com/kahuang","@type":"Person","name":"kahuang"},"datePublished":"2019-02-17T00:43:08Z","url":"https://github.com/python-hyper/h2/issues/1184"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4fe0b009-2b95-c528-d106-17bd04cc8f08
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8F4A:21BCEF:D40DB2:12ACB8A:6B06EF9D
html-safe-nonce3e5c9393913a8312758cb82c53a6a96ca7d400b3475d20614cdab263579dbac0
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjRBOjIxQkNFRjpENDBEQjI6MTJBQ0I4QTo2QjA2RUY5RCIsInZpc2l0b3JfaWQiOiIzMDA2MzYxNDAyNTc2NjYyNDI5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac9d66145ae08281615da630f0006d923749bfaa808db3f0bcd659ac367e1b9a77
hovercard-subject-tagissue:411132203
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-hyper/h2/1184/issue_layout
twitter:imagehttps://opengraph.githubassets.com/dc05b91e8df1ab38dfb701810fcd157fe3a1f792899cb6f0a13c1d7c21871a4e/python-hyper/h2/issues/1184
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/dc05b91e8df1ab38dfb701810fcd157fe3a1f792899cb6f0a13c1d7c21871a4e/python-hyper/h2/issues/1184
og:image:altFirst let me say this is an excellent library! Thanks for taking the time to maintain/update it. While writing an http2 client library for tornado (I can hopefully share it soon ;) ), I ran into ve...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamekahuang
hostnamegithub.com
expected-hostnamegithub.com
None351ea5ca38a5758919d44ee52c70fe9003c0c2dfa20f04effd7fac0c5330c16b
turbo-cache-controlno-preview
go-importgithub.com/python-hyper/h2 git https://github.com/python-hyper/h2.git
octolytics-dimension-user_id13035907
octolytics-dimension-user_loginpython-hyper
octolytics-dimension-repository_id37996233
octolytics-dimension-repository_nwopython-hyper/h2
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id37996233
octolytics-dimension-repository_network_root_nwopython-hyper/h2
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release891bde3f438884e9c6c7ccd826f33805910a978b
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python-hyper/h2/issues/1184#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-hyper%2Fh2%2Fissues%2F1184
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-hyper%2Fh2%2Fissues%2F1184
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python-hyper%2Fh2
Reloadhttps://github.com/python-hyper/h2/issues/1184
Reloadhttps://github.com/python-hyper/h2/issues/1184
Reloadhttps://github.com/python-hyper/h2/issues/1184
python-hyperhttps://github.com/python-hyper
h2https://github.com/python-hyper/h2
Codehttps://github.com/python-hyper/h2
Issues21 (21)https://github.com/python-hyper/h2/issues
Pull requests23 (23)https://github.com/python-hyper/h2/pulls
Actionshttps://github.com/python-hyper/h2/actions
Projectshttps://github.com/python-hyper/h2/projects
Wikihttps://github.com/python-hyper/h2/wiki
Security and quality2 (2)https://github.com/python-hyper/h2/security
Insightshttps://github.com/python-hyper/h2/insights
https://github.com/python-hyper/h2
New issuehttps://github.com/login?return_to=https://github.com/python-hyper/h2/issues/1184
New issuehttps://github.com/login?return_to=https://github.com/python-hyper/h2/issues/1184
#1185https://github.com/python-hyper/h2/pull/1185
Quadratic performance of h2.connection.H2Connection._open_streamshttps://github.com/python-hyper/h2/issues/1184#top
#1185https://github.com/python-hyper/h2/pull/1185
https://github.com/kahuang
kahuanghttps://github.com/kahuang
on Feb 17, 2019https://github.com/python-hyper/h2/issues/1184#issue-411132203
https://github.com/kahuang
kahuanghttps://github.com/kahuang
on Feb 17, 2019https://github.com/python-hyper/h2/issues/1184#issuecomment-464505373
#1185https://github.com/python-hyper/h2/pull/1185
https://github.com/pgjones
pgjoneshttps://github.com/pgjones
on Mar 2, 2019https://github.com/python-hyper/h2/issues/1184#issuecomment-468923850
https://github.com/kahuang
kahuanghttps://github.com/kahuang
on Mar 2, 2019https://github.com/python-hyper/h2/issues/1184#issuecomment-468943658
https://github.com/kahuang
kahuanghttps://github.com/kahuang
on Mar 20, 2019https://github.com/python-hyper/h2/issues/1184#issuecomment-475062395
@pgjoneshttps://github.com/pgjones
https://github.com/dimaqq
dimaqqhttps://github.com/dimaqq
on Mar 23, 2020https://github.com/python-hyper/h2/issues/1184#issuecomment-602455272
https://user-images.githubusercontent.com/662249/77296991-f898b480-6d2b-11ea-903f-b296a07b1697.png
Sign up for freehttps://github.com/signup?return_to=https://github.com/python-hyper/h2/issues/1184
Sign in to commenthttps://github.com/login?return_to=https://github.com/python-hyper/h2/issues/1184
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.