new-style class はえー

Python のドキュメントビルドシステムになってる Sphinx + docutils を最適化中。以下はPython の trunk で html ドキュメントをビルドするのにかかった時間 @ Core2 Duo 6300 (1.86GHz)

# 最適化前
333.76user 64.36system 6:39.46elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k

# 最適化後
184.43user 33.65system 3:38.23elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k

半分近くまで減らせた。今試して驚いたのが、 new-style class の効果。 node tree を traverse して isinstance が True ならなんたら・・・という処理が時間食ってるのを何とかするまえに、ちょっと Node を new-style class にしてみたら、想像以上に効いた。

# new-style class
   Ordered by: internal time
   List reduced from 2793 to 5 due to restriction <5>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
8781378/19837   22.869    0.000   30.331    0.002 tools/sphinx/util/__init__.py:427(_fast_traverse)
      460   11.858    0.026   11.864    0.026 {cPickle.dump}
 13752662   11.641    0.000   12.150    0.000 {isinstance}

# old-style class
   Ordered by: internal time
   List reduced from 2793 to 3 due to restriction <3>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 13697710   24.040    0.000   24.393    0.000 {isinstance}
8781378/19837   23.149    0.000   41.934    0.002 tools/sphinx/util/__init__.py:427(_fast_traverse)

isinstance の時間が、Node以外に対するisinstanceも少しは混じってるはずなのに、半分以下になってる。
もう old-style class は使わない。

このブログに乗せているコードは引用を除き CC0 1.0 で提供します。