Skip to content
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

status: avoid allocations when returning an OK status #2929

Merged
merged 1 commit into from
Jul 24, 2019

Conversation

dzbarsky
Copy link
Contributor

It's not expected for FromError to allocate when you have no error

@menghanl menghanl requested a review from dfawley July 23, 2019 17:29
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

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

Would you mind also updating FromContextError as well? Thanks!

status/status.go Outdated Show resolved Hide resolved
@dzbarsky
Copy link
Contributor Author

@dfawley good point, looks like the implementations on nil Status do what we want. PTAL?

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

@dfawley dfawley added the Type: Performance Performance improvements (CPU, network, memory, etc) label Jul 24, 2019
@dfawley dfawley added this to the 1.23 Release milestone Jul 24, 2019
@dzbarsky
Copy link
Contributor Author

https://travis-ci.org/grpc/grpc-go/jobs/563180848 seems like an infra flake? not sure how to tell travis to retry it

@dfawley
Copy link
Member

dfawley commented Jul 24, 2019

https://travis-ci.org/grpc/grpc-go/jobs/563180848 seems like an infra flake? not sure how to tell travis to retry it

Yes, looks like it. I kicked travis.

@dfawley dfawley changed the title Fix another common spurious status allocation status: avoid allocations when returning an OK status Jul 24, 2019
@dfawley dfawley merged commit 5da5b1f into grpc:master Jul 24, 2019
@crosbymichael
Copy link

Isn't this a breaking change for the API? Clients that expect a non-nil OK status is currently getting nil now and can cause issues with server and clients on different versions

@dfawley
Copy link
Member

dfawley commented Aug 28, 2019

Why is code broken whether the returned status is nil vs an allocated OK status? They should be functionally equivalent. Is your code explicitly checking for nil? If so, why?

@crosbymichael
Copy link

I'm just the messenger here :)

While I agree that nil and ok are equivalent in this context, this is still a breaking change. I'm guessing the nil check was for panics because it's not too common to call methods on a nil struct the way it's handled in go-grpc for the status type.

@dfawley
Copy link
Member

dfawley commented Aug 28, 2019

I'm guessing the nil check was for panics because it's not too common to call methods on a nil struct the way it's handled in go-grpc for the status type.

Calls to methods on a nil *Status are entirely safe. I disagree this is an API change. Whether the return value is nil or not was never specified or guaranteed by the API, and all the same meaningful semantics of the API are preserved: a *Status is returned which represents the input error (Code() returns OK, etc).

Did the behavior change? Yes, but the same is true for bug fixes. Was this absolutely necessary? No, we could have done something similar differently, but I don't think it's reasonable or correct for applications to be checking for nil here, either (before or after this change!). Note that the previous implementation could literally never return nil, so such a check was dead code. I am sorry that the change broke someone, but I do believe it was a valid change.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants