[WIP] Save allocations (?) by using symbols for JSON keys #252
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The JSONs we parse have the same keys repeating in many places.
Currently JSON parsing returns string keys, which allocates a new string every time:
This returns symbols instead, so each key is allocated at most once.
TODO: is this an incompatible API change in any way?
I'm a bit surprised that all tests passed :-). Manageiq tests pass too against this. The reason is that RecursiveOpenStruct implements "indifferent access" —
x.foo, x[:foo], x["foo"]
all work, whether you constructed with symbol or string keys:https://github.com/aetherknight/recursive-open-struct/blob/v1.0.4/lib/recursive_open_struct.rb#L131-L135
TODO: verify that this produces "mortal" symbols that can be GC'd. That's only possible since MRI 2.2.
Nice theory, but needs evidence of win
I see total allocations slightly actually go up :-(Fluctuates randomly from run to run.Measuring on kubeclient test suite by appending this to
test/test_helper.rb
:I see on master 738077 total_allocated_objects:
and with this change 775103 total_allocated_objects:
I suspect RecursiveOpenStruct's
_get_key_from_table_
tryingname.to_s
beforename.to_sym
(see link above) means we still get a string allocated for each symbol. (:a.to_s
returns new string every time. Darn you Ruby and your mutable strings!) I'll try swapping these 2 ROS lines.I think this PR is orthogonal to other ROS problems listed in #251.
OTOH malloc_increase_bytes is down from 40800 to 23664 :-)Fluctuates randomly from run to run.I'm not sure what I should look at. I actually want peak memory usage on big JSON, don't think test suite does that.
@ilackarms can you help measuring this? (low priority, probably low payoff compared to #250.)
With your #250 involving a ruby JSON parser, I suppose we'll get lots of unavoidable string allocations during parsing, That's fine, but if you can make it free the strings and use symbols in the final data structure, could help with total RAM.