-
Notifications
You must be signed in to change notification settings - Fork 732
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
Reduce copying of admission.Request #1291
Conversation
@willbeason thanks for the PR! Looks like it needs DCO |
There is a corresponding linter that identifies problems like these (hugeParam). The downside is that it's noisy so it isn't always appropriate (for the reasons I mentioned above). |
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
Thanks for the contribution! Is it possible to run before/after benchmarks in |
That's a good idea - I'll do that now and post back. |
It looks like the difference between before/after is negligible at the scope of individual requests. Copying an admission.Request takes about 15ns: (after change)
(before change)
Code I used for benchmarking:
|
The benchmarks run in milliseconds, so an 0.0001% change would be very difficult to measure directly:
|
Codecov Report
@@ Coverage Diff @@
## master #1291 +/- ##
==========================================
+ Coverage 48.56% 48.67% +0.10%
==========================================
Files 68 68
Lines 4890 4890
==========================================
+ Hits 2375 2380 +5
+ Misses 2164 2160 -4
+ Partials 351 350 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
WDYT about using pointers to the request object? I'm not against the change as-is, but using a pointer to the entire object might make it easier if we need to accommodate, say, more complex validation of the config resource. |
I think that's a pretty good argument to just use pointers. There's the chance of someone mutating the Request, but we shouldn't ever be trying to modify a Request in the first place. If added to this PR, it would reduce copying further. |
LMK when the pointer change has been made! |
47dcdcd
to
961fe14
Compare
The admission.Request type is 400 bytes, making it huge to pass by value to functions. Each time this is passed to another function, the runtime fully copies this object, many times due to the function depth. In general, we should avoid such unnecessary struct copying. This PR makes our methods use a reference to admission.Request where possible, greatly reducing copying. Signed-off-by: Will Beason <willbeason@google.com>
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
What this PR does / why we need it:
The admission.Request type is 400 bytes, making it huge to pass by value
to functions. Each time this is passed to another function, the runtime
fully copies this object, often many times due to the function depth. In
general, we should avoid such unnecessary struct copying.
This PR reduces this copying (for example) by limiting several functions
to only ask for the subfield of admission.Request they need - the 3 bytes
referencing the Raw object byte slice. Other changes in this PR are
analogous.
I've only done the optimization where it added no complexity to the
call sites. For example, it would be valid to pass a pointer to the
admission.Request, but this could potentially have side effects as
the function would be able to mutate the input. In other cases, the
admission.Request would need to be replaced by more than one
argument so I've not done so (I can if desired).