https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-06-03T09:20:52ZRuby Issue Tracking SystemRuby master - Bug #15895: String#gsub and String#sub should return original string if no substitution(s) have been made https://bugs.ruby-lang.org/issues/15895?journal_id=783102019-06-03T09:20:52Zashmaroli (Ashwin Maroli)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/78310/diff?detail_id=51945">diff</a>)</li></ul> Ruby master - Bug #15895: String#gsub and String#sub should return original string if no substitution(s) have been made https://bugs.ruby-lang.org/issues/15895?journal_id=783112019-06-03T10:19:44Zk0kubun (Takashi Kokubun)takashikkbn@gmail.com
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>I can understand why you want it, but that would be a breaking change for existing code which assumes that <code>gsub</code> always creates a new String instance and performs a destructive operation on the <code>gsub</code>'s return value. In that code the original string is suddenly modified once <code>gsub</code> behavior is changed as such.<br>
If you really need it, please consider proposing an option to force the behavior or another method.</p>
<p>Besides, please share a code which reflects your real-world use case which has <code>gsub</code> in a loop. The code and its benchmark results may encourage us to introduce such a feature. It may not be a bottleneck for your application and in that case the feature could be just a useless micro optimization. At least, I don't think we write a code like <code>'Hello World'.sub(/\d+/, 'x')</code>, and also you don't need to write <code>'Hello World'.gsub(/[<&>]/, html_entities_hash)</code> because I wrote the fast HTML escape method in Ruby core which is NOT using <code>gsub</code> <a href="https://github.com/ruby/ruby/pull/1164" class="external">https://github.com/ruby/ruby/pull/1164</a>.</p> Ruby master - Bug #15895: String#gsub and String#sub should return original string if no substitution(s) have been made https://bugs.ruby-lang.org/issues/15895?journal_id=783142019-06-03T16:10:48Zashmaroli (Ashwin Maroli)
<ul></ul><p>Thank you for pointing me to <code>CGI.escape_html</code>. I was not aware of it being faster than the <code>gsub</code> route. Moreover, you're right about the <code>gsub</code> usage not being a bottleneck in my codebase.</p>
<p>It simply resulted in numerous string allocations (as reported by the <code>memory_profiler</code> gem) that I wanted to reduce or eliminate.</p>
<p>As it turns out, <code>CGI.escape_html</code> allocates lesser Ruby strings than <code>gsub</code>. So, it is a win-win solution for me.</p>
<p>Thank you once again.</p> Ruby master - Bug #15895: String#gsub and String#sub should return original string if no substitution(s) have been made https://bugs.ruby-lang.org/issues/15895?journal_id=783152019-06-03T16:52:29Zashmaroli (Ashwin Maroli)
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Closed</i></li></ul>