Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Remove convTslice calls in Record() #1268

Conversation

howardjohn
Copy link
Contributor

@howardjohn howardjohn commented Sep 14, 2021

This is built upon #1267; that
one should likely merge first. I split this out as it has a small public
API change (to work around circular imports) to avoid issues on the
first PR.

Benchmark relative to #1267:

name                    old time/op    new time/op    delta
Record0-6                 1.77ns ± 1%    1.59ns ± 3%   -9.98%  (p=0.008 n=5+5)
Record1-6                  593ns ± 8%     544ns ± 8%   -8.40%  (p=0.095 n=5+5)
Record8-6                 1.20µs ± 3%    1.23µs ± 3%   +2.34%  (p=0.206 n=5+5)
Record8_WithRecorder-6     765ns ± 7%     783ns ± 6%     ~     (p=0.690 n=5+5)
Record8_Parallel-6        1.19µs ± 2%    1.19µs ± 6%     ~     (p=0.548 n=5+5)
Record8_8Tags-6           1.22µs ± 3%    1.22µs ± 3%     ~     (p=1.000 n=5+5)

name                    old alloc/op   new alloc/op   delta
Record0-6                  0.00B          0.00B          ~     (all equal)
Record1-6                   120B ± 0%       96B ± 0%  -20.00%  (p=0.008 n=5+5)
Record8-6                   344B ± 0%      320B ± 0%   -6.98%  (p=0.008 n=5+5)
Record8_WithRecorder-6      424B ± 0%      424B ± 0%     ~     (all equal)
Record8_Parallel-6          344B ± 0%      320B ± 0%   -6.98%  (p=0.008 n=5+5)
Record8_8Tags-6             344B ± 0%      320B ± 0%   -6.98%  (p=0.008 n=5+5)

name                    old allocs/op  new allocs/op  delta
Record0-6                   0.00           0.00          ~     (all equal)
Record1-6                   3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)
Record8-6                   3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)
Record8_WithRecorder-6      4.00 ± 0%      4.00 ± 0%     ~     (all equal)
Record8_Parallel-6          3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)
Record8_8Tags-6             3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)

For #1265

@howardjohn howardjohn requested review from rghetia and a team as code owners September 14, 2021 17:17
@google-cla google-cla bot added the cla: yes label Sep 14, 2021
@howardjohn howardjohn force-pushed the record/optimize-recorder-interface branch 4 times, most recently from 01624f5 to 1a526e6 Compare September 14, 2021 20:21
This is built upon
census-instrumentation#1267; that
one should likely merge first. I split this out as it has a small public
API change (to work around circular imports) to avoid issues on the
first PR.

Benchmark relative to census-instrumentation#1267:
```
me                    old time/op    new time/op    delta
Record0-6                 1.74ns ± 4%    1.79ns ± 2%   +2.85%  (p=0.238 n=5+5)
Record1-6                  634ns ± 6%     542ns ± 9%  -14.55%  (p=0.008 n=5+5)
Record8-6                 1.21µs ± 5%    1.23µs ± 2%   +1.97%  (p=0.254 n=5+5)
Record8_WithRecorder-6     777ns ± 5%     792ns ± 5%   +1.97%  (p=0.421 n=5+5)
Record8_Parallel-6        1.26µs ±24%    1.22µs ± 2%     ~     (p=0.690 n=5+5)
Record8_8Tags-6           1.23µs ± 2%    1.25µs ± 3%     ~     (p=0.651 n=5+5)

name                    old alloc/op   new alloc/op   delta
Record0-6                  0.00B          0.00B          ~     (all equal)
Record1-6                   120B ± 0%       96B ± 0%  -20.00%  (p=0.008 n=5+5)
Record8-6                   344B ± 0%      320B ± 0%   -6.98%  (p=0.008 n=5+5)
Record8_WithRecorder-6      424B ± 0%      424B ± 0%     ~     (all equal)
Record8_Parallel-6          344B ± 0%      320B ± 0%   -6.98%  (p=0.008 n=5+5)
Record8_8Tags-6             344B ± 0%      320B ± 0%   -6.98%  (p=0.008 n=5+5)

name                    old allocs/op  new allocs/op  delta
Record0-6                   0.00           0.00          ~     (all equal)
Record1-6                   3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)
Record8-6                   3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)
Record8_WithRecorder-6      4.00 ± 0%      4.00 ± 0%     ~     (all equal)
Record8_Parallel-6          3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)
Record8_8Tags-6             3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.008 n=5+5)
```
@howardjohn howardjohn force-pushed the record/optimize-recorder-interface branch from 1a526e6 to 90a0d3c Compare September 14, 2021 20:35
stats/record.go Outdated
@@ -94,7 +97,7 @@ func Record(ctx context.Context, ms ...Measurement) {
if len(ms) == 0 {
return
}
recorder := internal.DefaultRecorder
recorder := InternalMeasurementRecorder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me double-check my reasoning here? In theory, this could break someone who was overriding internal.DefaultRecorder, but that isn't possible, since it is in an internal package.

As long as this is backwards-compatible, I think we can accept the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your assessment; its backwards compatible because its internal and thus cannot be used. However, it does add a new public field that a user could start messing with when they probably shouldn't. I could not find a way to avoid this without introducing an import loop unfortunately.

edit: I may have an idea, let me try it out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored it to avoid leaking into the public API, and its still backwards compatible since they could not override internal.DefaultRecorder as mentioned above.

// avoids interface{} conversion.
// This will be a func(tags *tag.Map, measurement []Measurement, attachments map[string]interface{}) type,
// but is interface{} here to avoid import loops
var MeasurementRecorder interface{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone overrode this with a func(tags *tag.Map, measurement []Measurement, attachments map[string]interface{}), would recorder := internal.MeasurementRecorder.(measurementRecorder) accept it?

At first glance, this seems unnecessary, since this is already in an internal package

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure what you mean:

if you mean modified this L28 to use that instead of interface{} it would not compile (import loop)

If you mean set the value; we do (internal.MeasurementRecorder = recordMeasurement) but it can only be done in this package since its in internal, so a user couldn't do it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see the import loop now. This approach is probably the best we can do. Are the benchmarks in the description updated for the latest attempt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated it, but its the same (besides recording noise) as the initial iteration.

Copy link
Collaborator

@dashpole dashpole left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to hold off merging this for a day or two to make sure others have a chance to comment.

@howardjohn
Copy link
Contributor Author

Is this good to go?

@dashpole
Copy link
Collaborator

dashpole commented Oct 5, 2021

cc @punya
for a second opinion

Copy link
Contributor

@punya punya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the very late review. I thought of another approach for handling the import cycle (moving Measurement into the internal package and using a type alias), but it seems like a lot more code change for not enough value. I'm 👍 for this PR.

@dashpole dashpole merged commit bf52d9d into census-instrumentation:master Oct 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants