Closed NickLaMuro closed 6 years ago
@NickLaMuro Let's modify the method to either ps(pid, options = {})
or just ps(options = {})
. Then we can better control everything. I'll make it a major release.
With that change then users can just do ps(:smaps => false)
Of course, that change and your suggestion are not mutually exclusive.
@NickLaMuro With the ability to just disable smaps completely, do you still want to pursue this?
@djberg96 Will have to review everything that you did in the 1.2.0 changes, but this might be the case that we no longer need/want this, yes.
@NickLaMuro Since users can now explicitly disable smaps and other data collection, I think I'll go ahead and close this.
:warning: This change definitely is risky, and warrants caution before merging. :warning:
This was the best change that I could come up with for reducing the number of memory allocations on linux, without doing something like making
smaps
optional or lazy loaded. More details below.Description
This change takes advantage of a few things in ruby and how the smaps file is structured to reduce the object allocations for a single process* on linux. These are:
smaps
, and the existing regexp is accurate in describing that..to_i
on a string, it will ignore all whitespace, and then only process the next set of consecutive digits. So both" 4000 kb".to_i
and"4000.0123".to_i
would return 4000..start_with?
doesn't allocate any new strings in rubyGains of course are also applicable for
.ps(nil)
as wellThe current implementation of this will always have to generate intermediate strings for the regexp matches, which seems to be 2 per match (haven't counted though).
That said, while this method has less allocations, it doesn't seem to perform noticeably faster, and it technically taking advantage of a ruby quirk, which adds risk. That alone might be grounds for not wanting to merge this change, which I can understand.
Benchmarks
Object Allocations
Benchmarks taken by doing the following:
Results:
Note: By removing calling
Smaps.new
all together dropped the allocations down to1122
.IPS
Munged results from all four invocations of the
bench/bench_ips_ps
script. There is no change, but put the results here for full disclosure:Regarding the lack of a speed increase, I am willing to bet that the time to iterate through of the line items in the smaps file is more of a bottleneck than the difference between
line.starts_with?
and/some regexp/ === line
.