From: James Tucker Date: 2008-03-29T00:39:11+09:00 Subject: Re: Why Ruby do not optimize code at all? On 28 Mar 2008, at 15:20, Chiyuan Zhang wrote: > I think the problem is that you can redefine any method > of any class at any time. If some one redefined the `*' > method of Fixnum, your code won't pass, I guess. More than this, it may not even be a fixnum or string or whatever instance class. Not even constants are 'constant'. > 2008/3/28, Heesob Park : >> Hi, >> >> I do not undertand why ruby doesn't do any optimization at all during >> parsing time. The AST defines very little, if anything, about what the code will do, until it is considered as a whole. (Which may not be possible if there is any eval(). >> Some optimization maybe affects debugging process. >> Nevertheless, it seems "Constant folding" is not harmful but helpful. >> I tried some pre-evaluation of constant or literal nodes in parsing >> time. As said by others, open classes means parse time != run time, at all. >> Consider this code >> >> i = 320 * 200 * 32 The only time I could see this being sensible to do is if it's actually closer to: A = 320.freeze B = 200.freeze C = 32.freeze i = A * B * C However, the realistic implications are that freeze plus the discussed optimization causes a pre-processor fixed definition of the methods A.*() and B.*(). In MRI, one can unfreeze too, so this still isn't "safe", and the source code does not resemble what is run. This will kick people most, when reading someone else code. >> >> What do you think of the minimum optimization? I think it shows up immediately in profiling if it's relevant, as if it is relevant, you will highly likely see a large number of calls to Fixnum#*, and in some of the given examples, this is exactly where you'd manually unroll to optimize for speed, or alternatively: BIG_NUMBER = 100_000 * 100 SOMETHING_THAT_SHOULD_BE_A_CONSTANT = BIG_NUMBER * 'some string' def foo SOMETHING_THAT_SHOULD_BE_A_CONSTANT end >> Regards, >> >> Park Heesob >> >> -- >> Posted via http://www.ruby-forum.com/.