<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>time to bleed by Joe Damato - Latest Comments in 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.disqus.com/</link><description></description><atom:link href="https://timetobleed.disqus.com/6_line_eventmachine_bugfix_2x_faster_gc_1300_requestssec/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 20 Oct 2009 05:52:30 -0000</lastBuildDate><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-20616450</link><description>&lt;p&gt;This is way over my head. :(&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Anon</dc:creator><pubDate>Tue, 20 Oct 2009 05:52:30 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-11784333</link><description>&lt;p&gt;Interesting post. I have stumbled and twittered this for my friends. Hope others find it as interesting as I did.&lt;/p&gt;&lt;p&gt;Darek from &lt;a href="http://www.informationex.com" rel="nofollow noopener" target="_blank" title="http://www.informationex.com"&gt;classifieds posting service&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">darekjones</dc:creator><pubDate>Fri, 26 Jun 2009 14:18:08 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-9329404</link><description>&lt;p&gt;I'm always very impressed when people go to these lengths to track a problem. Very very nice work, and thanks!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Glenn Gillen</dc:creator><pubDate>Thu, 14 May 2009 13:20:32 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8894272</link><description>&lt;p&gt;Awesome post! I really like such a write-ups!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">radarek</dc:creator><pubDate>Fri, 01 May 2009 10:15:10 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8863145</link><description>&lt;p&gt;Just for extreme clarity:&lt;/p&gt;&lt;p&gt;This patch affects ruby code running on EventMachine with the epoll reactor.&lt;/p&gt;&lt;p&gt;  require 'eventmachine'; EM.epoll; &lt;a href="http://EM.run" rel="nofollow noopener" target="_blank" title="EM.run"&gt;EM.run&lt;/a&gt; {} # 800k stack on linux on old EM&lt;/p&gt;&lt;p&gt;There may be a similar patch coming for the kqueue reactor, but that is less affected (40k vs. 800k).&lt;/p&gt;&lt;p&gt;  require 'eventmachine'; EM.kqueue; &lt;a href="http://EM.run" rel="nofollow noopener" target="_blank" title="EM.run"&gt;EM.run&lt;/a&gt; {} # 40k stack on OSX&lt;/p&gt;&lt;p&gt;The issue does not exist in any extreme with the select reactor.&lt;/p&gt;&lt;p&gt;  require 'eventmachine'; &lt;a href="http://EM.run" rel="nofollow noopener" target="_blank" title="EM.run"&gt;EM.run&lt;/a&gt; {}&lt;/p&gt;&lt;p&gt;You can see the effects of the issue most drastically when using EventMachines defer method, because it spawns up 20 threads, each of which will carry a heavy stack.&lt;/p&gt;&lt;p&gt;I have recently done some research on performance metrics of ruby's green thread implementation. Whilst there is a much more significant impact to ruby's performance when the stack size is large, the specific impact of the number of threads does not become noticeable until high numbers of threads (two orders of magnitude and above (100+ threads)). The choice of 20 threads seems sensible, given this research.&lt;/p&gt;&lt;p&gt;Each spawned thread carries a copy of the stack, though, so if you spawn threads 'later' in your execution stack (like Thin does), then you will see extra performance degradation from these issues. As such the real highlight comes from an app like this:&lt;/p&gt;&lt;p&gt;  require 'eventmachine'; EM.epoll&lt;br&gt;  require 'sinatra'; def (Sinatra::Application).deferred?(env); true; end&lt;br&gt;  get '/' do escape_html Thread.current.inspect end&lt;/p&gt;&lt;p&gt;Some of the performance impact can be slightly subdued by doing the following:&lt;/p&gt;&lt;p&gt;  EM.defer {}&lt;/p&gt;&lt;p&gt;Prior to thin loading, as the 20 threads for doing work in will be sitting on a significantly smaller stack. The main eventmachine stack would still be very large though, so this 'quick fix' isn't really a fix at all.&lt;/p&gt;&lt;p&gt;Some of the general impacts of stack size on ruby performance are demonstrated here:&lt;br&gt;&lt;a href="http://pastie.textmate.org/private/iynyujlrzorq7pcgjtwuta" rel="nofollow noopener" target="_blank" title="http://pastie.textmate.org/private/iynyujlrzorq7pcgjtwuta"&gt;http://pastie.textmate.org/...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;And can be generated like so:&lt;br&gt;&lt;a href="http://gist.github.com/104213" rel="nofollow noopener" target="_blank" title="http://gist.github.com/104213"&gt;http://gist.github.com/104213&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Maybe I'll collaborate or separately post some ruby programmer oriented explanations of these posts soon. :)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">raggi</dc:creator><pubDate>Thu, 30 Apr 2009 10:50:24 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8852877</link><description>&lt;p&gt;Cool story, bro. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Kate</dc:creator><pubDate>Thu, 30 Apr 2009 02:11:01 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8852817</link><description>&lt;p&gt;I spend most of my time programming in higher level language, but have always wanted to learn more about lower level tools like gdb, nm, objdump, etc. What resources would you recommend for learning these tools? I have tried reading guides online, but I have always found them lacking, particularly on how one goes about creating a mental model of the running system from all the data that can be seen with the tools.&lt;/p&gt;&lt;p&gt;Also, how does one go about honing their assembly skills? Other than the architecture class in college where I learned MIPS, I never had the opportunity to write any assembly, not in MIPS and not in x86. Do you have any recommendations on that too?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Duncan</dc:creator><pubDate>Thu, 30 Apr 2009 02:05:50 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8852547</link><description>&lt;p&gt;Another ruby superhero saves the day. Why not use something where you don't have to debug language internals? Sounds awful productive to me.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Kraln</dc:creator><pubDate>Thu, 30 Apr 2009 01:41:11 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8834652</link><description>&lt;p&gt;Thanks for reading guys!&lt;/p&gt;&lt;p&gt;Don't forget to subscribe, lots of cool posts in the queue =] &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 18:33:53 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8828694</link><description>&lt;p&gt;Thanks! If you like slightly-larger changes with big results you should check out my other post about a fibers implementation for Ruby 1.8.7: &lt;a href="http://timetobleed.com/fibers-implemented-for-ruby-1867/" rel="nofollow noopener" target="_blank" title="http://timetobleed.com/fibers-implemented-for-ruby-1867/"&gt;http://timetobleed.com/fibe...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Thanks for reading and don't forget to subscribe =]&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 18:19:28 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8826949</link><description>&lt;p&gt;Nice work. I love small changes with big results. And thanks for the quick intro to gdb.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Brian Morearty</dc:creator><pubDate>Wed, 29 Apr 2009 17:30:51 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8826487</link><description>&lt;p&gt;Why doesn't it seem likely? I've fixed a couple bugs in the Ruby threading implementation and implemented Fibers for Ruby 1.8.7. So I've messed with the threading implementation quite a bit =]&lt;/p&gt;&lt;p&gt;Anyway, please see:&lt;/p&gt;&lt;p&gt;&lt;a href="http://github.com/ice799/matzruby/blob/039eaea42b63e1a1ed2e972a5baacb60d5b4a9d3/eval.c#L10718" rel="nofollow noopener" target="_blank" title="http://github.com/ice799/matzruby/blob/039eaea42b63e1a1ed2e972a5baacb60d5b4a9d3/eval.c#L10718"&gt;http://github.com/ice799/ma...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://github.com/ice799/matzruby/blob/039eaea42b63e1a1ed2e972a5baacb60d5b4a9d3/eval.c#L10589" rel="nofollow noopener" target="_blank" title="http://github.com/ice799/matzruby/blob/039eaea42b63e1a1ed2e972a5baacb60d5b4a9d3/eval.c#L10589"&gt;http://github.com/ice799/ma...&lt;/a&gt;&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 17:15:44 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8826199</link><description>&lt;p&gt;Great post and a really nice job. I suffered from this and I was clueless but now I know better :)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">oldmoe</dc:creator><pubDate>Wed, 29 Apr 2009 17:06:30 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8825936</link><description>&lt;p&gt;I would check to make sure that thread-switch will cause a full memcpy of the whole stack. It doesn't seem likely.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jon</dc:creator><pubDate>Wed, 29 Apr 2009 16:59:24 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8824674</link><description>&lt;p&gt;Sounds great.  Hit me up.  lastname.firstname@gmail.com&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">James</dc:creator><pubDate>Wed, 29 Apr 2009 16:17:50 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8824413</link><description>&lt;p&gt;We can chat about this via e-mail if you like - this is difficult via blog comments.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 16:09:47 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8824207</link><description>&lt;p&gt;&amp;gt; However, stack allocating stuff is useful and *can* be done safely, even for large objects.&lt;/p&gt;&lt;p&gt;I'm not really concerned about memory use here.  (Although, haha, I think we can both agree that you should attempt to conserve memory.)  I'm concerned about virtual address space use.&lt;/p&gt;&lt;p&gt;Do you know of a threading implementation that does do this safely?  As far as I know (and again, I'm no expert), pthreads does not solve this problem.  If a function that uses a lot of memory in a frame can be called from any thread, all stacks have to be spaced at a distance wide enough in virtual memory to accomodate that frame, even if it's almost never called and never simultaneously.&lt;/p&gt;&lt;p&gt;I'm thinking of a large thread pool executing a variety of periodic tasks, among which is one that uses the large function or functions in question.  In such a pool pthread_attr_getstacksize for each thread in the pool must be at least as large as the largest possible stack generated by a call from that pool, even if the average stack is very small.  And that stack size affects the overall usage of virtual space by pthread stacks as a whole.&lt;/p&gt;&lt;p&gt;You can make it better by guaranteeing that the big function is only called from one thread, then setting the stack size of that thread much larger and setting all the rest small.  But in my inexperienced knowledge there is no thread implementation that will handle this detail automatically.  Please let me know if I'm wrong; I'd love to find that this is something I don't have to worry about.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">James</dc:creator><pubDate>Wed, 29 Apr 2009 16:03:38 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8823536</link><description>&lt;p&gt;I read the explanation and it makes perfect sense now. I haven't coded much in C++ since college, mostly .NET apps nowadays, so my analysis was admittedly going slower than I would've liked. Anyway thanks for the explanation. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">terry</dc:creator><pubDate>Wed, 29 Apr 2009 15:44:05 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8823492</link><description>&lt;p&gt;This speeds up Ruby's GC -if- you are using EventMachine. The patch fixed a bug in EventMachine which was slowing down Ruby's GC.&lt;/p&gt;&lt;p&gt;Does that clear it up?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 15:43:05 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8822400</link><description>&lt;p&gt;I am not sure if i understood this. Does this mean that this patches speeds up ruby's GC collection?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">mark</dc:creator><pubDate>Wed, 29 Apr 2009 15:35:48 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8822075</link><description>&lt;p&gt;Ah, yes, that makes sense, thanks ice.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ken Pratt</dc:creator><pubDate>Wed, 29 Apr 2009 15:32:59 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8821474</link><description>&lt;p&gt;Let me know if the explanation I gave Ken clears it up. I'll add a little more to the article itself in the "patch" section.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 15:22:59 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8821422</link><description>&lt;p&gt;AH, Ok.&lt;/p&gt;&lt;p&gt;Moving it into the class definition causes the object to be allocated on the heap when an object of the class is created with new.&lt;/p&gt;&lt;p&gt;The answer to your question is that it doesn't decrease the memory usage. It decreases the size of the stack because it moves the object over to the heap.&lt;/p&gt;&lt;p&gt;Ruby scans the process stack, not the process heap (it scans its own internal heap which is not the same as the process heap).&lt;/p&gt;&lt;p&gt;Does that make more sense? I'll add a little bit of info to the article in the "Patch" section above.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 15:21:44 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8821362</link><description>&lt;p&gt;@ice799 You are correct and my mistake, I missed the patch link when I originally looked over the article. However, like Ken I'm curious about how the patch works but now that I'm aware of the link I'll do some detective work of my own. :-)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">terry</dc:creator><pubDate>Wed, 29 Apr 2009 15:20:00 -0000</pubDate></item><item><title>Re: 6 Line EventMachine Bugfix = 2x faster GC, +1300% requests/sec</title><link>http://timetobleed.com/6-line-eventmachine-bugfix-2x-faster-gc-1300-requestssec/#comment-8821343</link><description>&lt;p&gt;OK, so I'll agree that it isn't as simple as I said it was. I should be more careful; I didn't think anyone would actually read this in the first place.&lt;/p&gt;&lt;p&gt;It's only a performance issue on certain threading implementations. It could be a portability issue, though.&lt;/p&gt;&lt;p&gt;It depends on what your invariant is - if you just need to stack allocate a huge amount to use and toss out once your function returns, I don't see that necessarily being problematic. If your invariant is that that stack frame doesn't return for a while (for some definition of "a while") it could be a problem.&lt;/p&gt;&lt;p&gt;If your argument is simply "using a lot of memory is bad" then sure I agree with that argument.&lt;/p&gt;&lt;p&gt;However, stack allocating stuff is useful and *can* be done safely, even for large objects.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe Damato (ice799)</dc:creator><pubDate>Wed, 29 Apr 2009 15:19:17 -0000</pubDate></item></channel></rss>