<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ismael Juma &#187; protocol buffers</title>
	<atom:link href="http://blog.juma.me.uk/tag/protocol-buffers/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.juma.me.uk</link>
	<description></description>
	<lastBuildDate>Fri, 18 Nov 2011 00:09:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.juma.me.uk' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ismael Juma &#187; protocol buffers</title>
		<link>http://blog.juma.me.uk</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.juma.me.uk/osd.xml" title="Ismael Juma" />
	<atom:link rel='hub' href='http://blog.juma.me.uk/?pushpress=hub'/>
		<item>
		<title>Json serialization/deserialization faster than protocol buffers?</title>
		<link>http://blog.juma.me.uk/2009/03/25/json-serializationdeserialization-faster-than-protocol-buffers/</link>
		<comments>http://blog.juma.me.uk/2009/03/25/json-serializationdeserialization-faster-than-protocol-buffers/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 10:47:13 +0000</pubDate>
		<dc:creator>Ismael Juma</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[protocol buffers]]></category>

		<guid isPermaLink="false">http://blog.juma.me.uk/?p=91</guid>
		<description><![CDATA[I was reading the The Book of JOSH and saw the following statement: &#8220;Json delivers on what XML promised. Simple to understand, effective data markup accessible and usable by human and computer alike. Serialization/Deserialization is on par with or faster then XML, Thrift and Protocol Buffers.&#8221; That seemed a bit too definite for my taste. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.juma.me.uk&amp;blog=4860094&amp;post=91&amp;subd=ijuma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was reading the <a href="http://thegreylensmansview.blogspot.com/2009/02/book-of-josh.html">The Book of JOSH</a> and saw the following statement:</p>
<p>&#8220;Json delivers on what XML promised. Simple to understand, effective data markup accessible and usable by human and computer alike. Serialization/Deserialization is on par with or faster then XML, Thrift and Protocol Buffers.&#8221;</p>
<p>That seemed a bit too definite for my taste. There are so many variables that can affect the results that I was interested in more information, so I <a href="http://thegreylensmansview.blogspot.com/2009/02/book-of-josh.html?showComment=1237841220000#c2489295053603640559">asked</a> for it and eventually got an <a href="http://thegreylensmansview.blogspot.com/2009/02/book-of-josh.html?showComment=1237898820000#c926785339918676205">answer</a>.</p>
<p>I had a brief look at the <a href="http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking">benchmark</a> referenced and that was enough to come up with some talking points. To make it easier to follow, I will just compare protocol buffers and json (jackson). I started by running the benchmark in my machine (java 1.6.0_14-ea-b03): </p>
<table>
<tr>
<th></th>
<th>Object create</th>
<th>Serialization</th>
<th>Deserialization</th>
<th>Serialized Size</th>
</tr>
<tr>
<td>protobuf</td>
<td>312.95730</td>
<td>3052.26500</td>
<td>2340.84600</td>
<td>217</td>
</tr>
<tr>
<td>json</td>
<td>182.64535</td>
<td>2284.88300</td>
<td>3362.31850</td>
<td>310</td>
</tr>
</table>
<p>Ok, so json doesn&#8217;t seem to be faster on deserialization and the size is almost 50% bigger (a big deal if the network is the bottleneck as is often the case). Why is serialization of protobuf so slow though? Let&#8217;s see the code:</p>
<p><pre class="brush: java;">
    public byte[] serialize(MediaContent content, ByteArrayOutputStream baos) throws IOException
    {
        content.writeTo(baos);
        return baos.toByteArray();
    }
</pre></p>
<p>How about we replace that with content.toByteArray()?</p>
<table>
<tr>
<th></th>
<th>Object create</th>
<th>Serialization</th>
<th>Deserialization</th>
<th>Serialized Size</th>
</tr>
<tr>
<td>protobuf</td>
<td>298.89330</td>
<td>2087.79800</td>
<td>2339.44450</td>
<td>217</td>
</tr>
<tr>
<td>json (jackson)</td>
<td>174.49190</td>
<td>2482.53350</td>
<td>3599.90800</td>
<td>310</tr>
</table>
<p>That&#8217;s more like it. Let&#8217;s try something a bit more exotic just for fun and add XX:+DoEscapeAnalysis:</p>
<table>
<tr>
<th></th>
<th>Object create</th>
<th>Serialization</th>
<th>Deserialization</th>
<th>Serialized Size</th>
</tr>
<tr>
<td>protobuf</td>
<td>260.51330</td>
<td>1925.32300</td>
<td>2302.74250</td>
<td>217</td>
</tr>
<tr>
<td>json (jackson)</td>
<td>176.20370</td>
<td>2385.99750</td>
<td>3647.01700</td>
<td>310</td>
</tr>
</table>
<p>That reduces some of the cost of object creation for protobuf, but it&#8217;s still substantially slower than json. This is not hard to believe because of the builder pattern employed by the Java classes generated by protocol buffers, but I haven&#8217;t investigated it in more detail. In any case, protocol buffers is better in 3 of the measures for this particular benchmark.</p>
<p>What does this mean? Not a lot. As usual, where performance is important, you should create benchmarks that mirror your application and environment. I just couldn&#8217;t let the blanket &#8220;json is on par with or faster than&#8230;&#8221; statement pass without a bit of scrutiny. ;) </p>
<br />Posted in java, programming, scala Tagged: json, performance, protocol buffers <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ijuma.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ijuma.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ijuma.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ijuma.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ijuma.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ijuma.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ijuma.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ijuma.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ijuma.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ijuma.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ijuma.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ijuma.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ijuma.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ijuma.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.juma.me.uk&amp;blog=4860094&amp;post=91&amp;subd=ijuma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.juma.me.uk/2009/03/25/json-serializationdeserialization-faster-than-protocol-buffers/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0eb053bf15b0a3669c973bc50be45a7c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ijuma</media:title>
		</media:content>
	</item>
	</channel>
</rss>
