From: Brian Candler Date: 2003-04-14T01:51:49+09:00 Subject: Re: Need IO Optimization help On Mon, Apr 14, 2003 at 01:34:42AM +0900, gabriele renzi wrote: > >You can eliminate the extra argument by doing > > > > mycounter = mycounter.succ > > well I suppose his point is that not using Fixnum you can have > Counter.succ!, without the need for an assignment , written directly > in C, possibly faster.. > (and though possibly low level optimized as in "inc reg" vs "add > reg,1" ) I don't think you'll get anywhere close to where that would make any difference. The big overhead is in the dispatch to .succ or .succ! where the interpreter has to walk through the chain of object classes and superclasses to find the correct method to call. Once you have invoked a method and got a return 'VALUE', storing that value in a local variable is minimal cost. You might be able to optimise this using Polymorphic Inline Caches (Google will give you some papers on this), but in the general case I don't think you'll ever be able to get as far as "inc reg" without some extremely serious data-flow analysis. If you'd like to try writing a Ruby to C (or C++) converter, I think you'll get the picture :-) The problems include: - any method you call could decide to redefine any method of any other object (including Fixnum#+ and Fixnum#succ) at any time. - any method which you pass a block to, can keep a copy of that block and call it at any time in the future In fact it's hard enough even to answer the question "at what point is this object no longer referenced?" that Ruby does mark-and-sweep periodic garbage collection rather than disposing of objects as soon as they are no longer used. Regards, Brian.