{"id":47,"date":"2012-01-26T22:19:52","date_gmt":"2012-01-26T22:19:52","guid":{"rendered":"http:\/\/blog.mozilla.org\/nfroyd\/?p=47"},"modified":"2012-01-26T22:19:52","modified_gmt":"2012-01-26T22:19:52","slug":"compressing-strings-in-js","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nfroyd\/2012\/01\/26\/compressing-strings-in-js\/","title":{"rendered":"compressing strings in JS"},"content":{"rendered":"<p>As we kept increasing <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=715927\">the amount<\/a> <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=707320\">of information<\/a> <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=717105\">we send<\/a> in via Telemetry, we need to start thinking about how to keep the size of the ping packets containing that information as small as possible. The packets are just JSON, so the first thing to try is <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=717061\">to compress the data with gzip prior to sending it<\/a>.<\/p>\n<p>This is how you compress a string in a language like Python:<\/p>\n<pre>compressed = zlib.compress(str)<\/pre>\n<p>(Yes, yes, this is not gzip compression.\u00a0 Close enough for pedagogical purposes.)<\/p>\n<p>Short and simple. Boy, I hope it&#8217;s that easy in JS. Hm, let&#8217;s see, there&#8217;s this <tt>nsIStreamConverter<\/tt> interface, that looks promising:<\/p>\n<pre>let converter = Cc[\"@mozilla.org\/streamconv;1?from=uncompressed&amp;to=gzip\"].createInstance(Ci.nsIStreamConverter);\r\nlet stream = Cc[\"@mozilla.org\/stringinputstream;1\"].createInstance(Ci.nsIStringInputStream);\r\nstream.data = string;\r\n\/\/ Hm, having to respecify input\/output types is a bit weird.\r\nlet gzipStream = converter.convert(stream, \"uncompressed\", \"gzip\", null);<\/pre>\n<p>OK, we wound up with a stream, rather than a string, but that&#8217;s OK, because <a href=\"http:\/\/mxr.mozilla.org\/mozilla-central\/source\/content\/base\/public\/nsIXMLHttpRequest.idl#236\"><tt>nsIXMLHttpRequest.send<\/tt> will happily accept a stream<\/a>. So, nothing to worry about. (This is a little white lie; please hold your comments until the end.)<\/p>\n<p>Hm, that doesn&#8217;t seem to work. I get <tt>NS_ERROR_NOT_IMPLEMENTED<\/tt>. Oh, look, <tt>nsDeflateConverter<\/tt> <a href=\"http:\/\/mxr.mozilla.org\/mozilla-central\/source\/modules\/libjar\/zipwriter\/src\/nsDeflateConverter.cpp#99\">doesn&#8217;t implement <tt>nsIStreamConverter.convert<\/tt><\/a>. In fact, none of the stream converters in the tree seem to implement <tt>convert<\/tt>. What a bummer.<\/p>\n<p>Hey, here&#8217;s <tt>nsIStreamConverterService<\/tt>! Maybe he can help. His <tt>convert<\/tt> method <a href=\"http:\/\/mxr.mozilla.org\/mozilla-central\/source\/netwerk\/streamconv\/src\/nsStreamConverterService.cpp#513\">just punts to <tt>nsIStreamConverter.convert<\/tt><\/a>, so that won&#8217;t work, though. Ah, <tt>nsIStreamConverter<\/tt> has an <tt>asyncConvertData<\/tt> method, let&#8217;s try that:<\/p>\n<pre>function Accumulator() {\r\n  this.buffer = \"\";\r\n}\r\nAccumulator.prototype = {\r\n  buffer: null,\r\n  onRequestStart(request, context) {},\r\n  onRequestStop(request, context, statusCode) {},\r\n  onDataAvailable(request, context, inputStream, offset, count) {\r\n    let stream = Cc[\"@mozilla.org\/binaryinputstream;1\"].createInstance(Ci.nsIBinaryInputStream);\r\n    stream.setInputStream(inputStream);\r\n    let input = stream.readByteArray(count);\r\n    this.buffer += String.fromCharCode.apply(input);\r\n  }\r\n};\r\n\r\nlet accumulator = new Accumulator();\r\nlet converter = Cc[\"@mozilla.org\/streamconv;1?from=uncompressed&amp;to=gzip\"].createInstance(Ci.nsIStreamConverter);\r\n\/\/ More respecifying input\/output types.\r\nconverter.asyncConvertData(\"uncompressed\", \"gzip\", accumulator, null);\r\n\/\/ Oh, that method doesn't actually convert anything, it just prepares\r\n\/\/ the instance for doing conversion.\r\nlet stream = Cc[\"@mozilla.org\/stringinputstream;1\"].createInstance(Ci.nsIStringInputStream);\r\nstream.data = string;\r\nconverter.onRequestStart(null, null);\r\nconverter.onDataAvailable(null, null, stream, 0, string.length);\r\nconverter.onRequestStop(null, null, 201 \/* 417 *\/);\r\ncompressed = accumulator.buffer;<\/pre>\n<p>Well, it&#8217;s not as simple as I hoped for, but I guess it works.<\/p>\n<p>FWIW, I do understand why the input\/output types have to be respecified.\u00a0 But I think this is about the best way to do things currently; that&#8217;s kind of frightening. The above is one of those instances where you start to understand why people complain about things being so crufty.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we kept increasing the amount of information we send in via Telemetry, we need to start thinking about how to keep the size of the ping packets containing that information as small as possible. The packets are just JSON, so the first thing to try is to compress the data with gzip prior to [&hellip;]<\/p>\n","protected":false},"author":320,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/47"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/users\/320"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}