From: richard.schneeman@... Date: 2015-08-03T22:23:30+00:00 Subject: [ruby-core:70232] [Ruby trunk - Feature #11375] Decreased Object Allocation in Pathname.rb Issue #11375 has been updated by Richard Schneeman. 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. I agree we should be improving and optimizing Ruby so that the average developer can write code in the most readable/desirable way and not have to worry about sacrificing readability for speed. Unfortunately we have far more Ruby developers in the world than we have C developers who are capable of making speed optimizations. I'm very interested in speed. I had a PR recently to Rails where I removed many object allocations https://github.com/rails/rails/pull/21057 this was a roughly 10% increase in a Rails app by only manipulating existing Ruby code in Rails. On one hand I would like to not have to do some of these things like worrying about nil.to_s allocating a new empty string only to be concatenated to other strings. However I cannot always wait for someone else to optimize this in Ruby and I lack the tools, and capabilities to contribute a permanent fix upstream to Ruby (i'm working on getting there, but it's very slow). 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 ``` If another patch gives me these reduced allocations without needing to `.freeze` everything i'm in favor of that. I'm not sure if I have a good solution for the problem of benchmarking already optimized code. ---------------------------------------- Feature #11375: Decreased Object Allocation in Pathname.rb https://bugs.ruby-lang.org/issues/11375#change-53662 * Author: Richard Schneeman * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Pathname.rb has many string literals that are not mutated while being called. We can reduce object allocation and increase program speed by freezing these string literals. I've attached a patch that adds `.freeze` to all non-mutated string literals in `ext/pathname/lib/pathname.rb`. Tests on `test/pathname/test_pathname.rb` pass ---Files-------------------------------- ruby-changes.patch (6.42 KB) -- https://bugs.ruby-lang.org/