-
-
Notifications
You must be signed in to change notification settings - Fork 897
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
investigate #1970 - string optimization #1986
Comments
Sorry for the blunt comment in #1970 , @flavorjones IMO, this need not be reverted since it doesn't appear to be wrong or have a side-effect. (I don't have a JRuby environment, so can't really say about that aspect). But I did some additional tests by directly editing the installed gems ( Yes, the profiling is via the The profiling script is at: |
While not directly related to the issue under investigation here, I'm leaving a note regarding a couple of areas that could be refactored in future to reduce overall String allocations. The following native methods return a new Ruby String on every call:
In pure Ruby, the above would've been class Document
# Returns value of @encoding or nil
# @encoding is set by native method `set_encoding` exposed as `:encoding=`
attr_reader :encoding
end |
@ashmaroli I did a lot of benchmarks when tuning ruby-pg. One outcome was, that allocation counts can be a good indicator for performance improvements, but don't have to. In particular short string allocations like the above are way faster than lookups in a ruby Hash or st_tables (instance variables) - in fact by a factor of 3 to 5. String handling is very well optimized in ruby these days, so that reusing them is often more expensive than doing new allocations and corresponding GC'ing. Please conduct timer based benchmarks to verify effectiveness of such optimizations. |
@larskanis Thank you for joining this discussion. I understand that there is always a delicate balance between memory-usage and time-consumption and getting the right balance is tough.
Wow! That's interesting. Do you have a benchmark script that I can use to reproduce this observation? Factor of 3 to 5 is a significant difference. |
@ashmaroli After a bit of benchmarking here and here I noticed that I was wrong in my comment above. Reusing strings from Still I think that desitions based on allocation counts are valuable, but should be verified by timer based benchmarks. |
Background
PR #1970 was opened by @ashmaroli to attempt to reduce the number of allocated empty strings created by Nokogiri.
This PR was merged and released in v1.11.0.rc1.
@ashmaroli repeated his profiling (I think using the
memory_profiler
gem) and commented that it didn't seem to have an effect.Actions
Retrospecting
I should have asked for more details from @ashmaroli when he submitted the PR to reproduce his results, and demonstrate improvement, before merging. This would have allowed me to ask questions at a more appropriate time.
The text was updated successfully, but these errors were encountered: