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

Stash base dir paths in module constants #1416

Merged
merged 3 commits into from Apr 3, 2020

Conversation

ashmaroli
Copy link
Contributor

@ashmaroli ashmaroli commented Jan 29, 2020

__dir__ allocates a new string on each call. Using this method in string interpolation
("#{__dir__}/....") or path based methods (File.join(__dir__, ....), etc) results in duplication of the newly generated string.
This allocation can be reduced by stashing the value in frozen module / class constants.

@ashmaroli

This comment has been minimized.

@pyrmont pyrmont added the needs-review The PR needs to be reviewed label Feb 1, 2020
@pyrmont pyrmont self-assigned this Feb 1, 2020
@pyrmont
Copy link
Contributor

pyrmont commented Feb 5, 2020

Do you have an opinion @jneen? The idea makes sense to me but the overall reduction seems pretty small.

@pyrmont pyrmont added maintainer-action The PR has been reviewed but action by a maintainer is required and removed needs-review The PR needs to be reviewed labels Feb 5, 2020
@ashmaroli
Copy link
Contributor Author

ashmaroli commented Feb 6, 2020

@pyrmont I've extracted replacing global method load_relative with rouge_relative into a separate PR (#1430) so that it may have a greater chance to get merged even if this PR doesn't make it into master..

@ashmaroli
Copy link
Contributor Author

Memory profiling summary

--- master https://travis-ci.org/rouge-ruby/rouge/jobs/646657531
+++ PR     https://travis-ci.org/rouge-ruby/rouge/jobs/646846491

- Total allocated: 13.90 MB (140145 objects)
+ Total allocated: 13.86 MB (139773 objects)
- Total retained:  5.32 MB (37776 objects)
+ Total retained:  5.32 MB (37780 objects)

@pyrmont pyrmont removed the maintainer-action The PR has been reviewed but action by a maintainer is required label Apr 3, 2020
Copy link
Contributor

@pyrmont pyrmont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. While the performance improvement is minor, I think the real benefit comes from having the loading path be more clearly expressed.

@pyrmont pyrmont merged commit aa2b153 into rouge-ruby:master Apr 3, 2020
@pyrmont
Copy link
Contributor

pyrmont commented Apr 3, 2020

@ashmaroli I realise you split #1430 off from this one so that #1430 could more easily be merged. I'd still like to discuss that one but having looked at this, I think it's helpful in more clearly expressing intent. And, as you pointed out, it reduces memory usage! Thanks for submitting it :)

@ashmaroli ashmaroli deleted the dirname-constants branch April 5, 2020 07:11
mattt pushed a commit to NSHipster/rouge that referenced this pull request May 21, 2020
…-ruby#1416)

Rouge makes use of dynamic loading of files at various points 
throughout its codebase. In these instances, the method 
`Kernel#__dir__` is used. This commit instead uses module constants
to more clearly express intent and, as a bonus, reduce the number of
object allocations required.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants