/// <summary> /// WeakIntern the given InternableString. /// </summary> public string InternableToString(ref InternableString candidate) { if (candidate.Length == 0) { return(string.Empty); } InternResult resultForStatistics = Intern(ref candidate, out string internedString); #if DEBUG string expectedString = candidate.ExpensiveConvertToString(); if (!String.Equals(internedString, expectedString)) { throw new InvalidOperationException(String.Format("Interned string {0} should have been {1}", internedString, expectedString)); } #endif if (_internCallCountsByString != null) { lock (_internCallCountsByString) { switch (resultForStatistics) { case InternResult.FoundInWeakStringCache: _regularInternHits++; break; case InternResult.AddedToWeakStringCache: _regularInternMisses++; break; } _internCallCountsByString.TryGetValue(internedString, out int priorCount); _internCallCountsByString[internedString] = priorCount + 1; if (!candidate.ReferenceEquals(internedString)) { // Reference changed so 'candidate' is now released and should save memory. _internEliminatedStrings++; _internEliminatedChars += candidate.Length; } } } return(internedString); }
/// <summary> /// Version of Intern that gathers statistics /// </summary> private string InternWithStatistics <T>(T candidate) where T : IInternable { lock (_missedHardcodedStrings) { InternResult internResult = TryIntern(candidate, out string result); switch (internResult) { case InternResult.MatchedHardcodedString: _hardcodedInternHits++; break; case InternResult.FoundInWeakStringCache: _regularInternHits++; break; case InternResult.AddedToWeakStringCache: _regularInternMisses++; break; case InternResult.RejectedFromInterning: _rejectedStrings++; break; } if (internResult != InternResult.MatchedHardcodedString && internResult != InternResult.RejectedFromInterning) { _missedHardcodedStrings.TryGetValue(result, out int priorCount); _missedHardcodedStrings[result] = priorCount + 1; } if (!candidate.ReferenceEquals(result)) { // Reference changed so 'candidate' is now released and should save memory. _internEliminatedStrings++; _internEliminatedChars += candidate.Length; } return(result); } }