From: Eric Wong Date: 2015-08-04T08:22:46+00:00 Subject: [ruby-core:70240] Re: [Ruby trunk - Feature #11375] Decreased Object Allocation in Pathname.rb richard.schneeman@gmail.com wrote: > I think I figured out why i'm not getting emails and I believe I've > fixed the issue. Sorry again for the delayed response. No worries, I barely have Internet access the past few weeks. I'm only subscribed to ruby-core, though. I understand your pain, and I've had to do optimizations like these to fix known regressions such as https://bugs.ruby-lang.org/issues/10628 I hated making that commit, but in the end it's likely to affect someone, so I made the changes anyways :< Based on my comments on [ruby-core:70234], I would like you to reevaluate the freeze changes on the case/when statements with: a) multiple 'when' statements b) non-matching 'when' statements I'm alright if we uglify our code a little for consistent performance gains, but not to introduce performance regressions :) So I'm more likely to apply the patch with the case/when stuff sorted out. And I think there is at least one place where two `== x.freeze' checks could be converted into a case/when without introducing the ugly `freeze' word :) > If we're talking optimizations, I would love for these two expressions > to be roughly equivalent: > require 'benchmark/ips' > > world = "world" > Benchmark.ips do |x| > x.report("normal") { "hello #{world}" } > x.report("freeze") { "hello ".freeze + world } > end The normal way (concatstrings VM instruction) should already be faster if there's a slightly more complex concatenation. It's not allocation costs, just costs of instructions in this case. For example, add a trailing space at the end to see what you get: x.report("normal") { "hello #{world} " } x.report("freeze") { "hello ".freeze + world << " " } # gross! I'll see if I can make your original case faster, but it's already close between them and it would be hard without introducing regressions elsewhere. It would be great if we could implement the putstring VM instruction lazily, but I wonder how many C exts that would break...