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

RichardFooriRichardFoori 2018/01/10 20:01 So what are you waiting for? Once you take into account the above mentioned five factors, you will be in a better position to take an informed decision. <a href=http://www.cheapsportsnfljerseys.us.com/>Wholesale Jerseys Cheap</a>. Moreover, baby car seat strollers can be unclipped from the car seat at any given time without any hassle thus allowing you to carry your baby on the car seat itself if you want. Both hatchback luxury cars, they provide you with the comfort and classic looks of a Mercedes and the convenience of a compact design. Panama has everything that qualifies it as a Central American hotspot ?a teeming wildlife, wonderful sandy beaches, a haven for scuba divers, world class fishing areas, reliable transportation, English proficient population and cosmopolitan capital city ?minus the crowd. We are promoting a range of storage products ranging from 1TB to 108 TB using the Unified Storage technology which offers better performance, scalability, reliability and good return on investment. Come to a decision the types with price tag comparisons, brand name or high quality comparisons and consumer opinions. Purchasing the whole travel system on the other hand can prove to be much profitable. It removes all pop ups and quarantines any upcoming dangers throughout your participate in.<a href=http://www.cheapsportsnfljerseys.us.com/>Cheap China Jerseys</a>. In addition,<a href=http://www.cheapsportsnfljerseys.us.com/>Cheap Jerseys China Wholesale</a>, some tires require retreading of previously worn tires. Nothing should be more important to you than the customer. The most effective method to build a Quality Management:Government associations are giving the errand to be performed by the private parts.Author's Resource BoxAre you planning on visiting places in Panama, South America? Check out the different Vacation Homes in South America atArticle Source: Not only are they convenient and safer for kids but are even unique in styles and augments the smartness of your baby.
Visit my site:http://www.cheapsportsnfljerseys.us.com/

MichaelGefMichaelGef 2018/03/30 15:26 <a href=http://achat-cialis-generique.net/>cialis generique</a> commander cialis sur internet
<a href=http://commandercialisgenerique.net/>acheter cialis</a> commander cialis
<a href=http://acquistarecialisitalia.net/>acquistare cialis</a> acquisto cialis
<a href=http://comprar-cialis-generico.net/>venta cialis</a> cialis 20 mg

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


画像認証

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