Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freeze absolute paths for reduce allocations on file operations #125

Merged
merged 1 commit into from
Jun 28, 2020
Merged

Freeze absolute paths for reduce allocations on file operations #125

merged 1 commit into from
Jun 28, 2020

Commits on Jun 28, 2020

  1. 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
    ```
    byroot committed Jun 28, 2020
    Configuration menu
    Copy the full SHA
    0a68f87 View commit details
    Browse the repository at this point in the history