Google App Engineのアンチパターン(3) Greedy module loading(貪欲なモジュール読み込み)

Google App Engine Anti Patterns by Takashi Matsuo on Prezi」より。
なお、この資料はとても参考になるので、一度目を通しておくといいと思います。

Google App Engineのアンチパターン(3) Greedy module loading(貪欲なモジュール読み込み)

「一部でしか使わないモジュールは遅延ロードする」

from kay.utils import render_to_response

import hoge_utils
import fuga_utils

def index(request):
  return render_to_response('myapp/index.html', {'message': 'Hello'})

def hoge(request):
  entries = hoge_utils.get_entries()
  return render_to_response('myapp/hoge.html', {'entries': entries})

def fuga(request):
  entries = fuga_utils.get_entries()
  return render_to_response('myapp/hoge.html', {'entries': entries})

hoge_utils、fuga_utilsはそれぞれ関数hoge()、fuga()の中でしか使用されていません。
使用しないモジュールを読み込むという、不要な処理が行われています。

修正例

from kay.utils import render_to_response

def index(request):
  return render_to_response('myapp/index.html', {'message': 'Hello'})

def hoge(request):
  import hoge_utils
  entries = hoge_utils.get_entries()
  return render_to_response('myapp/hoge.html', {'entries': entries})

def fuga(request):
  import fuga_utils
  entries = fuga_utils.get_entries()
  return render_to_response('myapp/hoge.html', {'entries': entries})

一部でしか使わないモジュールは使用されるときに読み込むようにします。
こうすることで、使用されないモジュールを読み込むことがなくなりました。

アプリケーションが大きくなるにつれて、この差が大きくなっていくように思います。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください