From kazuhiko @ fdiary.net Sun Sep 7 14:50:23 2003 From: kazuhiko @ fdiary.net (Kazuhiko) Date: Sun, 07 Sep 2003 14:50:23 +0900 Subject: [Mobo-dev] MoBo キャッシュ化の試み Message-ID: かずひこです。 MoBo で、過去の集計をキャッシュさせることで高速化できないか、と考えてい ます。とりあえずやってみた非常にプリミティブな実装のパッチを添付しますが、 後述の理由により、ほとんど速度が変らないか、もしくは遅いかもしれません。 (T T) ○今回のとりあえず実装の解説 Monthly.total ( cond = {} ) にて、cond.inspect な String をキーとするハッ シュに total の計算結果を入れて、yyyymm_cache.db に pstore で格納します。 MoBoBase.save_items にて、yyyymm_cache.db のファイルを消します。 ○今回のとりあえず実装の問題点など Monthly.total が呼ばれるたびに pstore の transaction が発生するので遅く なっている。Monthly.initialize のタイミングでその月の cache の Hash を取 得してそれを使いまわすようにすべきでしょう。とは言っても、Monthly クラス のインスタンスが家計簿データベースとして pstore で格納されるので、 Monthly クラスのインスタンスに cache の Hash を持たせるわけにもいかない ので、Cache クラスを作って MoBo::Cache( yyyymm ) みたいに取得できるよう にすればいいのかな? ご意見などお聞かせくだされば幸いです。 Index: mobo.rb =================================================================== RCS file: /cvsroot/mobo/mobo/mobo.rb,v retrieving revision 1.11.2.15 diff -u -r1.11.2.15 mobo.rb --- mobo.rb 3 Aug 2003 09:34:04 -0000 1.11.2.15 +++ mobo.rb 7 Sep 2003 05:36:36 -0000 @@ -218,8 +218,27 @@ end def total( cond = {} ) + cond_str = cond.inspect + conf = MoBo::Config::new + @cache = PStore::new( "#{conf.data_path}#{month}_cache.db" ) + @cache.transaction do + begin + @cache_total = @cache['mobo'] + rescue + end + @cache_total = Hash::new unless @cache_total + end + if @cache_total[cond_str] then +# STDERR.puts "HIT: #{cond_str} => #{@cache_total[cond_str]}" + return @cache_total[cond_str] + end +# STDERR.puts "MISS: #{cond_str}" price = 0 each_item(cond) { |i| price += i.price } + @cache_total[cond_str] = price + @cache.transaction do + @cache['mobo'] = @cache_total + end price end @@ -735,6 +754,10 @@ end end @db['mobo'] = @monthly + end + begin + File.delete( "#{@conf.data_path}#{@year}#{@month}_cache.db" ) + rescue end end -- かずひこ ★シャア「名字が付いてない」 ☆一兵卒「あんなの飾りです。偉い人にはそれが分からんのです」 From kazuhiko @ fdiary.net Thu Sep 25 18:50:10 2003 From: kazuhiko @ fdiary.net (Kazuhiko) Date: Thu, 25 Sep 2003 18:50:10 +0900 Subject: [Mobo-dev] キャッシュで高速化 Message-ID: かずひこです。 まともなキャッシュ化を実装したところ、かなーり速くなりました。その他、 erbscan (http://raa.ruby-lang.org/list.rhtml?name=erbscan) があれば使う、 skel/*.rhtml の計算の無駄を省く、などあの手この手で高速化を計りました。 ●月表示 (w/o cache) real 0m1.927s user 0m1.775s sys 0m0.120s (w/ cache) -- 7 倍! real 0m0.274s user 0m0.244s sys 0m0.025s ●集計表示 (w/o cache) real 0m5.446s user 0m5.189s sys 0m0.174s (w/ cache) -- 25 倍! real 0m0.216s user 0m0.191s sys 0m0.022s 変更点は下記のとおりです。 2003-09-25 Kazuhiko * mobo.rb: use erbscan if possible. * mobo.rb: cache support. change some methods. MoBoBase#monthly(month).total(cond) -> MoBoBase#total(month, cond) MoBoBase#monthly(month).genre_total(cond) -> MoBoBase#genre_total(month, cond) MoBoBase#monthly(month).income_total(cond) -> MoBoBase#income_total(month, cond) MoBoBase#monthly(month).expense_total(cond) -> MoBoBase#expense_total(month, cond) MoBoBase#total(cond) -> MoBoBase#grand_total(cond) * skel/month.rhtml: ditto. * skel/report.rhtml: ditto. 一部のメソッドの使い方が変更になったのでご注意ください。 念のためバックアップをとってから cvs up されることをおすすめしますが、も はや以前のものには戻れない快適さです。:) というわけで、そろそろドキュメント整備の方に注力して、ファーストリリース を目指したいと思いますが、いかがでしょうか? ご意見など何でもお待ちしております。 -- かずひこ ★シャア「名字が付いてない」 ☆一兵卒「あんなの飾りです。偉い人にはそれが分からんのです」