Yappo::タワシ このページをアンテナに追加 RSSフィード

2008-05-13

tieしてSTDOUTにフックかける

HTTP::Engine の STDIN STDOUT 処理を Interface に read write API を生やして、Interfaceの外からwrite readを読んで処理するのか。

もしくは Interface は STDIN STDOUT をセットアップだけして Interface の外は print STDOUT $buffer とするだけでsocketに書き込めるようにするかの話がでました。


STDIN STDOUT だけセットアップするのは、Interface とそれ以外の依存度が減るので良いのですが、 Danga::Socket や POE などの非同期系IOを使う場合は、普通だと非同期な出力処理(write)が出来ないと思われます。というのはソース読んだら嘘でした><

でも write method 使わないと大変な事になりそうな気がする。教えて偉い人。


その悩みは tie で出来るよ。という事で IO::Scalar と tie を使って、 STDOUT に print したら任意のコードリファレンスに出力内容を渡す仕組みを作ってみました。

これで Danga::Socket とかで $sock->write して出力とか POE 等の任意のメソッドで出力出来ます。


で、ベンチ取ったら素のSTDOUTより倍遅い感じ。

tied  : 0 wallclock secs ( 0.60 usr +  0.00 sys =  0.60 CPU) @ 166666.67/s (n=100000)
scalar: 1 wallclock secs ( 0.32 usr +  0.01 sys =  0.33 CPU) @ 303030.30/s (n=100000)
normal: 0 wallclock secs ( 0.32 usr +  0.00 sys =  0.32 CPU) @ 312500.00/s (n=100000)
package Tied;
use strict;
use warnings;
use base 'IO::Scalar';

sub open {
    my ($self, $ref) = @_;

    ### Sanity:                                                                                                                    
    (ref($ref) eq "CODE") or Carp::croak "open() needs a ref to a code";

    ### Setup:                                                                                                                     
    *$self->{Pos} = 0;          ### seek position                                                                                  
    *$self->{CR}  = $ref;      ### code reference                                                                                  
    $self;
}

sub print {
    my $self = shift;
    *$self->{CR}->(@_);
    1;
}

package main;
use strict;
use warnings;
use Benchmark qw/timethese timeit timestr/;
use IO::Scalar;

my $count = 100_000;

my $tied = Tied->new( sub { print @_ } );
my $scalar = IO::Scalar->new( \my $buffer );

tie *STDOUT, 'IO::Scalar', \my $out;
my $t_tied = timeit $count => sub {
    print $tied "hoge\n";
};
my $t_scalar = timeit $count => sub {
    print $scalar "hoge\n";
};
my $t_normal = timeit $count => sub {
    print "hoge\n";
};
untie *STDOUT;

print "tied  :", timestr($t_tied), "\n";
print "scalar:", timestr($t_scalar), "\n";
print "normal:", timestr($t_normal), "\n";

DASMDASM 2008/05/28 23:08 Sanity とかいうダサいコメントはひろみちゅ先生的にヤヴァいんじゃないの?
消したら問題があるの? 死んだりはしないでしょ?

maypeecos@yaoo.commaypeecos@yaoo.com 2017/03/28 02:53 Dear Sir / Madam,
I wish to write you these letter asking for your concern.
We are group of local miners from Wasa Amefie area in the central region part of Ghana.
Has in stock 250 kg AU gold dust that seeking for interested buyer.
Please note , the price is negotiable.
Thanks.
Emmanuel

EntewlyannuatEntewlyannuat 2017/08/23 08:28 Most providers are highly comparable in what they offer which can be difficult to contend with on various levels.All forms of diabetes can impact your eye area, so ensure you are discovering an eye medical professional routinely to find any complications before they turn out to be worse. From offering options to help you improve your credit score to helping you establish more credit accounts, an unsecured business loan is easily obtainable with a little work. Examine when the site features a return coverage.Work only with a site providing you the service at a very small rate. <a href=http://www.cheap-wholesalenfljerseys.com/>Cheap NFL Jerseys Wholesale</a>.<a href=http://www.cheap-wholesalenfljerseys.com/>Cheap NFL Jerseys China Wholesale</a>. It takes a professional instructor and Driving Lessons Conwy to learn how to control a vehicle and how to be a really good driver. In direction of just take the newest safety hotfixes (as Microsoft phone calls them), take a look at The reason is that the carbohydrate food from your meals inform your liver to stop delivering glucose to the blood vessels. Do an every month checkup upon your information and facts for at minimal 2 times a 7 days or thrice a thirty day period.Work only with a site providing you the service at a very small rate. The entire cleaning process does not take a lot of time as the greatest cleaning equipments are employed in the process.<a href=http://www.cheap-wholesalenfljerseys.com/>Cheap NFL Jerseys From China</a>. Plant hire in Preston has been fast gaining popularity amongst those small-scale industrialists looking to expand into large-scale production.
Visit my site:http://www.cheap-wholesalenfljerseys.com/

RichardFooriRichardFoori 2017/10/10 12:26 Our multifarious capabilities, cross-sector expertise and detailed knowledge of various markets, put us in a unique position to take up Custom Research demands of yourself.S. <a href=http://www.cheapoutletjerseys.us.com/>Wholesale NFL Jerseys</a>. The ones that want to watch this movie online but do not know the web site can easily look for the movie and the approved website through any kind of o search engines like google online. Then, once complete, allow your first coat to dry for 12 to 24 hours. This binaural beat production is price finding and that I would propose it.S.S. The more that they trust you, the extra business you'll acquire.<a href=http://www.cheapoutletjerseys.us.com/>Wholesale NFL Jerseys Cheap</a>. It contains herbal, pure and safe ingredients that never bring side effects and safe to use. Yo digo que usted fue quien manej? el ecoendoscopio y que, con o sin movimiento de ella,<a href=http://www.cheapoutletjerseys.us.com/>Wholesale Jerseys</a>, le perfor? el es?fago.Focus on key areasDecorating each part of the venue is truly not necessary.au">camelbak<a>.
Visit my site:http://www.cheapoutletjerseys.us.com/

LardrindroxLardrindrox 2017/10/18 11:24 Viagra Osterreich Ohne Rezept Shelf Life Keflex Cipro Online No Prescription Overnight <a href=http://levibuyus.com>non prescription levitra</a> Atlantic Drug Store Viagra Cialis Generique Tadalafil 20 Mg Zentel Website With Free Shipping Overseas Pharmacy

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

トラックバック - http://d.hatena.ne.jp/yappo/20080513/1210615145