-
Notifications
You must be signed in to change notification settings - Fork 270
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
chore: avoid unnecessary json marshal #626
chore: avoid unnecessary json marshal #626
Conversation
Signed-off-by: Michael Crenshaw <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check my comment.
// Unmarshal again to strip type information (e.g. float64 vs. int) from the unstructured | ||
// object. This is important for diffing since it will cause godiff to report a false difference. | ||
var newUn unstructured.Unstructured | ||
err = json.Unmarshal(data, &newUn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to provide the same results if compared with the original code. Previously we were stripping the types from obj
and using that to marshal into data
. In the new code, data
will just hold the Marshal(obj)
result which could lead to a different behaviour. To validate this, it would be better to write unit tests that validate the type stripping logic. Checking on Pod resources values will probably be an easy way to validate this:
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data
is only used only for another Unmarshal
(or, more specifically, unmarshaling via json.NewDecoder
, but the behavior should be the same).
So before we had:
unstructured -> marshal -> unmarshal to unstructured -> marshal -> unmarshal to schema type
Now it will be:
unstructured -> marshal -> unmarshal to schema type
i.e. we're skipping unmarshal to unstructured
(which is just map[string]interface{}
) and the second marshal
.
My belief is that those to steps don't remove any type information that unstructured -> marshal
doesn't already remove.
But I'll write some tests to give us a bit more confidence that we're not losing anything important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM and covered by TestRemarshalResources
.
@agaudreault, @crenshaw-dev The |
8c91416
to
f416878
Compare
@leoluz PTAL The only way The "happy path" is already covered by I've added tests to ensure the happy path still works when the input type isn't the I've also added a test to ensure that the unhappy path behaves the same (i.e. the input value can't be unmarshaled into the schema type). I think this covers the relevant cases. |
Signed-off-by: Michael Crenshaw <[email protected]>
Signed-off-by: Michael Crenshaw <[email protected]>
Refactored the test to make SonarCloud happy. |
Quality Gate passedIssues Measures |
LGTM here are the tests I ran before and after.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Tks for the added tests. 🤞🏻
We have a utility function
stripTypeInformation
that json marshals and then unmarshals an unstructured in order to remove type information before diffing.The only caller of
stripTypeInformation
immediately JSON-marshals the new unstructured, i.e. we JSON-marshal the unstructured object twice. Instead of having a separate utility function, we can take advantage of theremarshal
function's marshaled copy of the unstructured to unmarshal and perform type-stripping.I ran a 120s memory profile on an Intuit app controller.
remarshal
was responsible for 2% of the memory use. A 120s CPU profile showedremarshal
as responsible for 3% of the CPU use. So I think the function is worth optimizing.Testing with a medium-size manifest (about 100 lines) showed the following difference in benchmark:
So 15% less memory and 23% less CPU time. The difference should be more dramatic with larger manifests.
I didn't commit the benchmark code, but here it is for reference: