[patch] Compression for Perl API

Brad Whitaker whitaker@danga.com
Fri, 15 Aug 2003 11:00:59 -0700 (PDT)


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

---1774023665-1090092404-1060970459=:7242
Content-Type: TEXT/PLAIN; charset=US-ASCII

I've had this on my todo list for some time, but after it was implemented
in the PHP API, I couldn't let Perl get behind. :P

A diff to MemCachedClient.pm is attached.  It uses Compress::Zlib to do
gzip compression on inserts and decompression on gets.  There is currently
a constant minimum compression gain of 20%.  That is, if it does the GZIP
on write and it saves less than 20%, it will insert the uncompressed
version in an effort to speed up later gets.  This number should probably
be tweaked later (and maybe bet set by the user?), but for now this seems
reasonable.

A new 'compress_threshold' key can now be passed to the constructor to
determine the minimum size threshold before a value is compressed.
Since compression is disabled by default, this key also serves as the
global on/off switch.  To change its value later there is also a
set_compress_threshold() method.

For instances when compression should be temporarily disabled regardless
of data size (such as inserting compressed images), there is a new
enable_compress() method which takes a true or false value.

Curious as to how compression affected speed, I wrote up a little test
inserting a set of test data repeatedly.  Below is the output of 3 runs
showing the number of seconds get/set actions took with and without
compression:

compress: no
sets: => 5.33751511573792
gets: => 6.15450966358185
compress: yes
sets: => 5.281329870224
gets: => 8.92959928512573

compress: no
sets: => 5.56796407699585
gets: => 6.13490855693817
compress: yes
sets: => 5.11762177944183
gets: => 8.82273375988007

compress: no
sets: => 5.89584612846375
gets: => 6.15535402297974
compress: yes
sets: => 5.19342517852783
gets: => 8.98201024532318

After writing this mail, I checked and saw that Brad committed this patch
just before he left.  As a result, I've put the new Perl client live at
test.livejournal.org and I'm watching to make sure everything continues to
run smoothly.

--
Brad Whitaker
whitaker@danga.com
---1774023665-1090092404-1060970459=:7242
Content-Type: TEXT/plain; name="MemCachedClient.pm.diff"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.55.0308151100580.7242@danga.com>
Content-Description: 
Content-Disposition: attachment; filename="MemCachedClient.pm.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KTsNCkBAIC0yMjYsNiArMjY5LDggQEANCiAgICAgbXkgJHNvY2sgPSBz
aGlmdDsNCiAgICAgbXkgJG91dHJlZiA9IHNoaWZ0Ow0KIA0KKyAgICB1c2Ug
Ynl0ZXM7ICMgcmV0dXJuIGJ5dGVzIGZyb20gbGVuZ3RoKCkNCisNCiAgICAg
bXkgJWZsYWdzOw0KICAgICBteSAldmFsOw0KICAgICBteSAlbGVuOyAgICMg
a2V5IC0+IGludGVuZGVkIGxlbmd0aA0KQEAgLTI1OCw3ICszMDMsOCBAQA0K
ICAgICAgICAgICAgIGZvcmVhY2ggKGtleXMgJWxlbikgew0KICAgICAgICAg
ICAgICAgICBuZXh0IHVubGVzcyBleGlzdHMgJHZhbHskX307DQogICAgICAg
ICAgICAgICAgIG5leHQgdW5sZXNzIGxlbmd0aCgkdmFseyRffSkgPT0gJGxl
bnskX307DQotICAgICAgICAgICAgICAgICR2YWx7JF99ID0gU3RvcmFibGU6
OnRoYXcoJHZhbHskX30pIGlmICRmbGFnc3skX30gJiAxOw0KKyAgICAgICAg
ICAgICAgICAkdmFseyRffSA9IENvbXByZXNzOjpabGliOjptZW1HdW56aXAo
JHZhbHskX30pIGlmICRIQVZFX1pMSUIgJiYgJGZsYWdzeyRffSAmIEZfQ09N
UFJFU1M7DQorICAgICAgICAgICAgICAgICR2YWx7JF99ID0gU3RvcmFibGU6
OnRoYXcoJHZhbHskX30pIGlmICRmbGFnc3skX30gJiBGX1NUT1JBQkxFOw0K
ICAgICAgICAgICAgICAgICAkb3V0cmVmLT57JF99ID0gJHZhbHskX307DQog
ICAgICAgICAgICAgfQ0KICAgICAgICAgICAgIHJldHVybiAxOw0KQEAgLTI5
Miw4ICszMzgsMTEgQEANCiAgICAgJ3NlcnZlcnMnID0+IFsgIjEwLjAuMC4x
NToxMTIxMSIsICIxMC4wLjAuMTU6MTEyMTIiLCANCiAgICAgICAgICAgICAg
ICAgICAgIjEwLjAuMC4xNzoxMTIxMSIsIFsgIjEwLjAuM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