python / python/cpython

__debug__ enhancement and parse time constants

オープン
#100,339 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

In the spirit of the efforts being made by the faster-cpython team to improve performance, I think there is some low hanging fruit it terms of improving runtime performance by... not running code in the first place, since the fastest code is the one that never runs!

The lowest of all the hanging fruits would be turning debug into an int, allowing it to take on multiple values and as such allowing multiple debug levels to checked for. This would require minimal change to the codebase and command line option to support a number after the -O argument and the same number being stored for the .opt part of the .pyc files, i.e. -O3 would replace __debug__ with 3 instead of the current bool approach. This approach would allow developers to write as much debug code as they need, while also having it get compiled out completely when it's no needed, as opposed to the current approach of toggling if 0: blocks everywhere or commenting out the different tiers of debug code when not in use.

if __debug__:
    print("Debug mode")
if __debug__ >= 2:
    print("Verbose debug mode")
if __debug__ >= 3:
    print("Paranoid debug mode")

And while that would already bring a lot of value by itself with minimal changes, the natural extension to it is actual compile time constants, i.e. __debug__ but with different names, which would make things a lot more readable. This fruit is a few branches higher on the tree. One approach to implementing these compile time constants would be to add a const keyword which declares all the compile time constants used in the file, which would be used for syntax error purposes. And during compilation they would get turned into an actual constant similarly to __debug__, with the caveat that consts would evaluate to False if not explicitly defined. As to how they would be defined, might as well use the -O option again, i.e. -OFOO would define FOO as True during compilation, i.e.:

const FOO, BAZ

# Only one of the code paths should remain, based on whether -OFOO was passed when running python
if FOO:
    print("Foo defined")
else:
    print("Foo not defined")

# If not declared by -OBAZ, should be treated as False and the if should be compiled out completely
if BAZ:
    print("We got baz")

This change would also require changes to the .pyc files, either by storing a hash/all the compile time constants used in the file itself, or just generating a completely different .pyc with a hash of all the compile time constants in place of the .opt part of the filename.


And the natural extension to that would be supporting integer constants similar to the debug extension, or better yet and builtin type -- int, float or string (constants only of course). These again require a change to the command line option in the form of -OFOO=3, but if the previous two changes are implemented, shouldn't require too much compiler changes.


All of these changes combined would more of less bring python on par with C's #if/#ifdef statements and allow for copious amounts of debug code to remain in the codebases with zero cost for the (optimized) runtime, while only incurring a small overhead for the startup (unless already cached).

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、CPython の -O コマンドライン処理、debug のコンパイル、そして issue で説明されている .pyc .opt メタデータを追跡します。提案されているデバッグレベル、宣言された compile-time 定数、型付き定数を、コンパイラーとキャッシュの要件と比較します。完了とするには、合意されたスコープと、受け入れられた動作の実装が必要です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
20/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。