httpdate: minor size reduction in DSO
authorEric Wong <normalperson@yhbt.net>
Tue, 19 Feb 2013 11:36:18 +0000 (19 11:36 +0000)
committerEric Wong <normalperson@yhbt.net>
Sun, 24 Feb 2013 21:48:41 +0000 (24 21:48 +0000)
Extra pointers waste space in the DSO.  Normally I wouldn't
care, but the string lengths are identical and this code
already made it into another project in this form.

size(1) output:

         text      data     bss     dec     hex  filename
before: 42881      2040     336   45257    b0c9  unicorn_http.so
 after: 42499      1888     336   44723    aeb3  unicorn_http.so

ref: http://www.akkadia.org/drepper/dsohowto.pdf

ext/unicorn_http/httpdate.c

index bfa11ca..bf54fdd 100644 (file)
@@ -5,13 +5,9 @@
 static const size_t buf_capa = sizeof("Thu, 01 Jan 1970 00:00:00 GMT");
 static VALUE buf;
 static char *buf_ptr;
-static const char *const week[] = {
-       "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-};
-static const char *const months[] = {
-       "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
+static const char week[] = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat";
+static const char months[] = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0"
+                             "Jul\0Aug\0Sep\0Oct\0Nov\0Dec";
 
 /* for people on wonky systems only */
 #ifndef HAVE_GMTIME_R
@@ -57,9 +53,9 @@ static VALUE httpdate(VALUE self)
        /* we can make this thread-safe later if our Ruby loses the GVL */
        snprintf(buf_ptr, buf_capa,
                 "%s, %02d %s %4d %02d:%02d:%02d GMT",
-                week[tm.tm_wday],
+                week + (tm.tm_wday * 4),
                 tm.tm_mday,
-                months[tm.tm_mon],
+                months + (tm.tm_mon * 4),
                 tm.tm_year + 1900,
                 tm.tm_hour,
                 tm.tm_min,