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
Domain: github.com
IPv4 address: 140.82.114.3
{"@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-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4fe0b009-2b95-c528-d106-17bd04cc8f08 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F4A:21BCEF:D40DB2:12ACB8A:6B06EF9D |
| html-safe-nonce | 3e5c9393913a8312758cb82c53a6a96ca7d400b3475d20614cdab263579dbac0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjRBOjIxQkNFRjpENDBEQjI6MTJBQ0I4QTo2QjA2RUY5RCIsInZpc2l0b3JfaWQiOiIzMDA2MzYxNDAyNTc2NjYyNDI5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9d66145ae08281615da630f0006d923749bfaa808db3f0bcd659ac367e1b9a77 |
| hovercard-subject-tag | issue:411132203 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-hyper/h2/1184/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dc05b91e8df1ab38dfb701810fcd157fe3a1f792899cb6f0a13c1d7c21871a4e/python-hyper/h2/issues/1184 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dc05b91e8df1ab38dfb701810fcd157fe3a1f792899cb6f0a13c1d7c21871a4e/python-hyper/h2/issues/1184 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kahuang |
| hostname | github.com |
| expected-hostname | github.com |
| None | 351ea5ca38a5758919d44ee52c70fe9003c0c2dfa20f04effd7fac0c5330c16b |
| turbo-cache-control | no-preview |
| go-import | github.com/python-hyper/h2 git https://github.com/python-hyper/h2.git |
| octolytics-dimension-user_id | 13035907 |
| octolytics-dimension-user_login | python-hyper |
| octolytics-dimension-repository_id | 37996233 |
| octolytics-dimension-repository_nwo | python-hyper/h2 |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 37996233 |
| octolytics-dimension-repository_network_root_nwo | python-hyper/h2 |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 891bde3f438884e9c6c7ccd826f33805910a978b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width