timestamps encoding (was: Re: Straw Poll: Restore Header Table and Static Table Indices)
Willy Tarreau <w@1wt.eu> Tue, 21 October 2014 14:30 UTC
Return-Path: <ietf-http-wg-request@listhub.w3.org>
X-Original-To: ietfarch-httpbisa-archive-bis2Juki@ietfa.amsl.com
Delivered-To: ietfarch-httpbisa-archive-bis2Juki@ietfa.amsl.com
Received: from localhost (ietfa.amsl.com [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id B47B01A1B74 for <ietfarch-httpbisa-archive-bis2Juki@ietfa.amsl.com>; Tue, 21 Oct 2014 07:30:05 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -6.312
X-Spam-Level:
X-Spam-Status: No, score=-6.312 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, J_CHICKENPOX_62=0.6, RCVD_IN_DNSWL_HI=-5, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01] autolearn=ham
Received: from mail.ietf.org ([4.31.198.44]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zFZsTmw_gnMX for <ietfarch-httpbisa-archive-bis2Juki@ietfa.amsl.com>; Tue, 21 Oct 2014 07:30:00 -0700 (PDT)
Received: from frink.w3.org (frink.w3.org [128.30.52.56]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id A37821A6F6B for <httpbisa-archive-bis2Juki@lists.ietf.org>; Tue, 21 Oct 2014 07:30:00 -0700 (PDT)
Received: from lists by frink.w3.org with local (Exim 4.72) (envelope-from <ietf-http-wg-request@listhub.w3.org>) id 1XgaOS-0003sP-DL for ietf-http-wg-dist@listhub.w3.org; Tue, 21 Oct 2014 14:26:36 +0000
Resent-Date: Tue, 21 Oct 2014 14:26:36 +0000
Resent-Message-Id: <E1XgaOS-0003sP-DL@frink.w3.org>
Received: from maggie.w3.org ([128.30.52.39]) by frink.w3.org with esmtp (Exim 4.72) (envelope-from <w@1wt.eu>) id 1XgaOJ-0003rD-6D for ietf-http-wg@listhub.w3.org; Tue, 21 Oct 2014 14:26:27 +0000
Received: from 1wt.eu ([62.212.114.60]) by maggie.w3.org with esmtp (Exim 4.72) (envelope-from <w@1wt.eu>) id 1XgaOI-0004bs-3z for ietf-http-wg@w3.org; Tue, 21 Oct 2014 14:26:27 +0000
Received: (from willy@localhost) by mail.home.local (8.14.4/8.14.4/Submit) id s9LEOpDQ032690; Tue, 21 Oct 2014 16:24:51 +0200
Date: Tue, 21 Oct 2014 16:24:51 +0200
From: Willy Tarreau <w@1wt.eu>
To: Julian Reschke <julian.reschke@gmx.de>
Cc: Poul-Henning Kamp <phk@phk.freebsd.dk>, Roberto Peon <grmocg@gmail.com>, Mark Nottingham <mnot@mnot.net>, RUELLAN Herve <Herve.Ruellan@crf.canon.fr>, Amos Jeffries <squid3@treenet.co.nz>, "ietf-http-wg@w3.org" <ietf-http-wg@w3.org>
Message-ID: <20141021142451.GJ30397@1wt.eu>
References: <20141016091626.GC3079@1wt.eu> <34008D72-5800-412E-A276-81C6C7BFA9E2@mnot.net> <20141020165353.GA25743@1wt.eu> <8F120E2F-704E-44CB-ACF2-C743B870B2B6@mnot.net> <CAP+FsNdOHVj=YTE35Vj7mbED0OKghd-mpnFZSMcZNBM3JybJDg@mail.gmail.com> <20141021092505.GA30397@1wt.eu> <52739.1413883975@critter.freebsd.dk> <20141021094425.GC30397@1wt.eu> <54462F3F.30502@gmx.de> <20141021102016.GG30397@1wt.eu>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Disposition: inline
In-Reply-To: <20141021102016.GG30397@1wt.eu>
User-Agent: Mutt/1.4.2.3i
Received-SPF: pass client-ip=62.212.114.60; envelope-from=w@1wt.eu; helo=1wt.eu
X-W3C-Hub-Spam-Status: No, score=-3.8
X-W3C-Hub-Spam-Report: AWL=-2.327, RP_MATCHES_RCVD=-1.43, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001
X-W3C-Scan-Sig: maggie.w3.org 1XgaOI-0004bs-3z 0d61c91fc8a98c92c507194450441dcd
X-Original-To: ietf-http-wg@w3.org
Subject: timestamps encoding (was: Re: Straw Poll: Restore Header Table and Static Table Indices)
Archived-At: <http://www.w3.org/mid/20141021142451.GJ30397@1wt.eu>
Resent-From: ietf-http-wg@w3.org
X-Mailing-List: <ietf-http-wg@w3.org> archive/latest/27664
X-Loop: ietf-http-wg@w3.org
Resent-Sender: ietf-http-wg-request@w3.org
Precedence: list
List-Id: <ietf-http-wg.w3.org>
List-Help: <http://www.w3.org/Mail/>
List-Post: <mailto:ietf-http-wg@w3.org>
List-Unsubscribe: <mailto:ietf-http-wg-request@w3.org?subject=unsubscribe>
On Tue, Oct 21, 2014 at 12:20:16PM +0200, Willy Tarreau wrote: > On Tue, Oct 21, 2014 at 12:02:39PM +0200, Julian Reschke wrote: > > On 2014-10-21 11:44, Willy Tarreau wrote: > > >... > > >I know, and you remember, that was one of the basic points of our > > >proposal 2 years ago. I think we'll hardly propose this here since > > >it changes the ability to pass certain invalid values. However, I > > > ... > > > > Right. But maybe we could try something the represents valid values > > well, but still allows transporting invalid values? > > I hadn't thought about that option honnestly and I find that it could be > very efficient and even save a lot of CPU by avoiding to process anything > complicated or out of range. I mean, if the date respects a very precise > format, we encode it and transport it in binary. Otherwise we leave it > in plain text mode. We can have an encoding starting with something > impossible in text mode (eg: LF byte) to mark that what follows is a > 32-bit binary encoding of a timestamp. A variable-length encoding would > allow to pass end of the current epoch and to use less bytes currently. > This method would even be compatible with existing encoding and parsers. I just experimented with this : variable length encoding of the date using 255/256 possible values for each byte, so that it remains compatible with string processing in programs (no zero byte is produced). The example code below converts valid strings to their compacted form and leaves the non-convertable ones intact. The converted string's format indicates what format it's in (ie: if it begins with 0xA its a compact one). The gains are interesting, just tested on example.com, there are 3 dates there, 24 bytes each, and reduced to 5 bytes each, that's 57 bytes saved in one response header (19 bytes saved per valid header). The encoding function works this way : it tries to parse a valid HTTP date and returns either the original string or the compacted one : const char *http_date_encode(const char *date) { struct tm tm; static char buf[10]; if (strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &tm) == NULL) return date; if (!nz_encode(buf + 1, sizeof(buf) - 1, mktime(&tm))) return date; buf[0] = '\n'; return buf; } The decoder does the opposite, it checks whether the date string starts with an LF character and returns either its expansion or the string itself : const char *http_date_decode(const char *date) { struct tm *tm; static char buf[200]; time_t t; if (*date != '\n') return date; t = nz_decode(date + 1); tm = gmtime(&t); strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", tm); return buf; } The non-zero encoding/decoding functions are simple base255 encoders which ensure that we won't add an extra byte for a long time, but if someone doesn't feel at ease with these, it's possible to use another format (and waste more space) : /* decode a non-zero encoded string into a long int */ unsigned long nz_decode(const char *buf) { unsigned long v; unsigned long mul = 1; for (v = 0; *buf; buf++) { v += mul * (unsigned char)(*buf - 1); mul *= 255; } return v; } /* encode value <v> into a non-zero string */ const char *nz_encode(char *buf, int size, unsigned long v) { char *out; for (out = buf; size >= 1;) { if (!v) { *out = 0; return buf; } *(out++) = 1 + (v % 255); v /= 255; } return NULL; } Then the calling code which converts the date both ways and compares outputs. int main(int argc, char **argv) { const char *in; const char *enc; const char *dec; if (argc < 2) return 1; in = argv[1]; enc = http_date_encode(argv[1]); dec = http_date_decode(enc); printf("in =<%s> (%d chars)\n", in, strlen(in)); printf("enc=<%s> (%d chars) savings=%d\n", enc, strlen(enc), strlen(in)-strlen(enc)); printf("dec=<%s> (%d chars) strcmp=%d\n", dec, strlen(dec), strcmp(in, dec)); return 0; } $ curl -I example.com HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: max-age=604800 Content-Type: text/html Date: Tue, 21 Oct 2014 14:20:53 GMT Etag: "359670651" Expires: Tue, 28 Oct 2014 14:20:53 GMT Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT Server: ECS (iad/182A) X-Cache: HIT x-ec-custom-error: 1 Content-Length: 1270 These 3 dates (Date, Expires, Last-Modified) can then be shrunk by 57 bytes total without the risk of altering an invalid content. What's nice with this method is that only the producer needs to know what headers it wants to compact, the recipient doesn't need to know them since it can use the LF byte to know if a value needs to be expanded or not. An if-modified-since in a request could be reduced by 19 bytes, which is nice as well. Thoughts ? Willy
- Re: Restore Header Table and Static Table Indices Nicholas Hurley
- Restore Header Table and Static Table Indices Jeff Pinner
- Re: Restore Header Table and Static Table Indices Kulkarni, Saurabh
- Re: Restore Header Table and Static Table Indices Poul-Henning Kamp
- Straw Poll: Restore Header Table and Static Table… Mark Nottingham
- Re: Restore Header Table and Static Table Indices Ludin, Stephen
- Re: Restore Header Table and Static Table Indices Michael Sweet
- Re: Restore Header Table and Static Table Indices Greg Wilkins
- Re: Restore Header Table and Static Table Indices Daniel Stenberg
- Re: Restore Header Table and Static Table Indices Simpson, Robby (GE Energy Management)
- Re: Restore Header Table and Static Table Indices Roberto Peon
- RE: Restore Header Table and Static Table Indices Mike Bishop
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Shigeki Ohtsu
- Re: Straw Poll: Restore Header Table and Static T… Greg Wilkins
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Amos Jeffries
- RE: Straw Poll: Restore Header Table and Static T… K.Morgan
- Re: Straw Poll: Restore Header Table and Static T… Nicholas Hurley
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Nicholas Hurley
- RE: Straw Poll: Restore Header Table and Static T… RUELLAN Herve
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Greg Wilkins
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Roberto Peon
- Re: Straw Poll: Restore Header Table and Static T… Greg Wilkins
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Roberto Peon
- Re: Straw Poll: Restore Header Table and Static T… Poul-Henning Kamp
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Greg Wilkins
- Re: Straw Poll: Restore Header Table and Static T… Adrian Cole
- Re: Restore Header Table and Static Table Indices Adrian Cole
- Re: Straw Poll: Restore Header Table and Static T… Greg Wilkins
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Adrian Cole
- Re: Straw Poll: Restore Header Table and Static T… Adrian Cole
- Re: Straw Poll: Restore Header Table and Static T… Amos Jeffries
- Re: Straw Poll: Restore Header Table and Static T… Poul-Henning Kamp
- Re: Straw Poll: Restore Header Table and Static T… Adrian Cole
- RE: Straw Poll: Restore Header Table and Static T… RUELLAN Herve
- Re: Straw Poll: Restore Header Table and Static T… Poul-Henning Kamp
- Re: Straw Poll: Restore Header Table and Static T… Simpson, Robby (GE Energy Management)
- Re: Straw Poll: Restore Header Table and Static T… Simpson, Robby (GE Energy Management)
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Jason Greene
- RE: Straw Poll: Restore Header Table and Static T… RUELLAN Herve
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Poul-Henning Kamp
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Greg Wilkins
- Re: Straw Poll: Restore Header Table and Static T… Mark Nottingham
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Poul-Henning Kamp
- Re: Straw Poll: Restore Header Table and Static T… Roberto Peon
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Poul-Henning Kamp
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Julian Reschke
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Poul-Henning Kamp
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Greg Wilkins
- Re: Straw Poll: Restore Header Table and Static T… Willy Tarreau
- Re: Straw Poll: Restore Header Table and Static T… Jason Greene
- timestamps encoding (was: Re: Straw Poll: Restore… Willy Tarreau
- #578 [was: Straw Poll: Restore Header Table and S… Mark Nottingham
- Re: timestamps encoding (was: Re: Straw Poll: Res… Martin Nilsson
- Re: timestamps encoding Amos Jeffries
- Re: #578 [was: Straw Poll: Restore Header Table a… Amos Jeffries
- Re: #578 [was: Straw Poll: Restore Header Table a… Mark Nottingham
- Re: timestamps encoding Martin Nilsson