<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:iprog.com,2007:postingsatom</id>
  <link rel="alternate" type="text/html" href="http://iprog.com"/>
  <link rel="self" type="application/atom+xml" href="http://iprog.com/postings/atom"/>
  <title>interprogramming articles</title>
  <updated>2012-07-19T16:11:00Z</updated>
  <entry>
    <id>tag:iprog.com,2007:Posting43</id>
    <published>2012-07-19T16:11:00Z</published>
    <updated>2012-07-19T16:11:00Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2012/07/code-transition-to-github"/>
    <title>Code Transition to Github</title>
    <content type="html">&lt;p&gt;I&amp;#8217;ve finally taken time to migrate all my public code bases over to github. Effective immediately, the subversion repository is deprecated and will likely disappear soon.&lt;/p&gt;
&lt;p&gt;Links to everything can be found on the &lt;a href="/projects"&gt;projects&lt;/a&gt; page.&lt;/p&gt;</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting42</id>
    <published>2012-07-14T22:30:00Z</published>
    <updated>2012-07-14T22:54:51Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2012/07/new-and-shiny-site-design"/>
    <title>New and shiny site design</title>
    <content type="html">&lt;p&gt;Once again it has become time to refresh the site design. Looking back, it appears the last site refresh was in July of 2007&amp;#8212;I&amp;#8217;m not sure if 5 years on the same design demonstrates longevity and consistency, or if it just means I&amp;#8217;m horribly overdue.&lt;/p&gt;
&lt;p&gt;Regardless, now everything is nice and shiny. One of the goals of the new design was to do my best to leverage HTML5 and CSS3, including new HTML5 tags and use of many of the newer &lt;span class="caps"&gt;CSS&lt;/span&gt; features. As a consequence, the only image file is the background. Everything else is &lt;span class="caps"&gt;CSS&lt;/span&gt; effects along with a couple &lt;span class="caps"&gt;CSS&lt;/span&gt; fonts.&lt;/p&gt;
&lt;p&gt;I also used the refresh as an excuse to experiment with responsive design. I haven&amp;#8217;t had a chance to test a wide array of small-screen devices yet, but on the ones I have access to (various iDevices), the site is usable. I&amp;#8217;d love to hear your experience, good or bad, on any kind of smaller-screen device.&lt;/p&gt;
&lt;p&gt;Other changes include numerous updates to the software stack (Rails 3.2, etc.), better syntax highlighting (now with line numbers!), and improved (in theory) spam filtering.&lt;/p&gt;</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting41</id>
    <published>2012-06-09T00:29:00Z</published>
    <updated>2012-07-14T22:47:31Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2012/06/catching-up"/>
    <title>Catching up</title>
    <content type="html">&lt;p&gt;It&amp;#8217;s clearly time that I catch up with the world around me. It&amp;#8217;s curious how much can pass you by while you&amp;#8217;re consumed with other priorities. Now that some of those have passed, I&amp;#8217;m rediscovering several things that got pushed aside.&lt;/p&gt;
&lt;p&gt;Among them is hopefully writing at least a little more often here. Apparently, that won&amp;#8217;t be a difficult bar to jump over&amp;#8212;it&amp;#8217;s been 2 1/2 years since my last post. Ack!&lt;/p&gt;
&lt;p&gt;Apart from writing here, the process of catching up looks like it will include a bunch of software upgrades (Rails 3 a part of that), a new design for here, and migrating a bunch of stuff to git.&lt;/p&gt;
&lt;p&gt;Stay tuned.&lt;/p&gt;</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting40</id>
    <published>2009-11-13T16:09:00Z</published>
    <updated>2009-11-13T16:09:00Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2009/11/faster-alternatives-activerecord-to_xml-rails-ruby"/>
    <title>Faster alternatives to ActiveRecord::Base.to_xml  (Rails Performance Series)</title>
    <content type="html">&lt;p&gt;This is part 5 of my ongoing series on Ruby on Rails performance. Today&amp;#8217;s topic is a bit more complicated that some of the previous parts.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m going to explore an alternative to using the method &lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="constant"&gt;Base&lt;/span&gt;.to_xml&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Before I get into that, I probably should explain why. After all, &lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="constant"&gt;Base&lt;/span&gt;.to_xml&lt;/code&gt; is really easy to use. The alternative I&amp;#8217;m going to demonstrate isn&amp;#8217;t very easy to use.&lt;/p&gt;
&lt;p&gt;The problem is that &lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="constant"&gt;Base&lt;/span&gt;.to_xml&lt;/code&gt; can be really slow&amp;#8212;even after making the optimizations I&amp;#8217;ve explored in the previous parts to this series. For fairly simple AR objects, this won&amp;#8217;t be a problem. However, when you have a deep object hierarchy and need substantial portions of it to be included in the &lt;span class="caps"&gt;XML&lt;/span&gt; response, it becomes a problem.&lt;/p&gt;
&lt;p&gt;One instance where I encountered this was a nested has_many tree. That is, the object tree looks something like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;ParentObject has_many ChildObjects&lt;br /&gt;
Each ChildObject has_many OtherObjects&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The &lt;span class="caps"&gt;XML&lt;/span&gt; output is often 100k or more, and representing a few dozen or more total objects.&lt;/p&gt;
&lt;a id="more"&gt;&lt;/a&gt;
&lt;p&gt;With that scenario, the &lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="constant"&gt;Base&lt;/span&gt;.to_xml&lt;/code&gt; call looks something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;&lt;span class="instance-variable"&gt;@parent_object&lt;/span&gt;.to_xml(&lt;span class="symbol"&gt;:include&lt;/span&gt;=&amp;gt;{&lt;span class="symbol"&gt;:child_objects&lt;/span&gt;=&amp;gt;{&lt;span class="symbol"&gt;:include&lt;/span&gt;=&amp;gt;&lt;span class="symbol"&gt;:other_objects&lt;/span&gt;}})&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The alternative is definitely more work:&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;h = {&lt;span class="symbol"&gt;:attr1&lt;/span&gt;=&amp;gt;parent_object.attr1, &lt;span class="symbol"&gt;:attr2&lt;/span&gt;=&amp;gt;parent_object.attr2} &lt;span class="comment"&gt;# ... etc&lt;/span&gt;

h[&lt;span class="symbol"&gt;:child_objects&lt;/span&gt;] = parent.child_objects.map &lt;span class="keyword"&gt;do&lt;/span&gt; |child|
  h2 = {&lt;span class="symbol"&gt;:attr1&lt;/span&gt;=&amp;gt;child.attr1, &lt;span class="symbol"&gt;:attr2&lt;/span&gt;=&amp;gt;child.attr2} &lt;span class="comment"&gt;# ... etc&lt;/span&gt;

  h2[&lt;span class="symbol"&gt;:other_objects&lt;/span&gt;] = child.other_objects.map &lt;span class="keyword"&gt;do&lt;/span&gt; |other|
    h3 = {&lt;span class="symbol"&gt;:attr1&lt;/span&gt;=&amp;gt;other.attr1, &lt;span class="symbol"&gt;:attr2&lt;/span&gt;=&amp;gt;other.attr2} &lt;span class="comment"&gt;# ... etc&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  h2
&lt;span class="keyword"&gt;end&lt;/span&gt;

h.to_xml(&lt;span class="symbol"&gt;:root&lt;/span&gt;=&amp;gt;&lt;span class="string"&gt;&lt;span class="delimiter"&gt;&amp;quot;&lt;/span&gt;&lt;span class="content"&gt;parent_object&lt;/span&gt;&lt;span class="delimiter"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we&amp;#8217;re actually still using a form of &lt;code class="CodeRay"&gt;to_xml&lt;/code&gt; &amp;#8212; it&amp;#8217;s just &lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;Hash&lt;/span&gt;.to_xml&lt;/code&gt; instead.&lt;/p&gt;
&lt;p&gt;There are some ways this could be streamlined, such as using &lt;code class="CodeRay"&gt;parent.attributes&lt;/code&gt; to pull the list of attributes, or even an array of wanted attributes. I skipped that here to make the example easier to understand.&lt;/p&gt;
&lt;p&gt;This method does have a couple benefits. One is that you can use more intelligent logic in regard to selecting the &lt;code class="CodeRay"&gt;child_objects&lt;/code&gt; to include. For example: &lt;code class="CodeRay"&gt;parent.child_objects.all(&lt;span class="symbol"&gt;:limit&lt;/span&gt;=&amp;gt;&lt;span class="integer"&gt;10&lt;/span&gt;, &lt;span class="symbol"&gt;:order&lt;/span&gt;=&amp;gt;&lt;span class="string"&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;span class="content"&gt;id desc&lt;/span&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;/span&gt;)&lt;/code&gt; if you wanted just the last ten child objects.&lt;/p&gt;
&lt;p&gt;Beyond that, and the focus of this posting, I found that the above resulted in a 2-3x speed increase. While I haven&amp;#8217;t dug really far into the matter, it appears that the Rails magic that figures out what associations to include and the associated reflection has a heavy overhead.&lt;/p&gt;
&lt;p&gt;I still use &lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="constant"&gt;Base&lt;/span&gt;.to_xml&lt;/code&gt; much of the time, but the above option is nice for frequently requested actions with large, deep object trees.&lt;/p&gt;</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting39</id>
    <published>2009-10-26T19:14:00Z</published>
    <updated>2009-10-26T19:14:00Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2009/10/measuring-activeresource-response-times-ruby-rails"/>
    <title>Measuring ActiveResource Response Times (Rails Performance Series)</title>
    <content type="html">&lt;p&gt;This is a quick one, but if your Rails app uses ActiveResource as a client, it can be really useful to know something about each ARes request.&lt;/p&gt;
&lt;p&gt;ActiveResource actually has some built-in logging of what requests are made, the response size, and how long it took. But, it&amp;#8217;s not enabled. The good news is, it&amp;#8217;s easy to turn it on.&lt;/p&gt;
&lt;p&gt;I like to make ActiveResource&amp;#8217;s logging match the rest of my environment &amp;#8212; that is, it will log when in development mode and not in production mode.&lt;/p&gt;
&lt;p&gt;Add the following to a file in &lt;code class="CodeRay"&gt;config/initializers/&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;ActiveResource&lt;/span&gt;::&lt;span class="constant"&gt;Base&lt;/span&gt;.logger = &lt;span class="constant"&gt;ActionController&lt;/span&gt;::&lt;span class="constant"&gt;Base&lt;/span&gt;.logger&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Obviously, if you want it to go to a different log file, or function at a different log level, you can create a new Logger instance instead.&lt;/p&gt;
&lt;p&gt;The logging output will look something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;GET&lt;/span&gt; http&lt;span class="symbol"&gt;:/&lt;/span&gt;/api-server.com/resources.xml
--&amp;gt; &lt;span class="integer"&gt;200&lt;/span&gt; &lt;span class="constant"&gt;OK&lt;/span&gt; (&lt;span class="integer"&gt;1526&lt;/span&gt; &lt;span class="integer"&gt;69&lt;/span&gt;ms)&lt;/code&gt;&lt;/pre&gt;
&lt;a id="more"&gt;&lt;/a&gt;
</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting38</id>
    <published>2009-10-21T15:08:00Z</published>
    <updated>2009-10-21T15:08:00Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2009/10/rails-xml-parsing-ruby-on-rails-performance-series"/>
    <title>Rails XML Parsing (Rails Performance Series)</title>
    <content type="html">&lt;p&gt;In part 3 of my Ruby on Rails Performance Series we&amp;#8217;ll look at how to improve the parsing speed of &lt;span class="caps"&gt;XML&lt;/span&gt; content.&lt;/p&gt;
&lt;p&gt;Rails will automatically parse &lt;span class="caps"&gt;XML&lt;/span&gt; in two key places:&lt;/p&gt;
&lt;p&gt;1. When &lt;span class="caps"&gt;XML&lt;/span&gt; content is sent to a controller/action pair via &lt;span class="caps"&gt;POST&lt;/span&gt; or &lt;span class="caps"&gt;PUT&lt;/span&gt;. (Your app is an &lt;span class="caps"&gt;API&lt;/span&gt; server.)&lt;br /&gt;
2. When &lt;span class="caps"&gt;XML&lt;/span&gt; content is received from an ActiveResource request. (Your app is an &lt;span class="caps"&gt;API&lt;/span&gt; client.)&lt;/p&gt;
&lt;p&gt;The default &lt;span class="caps"&gt;XML&lt;/span&gt; parser is &lt;span class="caps"&gt;REXML&lt;/span&gt;. It&amp;#8217;s written in pure Ruby which makes it very portable. And slow. Very slow.&lt;/p&gt;
&lt;p&gt;So, we need to replace &lt;span class="caps"&gt;REXML&lt;/span&gt; with something faster. The key is the &lt;code class="CodeRay"&gt;libxml-ruby&lt;/code&gt; gem which is just a binding to the very fast libxml2 C library.&lt;/p&gt;
&lt;p&gt;libxml-ruby has a different set of methods from &lt;span class="caps"&gt;REXML&lt;/span&gt; and is not drop in compatible. If you&amp;#8217;re planning on moving your own &lt;span class="caps"&gt;REXML&lt;/span&gt; parsing code to libxml-ruby, it will take some work. However, it&amp;#8217;s worth it. In one particular project, I had about a 100x speed improvement!&lt;/p&gt;
&lt;p&gt;Rails, as it often does, makes the switch to libxml-ruby super easy. First, install the gem (requires a functioning compiler, the libxml2 library, and headers for libxml2):&lt;/p&gt;
&lt;p&gt;&lt;code class="CodeRay"&gt;gem install libxml-ruby&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Then, just add a new file to &lt;code class="CodeRay"&gt;config/initializers/&lt;/code&gt;. Let&amp;#8217;s call it &lt;code class="CodeRay"&gt;config/initializers/xml_config.rb&lt;/code&gt;. In that file, put this:&lt;/p&gt;
&lt;p&gt;&lt;code class="CodeRay"&gt;&lt;span class="constant"&gt;ActiveSupport&lt;/span&gt;::&lt;span class="constant"&gt;XmlMini&lt;/span&gt;.backend = &lt;span class="string"&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;span class="content"&gt;LibXML&lt;/span&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This was only added in recent versions of Rails, I believe in 2.3.&lt;/p&gt;
&lt;p&gt;Restart your Rails app server(s) and off you go!&lt;/p&gt;
&lt;a id="more"&gt;&lt;/a&gt;
</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting37</id>
    <published>2009-10-14T15:51:00Z</published>
    <updated>2009-10-14T15:51:00Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2009/10/ruby-xml-generation-rails-performance-series"/>
    <title>Ruby XML Generation (Rails Performance Series)</title>
    <content type="html">&lt;p&gt;The standard way to build &lt;span class="caps"&gt;XML&lt;/span&gt; in Ruby is with the Builder gem.&lt;/p&gt;
&lt;p&gt;Builder iterates over each character in the output text to determine if it needs to escape it. When generating larger &lt;span class="caps"&gt;XML&lt;/span&gt; documents, this can be quite slow.&lt;/p&gt;
&lt;p&gt;Thankfully, the solution is already available via the &lt;code class="CodeRay"&gt;fast_xs&lt;/code&gt; gem. This gem does compile an extension, so you&amp;#8217;ll need a working compiler.&lt;/p&gt;
&lt;p&gt;&lt;code class="CodeRay"&gt;gem install fast_xs&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Then just require it after Builder:&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;require &lt;span class="string"&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;span class="content"&gt;builder&lt;/span&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;/span&gt;
require &lt;span class="string"&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;span class="content"&gt;fast_xs&lt;/span&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It will be used automatically by Builder going forward.&lt;/p&gt;
&lt;p&gt;If your app is built on Rails, it&amp;#8217;s even easier. Just install the gem&amp;#8212;that&amp;#8217;s it! Recent versions of Rails will auto-detect the existence of &lt;code class="CodeRay"&gt;fast_xs&lt;/code&gt; and enable it.&lt;/p&gt;
&lt;a id="more"&gt;&lt;/a&gt;
</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting36</id>
    <published>2009-10-07T17:32:00Z</published>
    <updated>2009-10-08T17:53:59Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2009/10/ruby-on-rails-performance-series-intro-yaml"/>
    <title>Ruby on Rails Performance Series: Intro and all about YAML</title>
    <content type="html">&lt;p&gt;Lately I&amp;#8217;ve been doing a lot of &lt;span class="caps"&gt;API&lt;/span&gt; type work in Rails &amp;#8212; both client and server. It mostly uses Rails&amp;#8217; default &lt;span class="caps"&gt;XML&lt;/span&gt; generation and consumption.&lt;/p&gt;
&lt;p&gt;Along the way I&amp;#8217;ve discovered that the default performance of &lt;span class="caps"&gt;XML&lt;/span&gt; and related things on Ruby and Rails is, shall we say, less than amazing. On the bright side, there are a number of ways to improve matters.&lt;/p&gt;
&lt;p&gt;So, I&amp;#8217;m going to do a series on improving the performance of Rails&amp;#8217; &lt;span class="caps"&gt;API&lt;/span&gt;-related stack.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll start off with serialized columns in ActiveRecord.&lt;/p&gt;
&lt;a id="more"&gt;&lt;/a&gt;
&lt;p&gt;Serialized columns are a way to store arbitrary information into a single &lt;span class="caps"&gt;TEXT&lt;/span&gt; column in your database. They probably shouldn&amp;#8217;t be used too often, but they are useful at times.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;YAML&lt;/span&gt; is the storage format for these serialized columns. They are defined like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;serialize &lt;span class="symbol"&gt;:some_column&lt;/span&gt;, &lt;span class="constant"&gt;Hash&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Easy enough. The column &lt;code class="CodeRay"&gt;some_column&lt;/code&gt; will be a native Ruby hash and yet will be serialized into a single &lt;span class="caps"&gt;TEXT&lt;/span&gt; column in the database. As is so often the case with Rails, it just works.&lt;/p&gt;
&lt;p&gt;The serialization format is &lt;span class="caps"&gt;YAML&lt;/span&gt;. If you use Rails&amp;#8217; built-in &lt;code class="CodeRay"&gt;to_xml&lt;/code&gt; methods to turn your ActiveRecord object into &lt;span class="caps"&gt;XML&lt;/span&gt;, it will also use &lt;span class="caps"&gt;YAML&lt;/span&gt; to serialize that particular column into a string and then pass that string to the &lt;span class="caps"&gt;XML&lt;/span&gt; generator.&lt;/p&gt;
&lt;p&gt;This is at least consistent and works quite well. It&amp;#8217;s just slow.&lt;/p&gt;
&lt;p&gt;The core problem is that &lt;span class="caps"&gt;YAML&lt;/span&gt; does not perform particularly well. There are others online who have documented this in sufficient detail so I won&amp;#8217;t do so here.&lt;/p&gt;
&lt;p&gt;If your use of objects with serialized columns, either directly to the DB or via an &lt;span class="caps"&gt;API&lt;/span&gt; is low volume, the performance doesn&amp;#8217;t particularly matter. On the other hand, it may be a hidden performance drain if you have a higher level of activity.&lt;/p&gt;
&lt;p&gt;In my tests, converting a Hash to &lt;span class="caps"&gt;YAML&lt;/span&gt; takes between 4 and 5 times as long as converting &lt;span class="caps"&gt;YAML&lt;/span&gt; back to a Hash.&lt;/p&gt;
&lt;p&gt;In the case I&amp;#8217;ve been working on, the database records are read often but written rarely. However, &lt;span class="caps"&gt;API&lt;/span&gt; traffic is much higher and performance there is critical.&lt;/p&gt;
&lt;p&gt;Because, performance at the DB layer isn&amp;#8217;t a big bottleneck here based on actual usage patterns, the decision was to leave the database records alone and continue to use the default serialization mechanism there.&lt;/p&gt;
&lt;p&gt;However, using &lt;span class="caps"&gt;YAML&lt;/span&gt; inside the API&amp;#8217;s &lt;span class="caps"&gt;XML&lt;/span&gt; output had to go. Some &lt;span class="caps"&gt;API&lt;/span&gt; responses have 100 or more individual instances of the hashes that are currently being serialized with &lt;span class="caps"&gt;YAML&lt;/span&gt;. The next question was, what does it get changed to?&lt;/p&gt;
&lt;p&gt;There are a few options: a) &lt;span class="caps"&gt;JSON&lt;/span&gt;, b) Marshal, c) nested &lt;span class="caps"&gt;XML&lt;/span&gt;, and d) some other custom serialization.&lt;/p&gt;
&lt;p&gt;Each has pros and cons. Interestingly, all are faster than &lt;span class="caps"&gt;YAML&lt;/span&gt;. &lt;span class="caps"&gt;JSON&lt;/span&gt; is around 3x faster but doesn&amp;#8217;t have quite the richness of object mapping. Marshal is really fast (about 9x over &lt;span class="caps"&gt;YAML&lt;/span&gt;) but has an almost binary-like output and isn&amp;#8217;t very portable. Nested &lt;span class="caps"&gt;XML&lt;/span&gt; may or may not map into something desirable on the client side and may not be much faster. Custom formats may also be limited in object representation and take the most work to implement, both client and server.&lt;/p&gt;
&lt;p&gt;If you had a simple array of strings, a custom format like &amp;#8220;one,two,three&amp;#8221; would be fairly straight forward. Complex objects, like entire referenced models would make this unworkable though.&lt;/p&gt;
&lt;p&gt;So what&amp;#8217;d I end up using? In this case, &lt;span class="caps"&gt;JSON&lt;/span&gt;. It&amp;#8217;s much faster, easy to work with, and the data that needs to be transmitted is in a very simply format: all key to numeric value pairs.&lt;/p&gt;
&lt;p&gt;Unfortunately, this can be a bit messy in both the client and server (although not difficult).&lt;/p&gt;
&lt;p&gt;On the server, the attribute needs to be converted to &lt;span class="caps"&gt;JSON&lt;/span&gt; before &lt;code class="CodeRay"&gt;to_xml&lt;/code&gt; sees it. The decision was to create an alternate version of the attribute (&amp;#8220;options&amp;#8221;) that was already in &lt;span class="caps"&gt;JSON&lt;/span&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;&lt;span class="keyword"&gt;def&lt;/span&gt; &lt;span class="function"&gt;options_json&lt;/span&gt;
  options.to_json
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then you&amp;#8217;ll need a couple options added to &lt;code class="CodeRay"&gt;to_xml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;to_xml(&lt;span class="symbol"&gt;:methods&lt;/span&gt;=&amp;gt;&lt;span class="symbol"&gt;:options_json&lt;/span&gt;, &lt;span class="symbol"&gt;:except&lt;/span&gt;=&amp;gt;&lt;span class="symbol"&gt;:options&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This tells &lt;code class="CodeRay"&gt;to_xml&lt;/code&gt; to skip the standard options rendering and include your custom method.&lt;/p&gt;
&lt;p&gt;On the client side, in your model class inherited from ActiveResource::Base, add:&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;&lt;span class="keyword"&gt;def&lt;/span&gt; &lt;span class="function"&gt;options&lt;/span&gt;
  &lt;span class="instance-variable"&gt;@options&lt;/span&gt; ||= &lt;span class="constant"&gt;ActiveSupport&lt;/span&gt;::&lt;span class="constant"&gt;JSON&lt;/span&gt;.decode(attributes[&lt;span class="string"&gt;&lt;span class="delimiter"&gt;&amp;quot;&lt;/span&gt;&lt;span class="content"&gt;options_json&lt;/span&gt;&lt;span class="delimiter"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;] || &lt;span class="string"&gt;&lt;span class="delimiter"&gt;&amp;quot;&lt;/span&gt;&lt;span class="content"&gt;{}&lt;/span&gt;&lt;span class="delimiter"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As noted above, some &lt;span class="caps"&gt;API&lt;/span&gt; response here had dozens of objects that needed to be serialized. Moving to &lt;span class="caps"&gt;JSON&lt;/span&gt; over &lt;span class="caps"&gt;YAML&lt;/span&gt; reduced those &lt;span class="caps"&gt;API&lt;/span&gt; calls by about 25% (roughly 4 seconds to 3 seconds).&lt;/p&gt;</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting35</id>
    <published>2009-08-09T19:25:00Z</published>
    <updated>2009-08-09T19:42:01Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2009/08/ebay-paypal-privacy-failures-unfriendly-websites"/>
    <title>ebay + Paypal : privacy failures and unfriendly websites</title>
    <content type="html">&lt;p&gt;I bought something on ebay this morning (a 60-year old book). That&amp;#8217;s not particularly exciting. But in the process of trying to pay for it, I had several observations.&lt;/p&gt;
&lt;p&gt;1. Finding and buying the item on ebay was fairly straight-forward. I&amp;#8217;m not sure why the item didn&amp;#8217;t show up in the main search results, but several acceptable options did show up in the &amp;#8220;Here&amp;#8217;s what people have in their stores&amp;#8221; section. Good enough.&lt;/p&gt;
&lt;p&gt;2. ebay continues to try to force everything through Paypal. I was going to do that this time anyway, but the lack of choice is below average.&lt;/p&gt;
&lt;p&gt;3. ebay and Paypal are still run as separate sites. So when I click on checkout from ebay, it sends me over to Paypal. That&amp;#8217;s the theory anyway. Instead I get a giant error message telling me that Paypal has experienced an error and that I should wait and try again.&lt;/p&gt;
&lt;p&gt;So I wait. And try again. And it fails again.&lt;/p&gt;
&lt;p&gt;I wait some more. Same error. &amp;#8220;Odd,&amp;#8221; I think.&lt;/p&gt;
&lt;p&gt;Months ago (years maybe) I started configuring my web browsers to only allow cookies back to the source domain. This is mostly a privacy protection attempt as it keeps volumes of advertising cookies from being accepted.&lt;/p&gt;
&lt;p&gt;On occasion some site exhibits bad behavior by relying on these cross-domain cookies. Wondering if ebay/Paypal is one of these errant sites, I dig into my browser&amp;#8217;s cookies.&lt;/p&gt;
&lt;p&gt;Sure enough, cookies that are shared between ebay.com and paypal.com have been refused. I enable just the select cookie and try the checkout again. Now it works.&lt;/p&gt;
&lt;p&gt;While reviewing all the cookies, there were more than a dozen various advertising cookies that ebay also tried to set. This merely justifies my general decision to block such things.&lt;/p&gt;
&lt;p&gt;The failure here is really two-fold. One is not having a better way to hand off the session data between domains &amp;#8212; something in the url would seem appropriate enough.&lt;/p&gt;
&lt;p&gt;The second is having an unhelpful, misleading error message.&lt;/p&gt;
&lt;p&gt;4. Lastly, Paypal still, after all these years, refuses to let me set a credit card as my default payment method, instead forcing me to go through several clicks and multiple pages to override it for each purchase.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m reminding yet again why I buy from ebay and use Paypal so infrequently. It&amp;#8217;s just a hassle.&lt;/p&gt;
&lt;a id="more"&gt;&lt;/a&gt;
</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
  <entry>
    <id>tag:iprog.com,2007:Posting34</id>
    <published>2009-06-26T02:34:00Z</published>
    <updated>2009-11-12T17:38:36Z</updated>
    <link rel="alternate" type="text/html" href="http://iprog.com/posting/2009/06/disabling_rails_asset_timestamps"/>
    <title>Disabling Rails asset timestamps</title>
    <content type="html">&lt;p&gt;One of the common questions in Rails land is how to disable the funny numbers at the end of images and &lt;span class="caps"&gt;CSS&lt;/span&gt; stylesheets. Let&amp;#8217;s explore what exactly those numbers are, why they&amp;#8217;re sometimes helpful, and how to turn them off when they&amp;#8217;re not.&lt;/p&gt;
&lt;a id="more"&gt;&lt;/a&gt;
&lt;p&gt;Those numbers are timestamps &amp;#8212; the number of seconds since January 1, 1970. The time is the last time the image or stylesheet was last modified.&lt;/p&gt;
&lt;p&gt;So if the time is June 26, 2009 at 02:39:24, then the timestamp would be 1245983964.&lt;/p&gt;
&lt;p&gt;Great! Now, why do we care?&lt;/p&gt;
&lt;p&gt;Rails appends these timestamps to cause web browsers to think the files are unique. This causes a browser to not use its cache and to reload the file. That is, even those these reference the same file, the browser thinks they are different.&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;&lt;span class="regexp"&gt;&lt;span class="delimiter"&gt;/&lt;/span&gt;&lt;span class="content"&gt;stylesheets&lt;/span&gt;&lt;span class="delimiter"&gt;/&lt;/span&gt;&lt;span class="modifier"&gt;s&lt;/span&gt;&lt;/span&gt;tyles.css?&lt;span class="integer"&gt;1245981180&lt;/span&gt;
&lt;span class="regexp"&gt;&lt;span class="delimiter"&gt;/&lt;/span&gt;&lt;span class="content"&gt;stylesheets&lt;/span&gt;&lt;span class="delimiter"&gt;/&lt;/span&gt;&lt;span class="modifier"&gt;s&lt;/span&gt;&lt;/span&gt;tyles.css?&lt;span class="integer"&gt;1245983964&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&amp;#8217;s backtrack just a little. Web browsers, in an effort to show you a page faster, will keep a copy of recently loaded files either in memory or on your hard disk. If it is just told to load &lt;code class="CodeRay"&gt;&lt;span class="regexp"&gt;&lt;span class="delimiter"&gt;/&lt;/span&gt;&lt;span class="content"&gt;stylesheets&lt;/span&gt;&lt;span class="delimiter"&gt;/&lt;/span&gt;&lt;span class="modifier"&gt;s&lt;/span&gt;&lt;/span&gt;tyles.css&lt;/code&gt; and you change the &lt;span class="caps"&gt;CSS&lt;/span&gt; file, the web browser might use the old copy in the cache rather than load the new version of the file.&lt;/p&gt;
&lt;p&gt;However, by appending these timestamps on the end, when the file changes, the browser will also think it&amp;#8217;s a different file and will always load the new copy.&lt;/p&gt;
&lt;p&gt;So it&amp;#8217;s really a useful feature most of the time.&lt;/p&gt;
&lt;p&gt;Sometimes it can get in the way (or you may just think it&amp;#8217;s ugly and aren&amp;#8217;t concerned about browser caches. For those instances, you can turn it off.&lt;/p&gt;
&lt;pre&gt;&lt;code class="CodeRay"&gt;&lt;span class="predefined-constant"&gt;ENV&lt;/span&gt;[&lt;span class="string"&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;span class="content"&gt;RAILS_ASSET_ID&lt;/span&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;/span&gt;] = &lt;span class="string"&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;span class="delimiter"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You should add a new file in your &lt;code class="CodeRay"&gt;config/initializers/&lt;/code&gt; directory to put that in. Let&amp;#8217;s call it no_timestamps.rb. So, in &lt;code class="CodeRay"&gt;config/initializers/no_timestamps.rb&lt;/code&gt;, just add the one line above. Then, restart your Rails server.&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it! No more appended timestamps.&lt;/p&gt;
&lt;p&gt;Updated 11/12/2009: As noted in the comments, I had incorrectly suggested using cache_asset_timestamps=false. Corrected the above to show the proper way to do this.&lt;/p&gt;</content>
    <author>
      <name>tm</name>
    </author>
  </entry>
</feed>
