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

IDictionary contracts and boxing #490

Open
AndirNotes opened this issue Jun 23, 2017 · 2 comments
Open

IDictionary contracts and boxing #490

AndirNotes opened this issue Jun 23, 2017 · 2 comments

Comments

@AndirNotes
Copy link

Accidentally during memory profiling of our app, I've discovered that contracts for IDictionary
https://github.com/Microsoft/CodeContracts/blob/master/Microsoft.Research/Contracts/MsCorlib/System.Collections.Generic.IDictionary.cs
Contains a lot of contracts like
Contract.Requires(!ReferenceEquals(key, null));
and this lead to boxing of a value type keys (e.g. keys like ints, Guids are quite common) and performance penalties. Looks like a bug.
I suspect that those contracts should be more clever than that and consider that generic type could be a ValueType and such check does not make sense at all.

In the meantime I need a workaround: Is it possible to ask somehow code contracts rewriter do not use contracts for implemented interfaces? Some way to ignore them for a specific type only?

@yaakov-h
Copy link
Contributor

IIRC there's an attribute you can apply to your specific types to tell the rewriter to ignore it. I forget the specifics, I think it's covered in the manual.

@AndirNotes
Copy link
Author

Oh, thank you. Found it. Quite unobvious attribute name, but it works.
[ContractOption("contract", "inheritance", false)]

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants