Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

pack, 1 pix gif, and mod_perl speed

by nop (Hermit)
on Jan 29, 2003 at 03:25 UTC ( #230844=perlquestion: print w/replies, xml ) Need Help??
nop has asked for the wisdom of the Perl Monks concerning the following question:

I'm a new user of pack, and just getting started with mod_perl.
I'm using this code to send a 1 pixel clear gif under mod_perl:
#!c:\apps\perl\bin\perl.exe use strict; my $hex = '47494638396101000100e3000000000' . '0bf000000bf00bfbf000000bfbf00bf' . '00bfbfc0c0c0808080ff000000ff00f' . 'fff000000ffff00ff00ffffffffff21' . 'f9040100000f002c000000000100010' . '0400402f045003b'; my $gif = pack("H*",$hex); binmode(STDOUT); print "Content-type: image/gif\n"; print "Content-length: 85\n"; print "\n"; print $gif;
That code above seems to work, and runs instantly on a winnt & indigo perl box.

As I mentioned, I'm a new user of "pack." After reading about pack in perlfunc, I tried specifying the length in the pack statement ('H85') vs. using the star ('H*'), like this:
#!c:\apps\perl\bin\perl.exe use strict; my $hex = '47494638396101000100e3000000000' . '0bf000000bf00bfbf000000bfbf00bf' . '00bfbfc0c0c0808080ff000000ff00f' . 'fff000000ffff00ff00ffffffffff21' . 'f9040100000f002c000000000100010' . '0400402f045003b'; my $gif = pack("H85",$hex); binmode(STDOUT); print "Content-type: image/gif\n"; print "Content-length: 85\n"; print "\n"; print $gif;
This second version runs extremely slowly on the same system, taking 3 or 4 seconds to load the (invisible) image.
  • Why do these two program have such different run times?
  • Is the 'H85' pack wrong? If so, why?
  • And, in general, am I going about this (dynamically generating a clear single pixel gif) the right way?

Many thanks

Replies are listed 'Best First'.
Re: pack, 1 pix gif, and mod_perl speed
by pfaut (Priest) on Jan 29, 2003 at 03:39 UTC

    The H85 is wrong. The count to H is a number of nybbles, not bytes. Your string is 170 characters long and each character represents a nybble so you should be using H170.

    I would guess the reason it takes longer is because you told the browser you were going to send 85 characters but you only actually output 43. Pack created a buffer containing the encoding of the first 85 nybbles of your string and ignored the rest. The browser was waiting for the other 42 characters and didn't stop waiting until the session timed out and the socket was closed.

    Your gif doesn't look that dynamic to me. Why not put it in a file?

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: pack, 1 pix gif, and mod_perl speed
by cees (Curate) on Jan 29, 2003 at 04:42 UTC

    Since your 'pack' question has been answered, I will try to answer your last question.

    You would be better off letting Apache cache the image in memory for you using the mod_mmap_static module. Using mod_perl for this is a little overkill, and more than likely slower.

    I use something like the following:

    AliasMatch /(clear|shim|spacer).gif$ /usr/local/apache/htdocs/ +f MMapFile /usr/local/apache/htdocs/clear.gif

    for Apache 2.0 you could look at the mod_mem_cache module. Also have a look at the mod_expires module which allows you to set arbitrary expiry times for your files to take the most advantage of caches and proxies.

    In this case I just don't think perl is right hammer for this nail.

Re: pack, 1 pix gif, and mod_perl speed
by shotgunefx (Parson) on Jan 29, 2003 at 07:15 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://230844]
Approved by BigLug
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-03-20 03:01 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (247 votes). Check out past polls.