Permalink
Browse files

use table for decode

  • Loading branch information...
kazeburo committed Feb 26, 2014
1 parent 7f5da68 commit 204731932caa91d50312f9a724793a835cabde3a
Showing with 69 additions and 64 deletions.
  1. +1 −0 META.json
  2. +9 −9 README.md
  3. +9 −9 lib/Cookie/Baker/XS.pm
  4. +50 −46 lib/Cookie/Baker/XS.xs
View
@@ -43,6 +43,7 @@
},
"runtime" : {
"requires" : {
+ "Exporter" : "0",
"perl" : "5.008001"
}
},
View
@@ -18,22 +18,22 @@ For more details, see [Cookie::Baker](http://search.cpan.org/perldoc?Cookie::Bak
# BENCHMARK
- ## length($cookie) == 600
+ ## length($cookie) == 675
Benchmark: running pp, xs for at least 1 CPU seconds...
- pp: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 16904.72/s (n=17919)
- xs: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 170835.85/s (n=181086)
+ pp: 1 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 16592.59/s (n=17920)
+ xs: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 182043.81/s (n=191146)
Rate pp xs
- pp 16905/s -- -90%
- xs 170836/s 911% --
+ pp 16593/s -- -91%
+ xs 182044/s 997% --
## length($cookie) == 17
Benchmark: running pp, xs for at least 1 CPU seconds...
- pp: 1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 214370.09/s (n=229376)
- xs: 1 wallclock secs ( 1.12 usr + 0.00 sys = 1.12 CPU) @ 1117090.18/s (n=1251141)
+ pp: 2 wallclock secs ( 1.05 usr + 0.01 sys = 1.06 CPU) @ 201749.06/s (n=213854)
+ xs: 0 wallclock secs ( 1.19 usr + 0.01 sys = 1.20 CPU) @ 1042617.50/s (n=1251141)
Rate pp xs
- pp 214370/s -- -81%
- xs 1117090/s 421% --
+ pp 201749/s -- -81%
+ xs 1042618/s 417% --
# SEE ALSO
View
@@ -35,21 +35,21 @@ For more details, see L<Cookie::Baker>'s document
=head1 BENCHMARK
- ## length($cookie) == 600
+ ## length($cookie) == 675
Benchmark: running pp, xs for at least 1 CPU seconds...
- pp: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 16904.72/s (n=17919)
- xs: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 170835.85/s (n=181086)
+ pp: 1 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 16592.59/s (n=17920)
+ xs: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 182043.81/s (n=191146)
Rate pp xs
- pp 16905/s -- -90%
- xs 170836/s 911% --
+ pp 16593/s -- -91%
+ xs 182044/s 997% --
## length($cookie) == 17
Benchmark: running pp, xs for at least 1 CPU seconds...
- pp: 1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 214370.09/s (n=229376)
- xs: 1 wallclock secs ( 1.12 usr + 0.00 sys = 1.12 CPU) @ 1117090.18/s (n=1251141)
+ pp: 2 wallclock secs ( 1.05 usr + 0.01 sys = 1.06 CPU) @ 201749.06/s (n=213854)
+ xs: 0 wallclock secs ( 1.19 usr + 0.01 sys = 1.20 CPU) @ 1042617.50/s (n=1251141)
Rate pp xs
- pp 214370/s -- -81%
- xs 1117090/s 421% --
+ pp 201749/s -- -81%
+ xs 1042618/s 417% --
=head1 SEE ALSO
View
@@ -13,27 +13,55 @@ extern "C" {
#include "ppport.h"
+static char hextbl[256] =
+/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
+{
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 3 0-9 */
+ 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4 @,A-Z */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5 */
+ 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6 `,a-z */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 8 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 9 */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f */
+};
+
+static
+void
+url_decode_key(const char *src, int src_len, char *d, int *key_len) {
+ int i, dlen=0;
+ for (i = 0; i < src_len; i++ ) {
+ if ( src[i] == '%' && isxdigit(src[i+1]) && isxdigit(src[i+2]) ) {
+ d[dlen++] = hextbl[(U8)src[i+1]] * 16 + hextbl[(U8)src[i+2]];
+ i += 2;
+ }
+ else {
+ d[dlen++] = src[i];
+ }
+ }
+ *key_len = dlen;
+}
+
static SV *
url_decode_val(pTHX_ const char *src, int start, int end) {
int dlen = 0, i = 0;
char *d;
- char s2, s3;
SV * dst;
dst = newSV(0);
(void)SvUPGRADE(dst, SVt_PV);
d = SvGROW(dst, (end - start) * 3 + 1);
for (i = start; i < end; i++ ) {
if ( src[i] == '%' && isxdigit(src[i+1]) && isxdigit(src[i+2]) ) {
- s2 = src[i+1];
- s3 = src[i+2];
- s2 -= s2 <= '9' ? '0'
- : s2 <= 'F' ? 'A' - 10
- : 'a' - 10;
- s3 -= s3 <= '9' ? '0'
- : s3 <= 'F' ? 'A' - 10
- : 'a' - 10;
- d[dlen++] = s2 * 16 + s3;
+ d[dlen++] = hextbl[(U8)src[i+1]] * 16 + hextbl[(U8)src[i+2]];
i += 2;
}
else {
@@ -46,59 +74,33 @@ url_decode_val(pTHX_ const char *src, int start, int end) {
return dst;
}
-static
-void
-url_decode_key(const char *src, int src_len, char *d, int *key_len) {
- int i, dlen=0;
- char s2, s3;
- for (i = 0; i < src_len; i++ ) {
- if ( src[i] == '%' && isxdigit(src[i+1]) && isxdigit(src[i+2]) ) {
- s2 = src[i+1];
- s3 = src[i+2];
- s2 -= s2 <= '9' ? '0'
- : s2 <= 'F' ? 'A' - 10
- : 'a' - 10;
- s3 -= s3 <= '9' ? '0'
- : s3 <= 'F' ? 'A' - 10
- : 'a' - 10;
- d[dlen++] = s2 * 16 + s3;
- i += 2;
- }
- else {
- d[dlen++] = src[i];
- }
- }
- *key_len = dlen;
-}
static
void
-renewmem(pTHX_ const char *d, int *cur, const int req) {
+renewmem(pTHX_ char **d, int *cur, const int req) {
if ( req > *cur ) {
*cur = req;
- Renew(d, *cur, char);
+ Renew(*d, *cur, char);
}
}
+
MODULE = Cookie::Baker::XS PACKAGE = Cookie::Baker::XS
PROTOTYPES: DISABLE
-void
+SV *
crush_cookie(cookie)
SV *cookie
PREINIT:
char *src, *prev, *p, *key;
- int i, prev_s=0, la, key_len, key_size = 256;
+ int i, prev_s=0, la, key_len, key_size=64;
STRLEN src_len;
HV *hv;
- PPCODE:
+ CODE:
hv = newHV();
- ST(0) = sv_2mortal(newRV_noinc((SV *)hv));
-
if ( SvOK(cookie) ) {
Newx(key, key_size, char);
-
src = (char *)SvPV(cookie,src_len);
prev = src;
for ( i=0; i<src_len; i++ ) {
@@ -113,7 +115,7 @@ crush_cookie(cookie)
}
p = memchr(prev, '=', i - prev_s);
if ( p != NULL ) {
- renewmem(aTHX_ key, &key_size, (p - prev)*3+1);
+ renewmem(aTHX_ &key, &key_size, (p - prev)*3+1);
url_decode_key(prev, p - prev, key, &key_len);
if ( !hv_exists(hv, key, key_len) ) {
(void)hv_store(hv, key, key_len,
@@ -136,15 +138,17 @@ crush_cookie(cookie)
}
p = memchr(prev, '=', i - prev_s);
if ( p != NULL ) {
- renewmem(aTHX_ key, &key_size, (p - prev)*3+1);
+ renewmem(aTHX_ &key, &key_size, (p - prev)*3+1);
url_decode_key(prev, p - prev, key, &key_len);
if ( !hv_exists(hv, key, key_len) ) {
(void)hv_store(hv, key, key_len,
url_decode_val(aTHX_ prev, p - prev + 1, la ), 0);
}
}
}
-
Safefree(key);
}
- XSRETURN(1);
+ RETVAL = newRV_noinc((SV *)hv);
+ OUTPUT:
+ RETVAL
+

0 comments on commit 2047319

Please sign in to comment.