Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3473 (closed: fixed)

Opened 4 years ago

Last modified 3 years ago

[PATCH] Performance problems with .to_json as used for .rjs templates

Reported by: Thomas Fuchs <t.fuchs@wollzelle.com> Assigned to: Rick Olson
Priority: high Milestone: 1.2
Component: ActionPack Version: 1.0.0
Severity: critical Keywords: ajax rjs performance json
Cc:

Description

I've some rather large page updates going on via .rjs templates, and the .to_json method used causes performance problems (this is with edge rails).

Here's what script/console tells me:

>> a = "A"*1.megabyte
=> ...
>> Benchmark.measure { a.to_json }
=> #<Benchmark::Tms:0x25491a0 @utime=6.99, @cstime=0.0, @total=7.01, @cutime=0.0, @label="", @stime=0.02, @real=7.00696516036987>
>> Benchmark.measure { a.inspect }
=> #<Benchmark::Tms:0x2544600 @utime=0.0799999999999983, @cstime=0.0, @total=0.0799999999999983, @cutime=0.0, @label="", @stime=0.0, @real=0.0897819995880127>

So, inspect is almost 100x faster than to_json... :/

Attachments

to_json.diff (1.3 kB) - added by shugo on 07/11/06 03:44:16.

Change History

07/11/06 03:44:16 changed by shugo

  • attachment to_json.diff added.

07/11/06 03:47:47 changed by shugo@ruby-lang.org

  • summary changed from Performance problems with .to_json as used for .rjs templates to [PATCH] Performance problems with .to_json as used for .rjs templates.

I encountered the same problem and added the following code to config/environment.rb.

I attached the patch to_json.diff. Are there any reasons to encode multibyte characters?

07/11/06 03:49:01 changed by shugo@ruby-lang.org

Sorry, I forgot to paste the code.

class String
  JSON_ESCAPED = {
     "\010" =>  '\b',
     "\f" =>    '\f',
     "\n" =>    '\n',
     "\r" =>    '\r',
     "\t" =>    '\t',
     '"' =>     '\"',
     '\\' =>    '\\\\'
  }

  def to_json
    return '"' + gsub(/[\010\f\n\r\t"\\]/) { |s|
      JSON_ESCAPED[s]
    } + '"'
  end
end

08/18/06 07:56:36 changed by anonymous

  • priority set to highest.
  • type set to defect.
  • severity changed from normal to critical.
  • milestone set to 1.2.

Just thought I should bump this before it bites us all in the ass.

08/18/06 16:48:21 changed by madrobby

  • cc deleted.
  • keywords changed from ajax+rjs+performance+json to ajax rjs performance json.
  • summary changed from %5BPATCH%5D+Performance+problems+with+.to_json+as+used+for+.rjs+templates to [PATCH] Performance problems with .to_json as used for .rjs templates.

Fight spam

08/18/06 16:50:51 changed by madrobby

  • owner changed from sam@conio.net to Rick Olson.

Btw, shugo's code yields the following performance (Quad G5):

>> Benchmark.measure { a.inspect }
=> #<Benchmark::Tms:0x259ed4c @real=0.151921987533569, @utime=0.15, @cstime=0.0, @total=0.16, @cutime=0.0, @label="", @stime=0.01>
>> Benchmark.measure { a.to_json }
=> #<Benchmark::Tms:0x258cad4 @real=0.0250940322875977, @utime=0.00999999999999979, @cstime=0.0, @total=0.0199999999999998, @cutime=0.0, @label="", @stime=0.01>

Rick, can you have a look at this? :)

08/18/06 17:25:43 changed by madrobby

  • status changed from new to closed.
  • resolution set to fixed.