Issue #9934 has been updated by Aman Gupta.


This is the best I could come up with. Definitely not ideal since it allocates another ruby object.

~~~
diff --git a/file.c b/file.c
index 77facac..71e2d93 100644
--- a/file.c
+++ b/file.c
@@ -3391,6 +3391,9 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
     rb_str_set_len(result, p - buf);
     rb_enc_check(fname, result);
     ENC_CODERANGE_CLEAR(result);
+    if (OBJ_TAINTED(result)) tainted = 1;
+    result = rb_enc_str_new(RSTRING_PTR(result), p - buf, rb_enc_get(result));
+    if (tainted) OBJ_TAINT(result);
     return result;
 }
 #endif /* _WIN32 */
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index 2c945ea..ed43ec0 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -458,6 +458,12 @@ class TestFileExhaustive < Test::Unit::TestCase
     end
   end
 
+  def test_expand_path_memsize
+    require "objspace"
+    path = File.expand_path("/foo")
+    assert_operator ObjectSpace.memsize_of(path), :<=, 5
+  end
+
   def test_expand_path_encoding
     drive = (DRIVE ? 'C:' : '')
     if Encoding.find("filesystem") == Encoding::CP1251
~~~

----------------------------------------
Bug #9934: High memory usage from file_expand_path_*
https://bugs.ruby-lang.org/issues/9934#change-47179

* Author: Aman Gupta
* Status: Open
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
* ruby -v: trunk
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
All the file expansion routines use `EXPAND_PATH_BUFFER()` which allocates PATH_MAX bytes on the heap per invocation.
The strings returned by `File.expand_path` are never realloc'd after they are populated, so they continue using 4kb (on linux) per string.
In our rails app, 22MB of heap usage is due to expanded path name strings.

~~~
$ ruby -robjspace -e' puts ObjectSpace.dump(File.expand_path("/foo")) '
{"address":"0x007fa2b44dd6c8", "type":"STRING", "class":"0x007fa2b3f99608", "bytesize":4, "capacity":4098, "value":"/foo", "encoding":"US-ASCII", "memsize":4099, "flags":{"wb_protected":true}}
~~~

The following failing patch demonstrates the issue as well:

~~~
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index 2c945ea..49be9de 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -458,6 +458,12 @@ class TestFileExhaustive < Test::Unit::TestCase
     end
   end

+  def test_expand_path_memsize
+    require "objspace"
+    path = File.expand_path("/foo")
+    assert_equal 5, ObjectSpace.memsize_of(path)
+  end
+
   def test_expand_path_encoding
     drive = (DRIVE ? 'C:' : '')
     if Encoding.find("filesystem") == Encoding::CP1251
~~~



-- 
https://bugs.ruby-lang.org/