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

use cached empty BasicProperties when null #887

Merged
merged 1 commit into from
Jul 3, 2020

Conversation

bollhals
Copy link
Contributor

@bollhals bollhals commented Jul 2, 2020

Proposed Changes

Instead of creating a new BasicProperties when the passed one is null, a cached empty one is used. This avoids creating a new instance every time.

Types of Changes

  • Bug fix (non-breaking change which fixes issue #NNNN)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause an observable behavior change in existing systems)
  • Documentation improvements (corrections, new content, etc)
  • Cosmetic change (whitespace, formatting, etc)

Checklist

  • I have read the CONTRIBUTING.md document
  • I have signed the CA (see https://cla.pivotal.io/sign/rabbitmq)
  • All tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in related repositories

Further Comments

edited as I accidentally posted it too early

@michaelklishin michaelklishin merged commit 772dc4d into rabbitmq:master Jul 3, 2020
@michaelklishin
Copy link
Member

Makes perfect sense to me. Thank you!

@michaelklishin michaelklishin added this to the 6.2.0 milestone Jul 3, 2020
michaelklishin added a commit that referenced this pull request Jul 3, 2020
use cached empty BasicProperties when null

(cherry picked from commit 772dc4d)
@michaelklishin
Copy link
Member

Backported to 6.x.

@JeremyMahieu
Copy link
Contributor

JeremyMahieu commented Jul 7, 2020

Just because internal readonly IBasicProperties _emptyBasicProperties; is "readonly" doesn't mean you can't change the properties of the object that referenced by _emptyBasicProperties. It means you can't assign a new object to it outside of the constructor.

I think it's fine now but it could introduce some bugs later if the some code decide to make modifications to the properties they receive and thus your _emptyBasicProperties gets changed aswell. Solution would be to clone the object on passing but then what have we achieved with caching it.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly

Because reference types contain a reference to their data, a field that is a readonly reference type must always refer to the same object. That object isn't immutable. The readonly modifier prevents the field from being replaced by a different instance of the reference type. However, the modifier doesn't prevent the instance data of the field from being modified through the read-only field.

@stebet
Copy link
Contributor

stebet commented Jul 7, 2020

The cached object is only used when sending data and no properties have been set, so it should never be able to be modified, except through reflection.

@bollhals bollhals deleted the cache.BasicProperties branch March 2, 2021 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants