Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Freeze absolute paths to reduce allocations on file operations
Most of the `File` methods cast the paths it receives with `FilePathValue` which ultimately delegate to `rb_new_str_frozen`. `rb_new_str_frozen` could be experessed as `str.frozen? ? str : str.dup`. So by freezing the paths passed to the various `File.` methods, we save at least one useless allocation each time, unless the string encoding is incompatible with the file system. ``` s = __FILE__ alloc = GC.stat[:total_allocated_objects] File.realpath(s) p GC.stat[:total_allocated_objects] - alloc # => 3 s = __FILE__.freeze alloc = GC.stat[:total_allocated_objects] File.realpath(s) p GC.stat[:total_allocated_objects] - alloc # => 2 ```
- Loading branch information