Title: Use PEP 669 API for cprofile · Issue #103533 · python/cpython · GitHub
Open Graph Title: Use PEP 669 API for cprofile · Issue #103533 · python/cpython
X Title: Use PEP 669 API for cprofile · Issue #103533 · python/cpython
Description: Feature or enhancement Replace the current setprofile mechanism with PEP 669 API for cProfile. Pitch It's faster. Before: Hanoi: 0.342869599997357, 1.4579414000036195, 4.252174587699983 fib: 0.15549560000363272, 0.7588815999988583, 4.880...
Open Graph Description: Feature or enhancement Replace the current setprofile mechanism with PEP 669 API for cProfile. Pitch It's faster. Before: Hanoi: 0.342869599997357, 1.4579414000036195, 4.252174587699983 fib: 0.1554...
X Description: Feature or enhancement Replace the current setprofile mechanism with PEP 669 API for cProfile. Pitch It's faster. Before: Hanoi: 0.342869599997357, 1.4579414000036195, 4.252174587699983 fib: 0....
Opengraph URL: https://github.com/python/cpython/issues/103533
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use PEP 669 API for cprofile","articleBody":"# Feature or enhancement\r\n\r\nReplace the current `setprofile` mechanism with PEP 669 API for cProfile.\r\n\r\n# Pitch\r\n\r\nIt's faster.\r\n\r\nBefore:\r\n```\r\nHanoi: 0.342869599997357, 1.4579414000036195, 4.252174587699983\r\nfib: 0.15549560000363272, 0.7588815999988583, 4.88040561907301\r\n\r\nHanoi: 0.33156009999947855, 1.394542599999113, 4.2060024713507635\r\nfib: 0.16397210000286577, 0.7469802000050549, 4.555532313070332\r\n\r\nHanoi: 0.3341937000004691, 1.394005099995411, 4.171248889471747\r\nfib: 0.15704140000161715, 0.7399598000047263, 4.711877250184387\r\n\r\nHanoi: 0.33133889999589883, 1.3821628000005148, 4.171447421409387\r\nfib: 0.15387790000386303, 0.754370700000436, 4.902397940064804\r\n\r\nHanoi: 0.3403002000050037, 1.3969628000049852, 4.105089564991276\r\nfib: 0.15468130000226665, 0.761441399998148, 4.922646758121312\r\n```\r\n\r\nAfter:\r\n```\r\nHanoi: 0.3318088000014541, 1.2147841000041808, 3.661096691826309\r\nfib: 0.15522490000148537, 0.6360437999974238, 4.097562955372092\r\n\r\nHanoi: 0.33085879999998724, 1.1502422000048682, 3.476535005279934\r\nfib: 0.15410060000431258, 0.6056611999956658, 3.9302974808580635\r\n\r\nHanoi: 0.35002540000277804, 1.145935599997756, 3.273864125256799\r\nfib: 0.1540030999967712, 0.5974198000039905, 3.8792712615299036\r\n\r\nHanoi: 0.3338971999983187, 1.1459307999975863, 3.431986851052829\r\nfib: 0.16891020000184653, 0.6197690999979386, 3.6692224625343126\r\n\r\nHanoi: 0.3318254999976489, 1.1875411000000895, 3.578812056362467\r\nfib: 0.1544136999946204, 0.5971600999982911, 3.867274082669449\r\n```\r\n\r\n20%+ speed up for overhead.\r\n\r\nI guess the incentive is not in doubt, but I did have some issues when I implemented it.\r\n\r\n1. There is a very slight backward compatibility issue, or it's more like a design decision. The profiler has to disable its own profiling now.\r\n\r\n```python\r\n# This works on main, but I don't think that's intuitive.\r\npr = cProfile.Profile()\r\npr.enable()\r\npr = cProfile.Profile()\r\npr.disable()\r\n```\r\n\r\nWe can make it work as before, I just think this is not the right way to do it with PEP 669. Because of this, I changed an old (15 years) test in `test_cprofile`.\r\n\r\n2. We need to get the actual c function from the descriptor, for which I used the code in the current legacy tracing. However, `_PyInstrumentation_MISSING` is not exposed so I had to store it locally (keep reading it from `sys` is a bit expensive).\r\n\r\n3. On that matter, are we going to expose some APIs on C? It would be nice if I don't have to get `sys.monitoring` and do stuff from there. We have some defined constants but some APIs could be handy. We may be able to reduce the overhead a bit if we have an interface like `PyEval_SetProfile`.\r\n\r\nAddendum:\r\n\u003cdetails\u003e\r\n\u003csummary\u003e Benchmark Code \u003c/summary\u003e\r\n\r\n```python\r\nimport timeit\r\n\r\nhanoi_setup = \"\"\"\r\nimport cProfile\r\ndef test():\r\n def TowerOfHanoi(n, source, destination, auxiliary):\r\n if n == 1:\r\n return\r\n TowerOfHanoi(n - 1, source, auxiliary, destination)\r\n TowerOfHanoi(n - 1, auxiliary, destination, source)\r\n TowerOfHanoi(16, \"A\", \"B\", \"C\")\r\npr = cProfile.Profile()\r\n\"\"\"\r\n\r\nfib_setup = \"\"\"\r\nimport cProfile\r\ndef test():\r\n def fib(n):\r\n if n \u003c= 1:\r\n return 1\r\n return fib(n - 1) + fib(n - 2)\r\n fib(21)\r\npr = cProfile.Profile()\r\n\"\"\"\r\n\r\ntest_baseline = \"\"\"\r\ntest()\r\n\"\"\"\r\n\r\ntest_profile = \"\"\"\r\npr.enable()\r\ntest()\r\npr.disable()\r\n\"\"\"\r\n\r\nbaseline = timeit.timeit(test_baseline, setup=hanoi_setup, number=100)\r\nprofile = timeit.timeit(test_profile, setup=hanoi_setup, number=100)\r\nprint(f\"Hanoi: {baseline}, {profile}, {profile / baseline}\")\r\n\r\nbaseline = timeit.timeit(test_baseline, setup=fib_setup, number=100)\r\nprofile = timeit.timeit(test_profile, setup=fib_setup, number=100)\r\nprint(f\"fib: {baseline}, {profile}, {profile / baseline}\")\r\n```\r\n\u003c/details\u003e\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-103534\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/gaogaotiantian","@type":"Person","name":"gaogaotiantian"},"datePublished":"2023-04-14T06:30:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/103533/cpython/issues/103533"}
| 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:3d663641-5074-dc51-eb29-e81c96bbf3ff |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 85EA:2D943D:1249EEC:199F17B:696A328F |
| html-safe-nonce | 643e541e567f1dc0a63f695257ddf5de158fcc74b6d29358962c46cdb4646e18 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NUVBOjJEOTQzRDoxMjQ5RUVDOjE5OUYxN0I6Njk2QTMyOEYiLCJ2aXNpdG9yX2lkIjoiNzEwMzA5NDY4OTE2MTE2MzQwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c068cd1fdcfb32d52c92b8bb30157cba664bc79ef08cda0d4571510743c2632d |
| hovercard-subject-tag | issue:1667598026 |
| 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/cpython/103533/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dd1ffa43b8bcb0358b9d27ebd8aee8287fa53491e51c9628b69a1dd22035e505/python/cpython/issues/103533 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dd1ffa43b8bcb0358b9d27ebd8aee8287fa53491e51c9628b69a1dd22035e505/python/cpython/issues/103533 |
| og:image:alt | Feature or enhancement Replace the current setprofile mechanism with PEP 669 API for cProfile. Pitch It's faster. Before: Hanoi: 0.342869599997357, 1.4579414000036195, 4.252174587699983 fib: 0.1554... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gaogaotiantian |
| hostname | github.com |
| expected-hostname | github.com |
| None | 321736bfdb3f591415ae895a0459bec204b26a76caf47ba5c980634cfacc4538 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 7a9163cefd1ea4bd06f8eb7c082f43e4e53f626f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width