python / python/cpython

Allocate JIT memory in large chunks near the executable

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

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

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

説明

By allocating JIT memory in large chunks we can reduce the overhead of compilation and possibly speed up the compiled code a bit.

Currently, when we need to allocate memory for the JIT, we ask the OS for a sufficiently large chunk of virtual memory. This is simple and reasonably efficient. It does have a few flaws:

  1. We need to create DWARF debug info and register it for each trace
  2. We need to make a syscall to get memory for each trace
  3. We have no control over the location of the jitted code, meaning that calls in the executable may need trampolines

By allocating large chunks we can reduce the overhead of (1) and (2) to once per-chunk, not per trace.
By allocating large chunks we can also afford the additional overhead of requesting memory near to the executable.

How it would work:

  • When we need memory for jitted code, we request it from our special allocator.
  • When the allocator needs memory, it requests it from the OS, making several requests for it near the executable before accepting an location
  • The allocator itself will be a standard obmalloc/jemalloc style block allocator
Size classes and fragmentation:

All jitted code will need to page aligned, so blocks will need to a multiple of the page size.
With 4 size classes per power of 2 increase in size (like jemalloc) and assuming 2M chunks we get relatively little internal fragmentation, but potential quite a lot of external fragmentation as there would be around 20 size classes.
The external fragmentation can be mitigated by allowing the OS to lazily allocate the pages on demand.

API

The _PyObject_VirtualAlloc function will need extending (or a new function added) to allow the desired address to be passed, so the allocator can get blocks near the executable.

The jit_alloc function's API will be unchanged.

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

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

はじめの一歩

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

調査の方向性

まず、_PyObject_VirtualAlloc と jit_alloc を経由する JIT の割り当てパスを追跡し、次に既存の仮想メモリ割り当ての動作を確認します。JIT メモリが実行可能ファイルの近くにある大きくページ境界にアラインされたチャンクから取得され、チャンクごとの DWARF 登録が行われ、jit_alloc API が変更されていなければ作業は完了です。

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

評価

技術スタック
c, python
領域
compilers, performance
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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