Skip to content

Commit

Permalink
Deleted some unneeded caching and disposed of some values to hopefull…
Browse files Browse the repository at this point in the history
…y fix some memory leak issues.
  • Loading branch information
MeltyPlayer committed Oct 17, 2023
1 parent ec7b064 commit 46c1feb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Schema/src/binary/text/BinarySchemaReaderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public string Generate(IBinarySchemaContainer container) {
SymbolTypeUtil.GetDeclaringTypesDownward(typeSymbol);

var sb = new StringBuilder();
var cbsb = new CurlyBracketTextWriter(new StringWriter(sb));
using var cbsb = new CurlyBracketTextWriter(new StringWriter(sb));

{
var dependencies = new List<string> { "System", "schema.binary" };
Expand Down
2 changes: 1 addition & 1 deletion Schema/src/binary/text/BinarySchemaWriterGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public string Generate(IBinarySchemaContainer container) {
SymbolTypeUtil.GetDeclaringTypesDownward(typeSymbol);

var sb = new StringBuilder();
var cbsb = new CurlyBracketTextWriter(new StringWriter(sb));
using var cbsb = new CurlyBracketTextWriter(new StringWriter(sb));

{
var dependencies = new List<string> { "System", "schema.binary" };
Expand Down
47 changes: 5 additions & 42 deletions Schema/src/util/symbols/BetterSymbolImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,16 @@

namespace schema.util.symbols {
internal static partial class BetterSymbol {
private static ConcurrentDictionary<INamedTypeSymbol,
IBetterSymbol<INamedTypeSymbol>>
typeCache_ = new(SymbolEqualityComparer.Default);

private static ConcurrentDictionary<ISymbol, IBetterSymbol> memberCache_ =
new(SymbolEqualityComparer.Default);


public static void ClearCache() {
BetterSymbol.typeCache_.Clear();
BetterSymbol.memberCache_.Clear();
}

public static IBetterSymbol<INamedTypeSymbol> FromType(
INamedTypeSymbol symbol,
SyntaxNodeAnalysisContext? context = null) {
if (BetterSymbol.typeCache_.TryGetValue(symbol, out var betterSymbol)) {
if (context != null) {
betterSymbol.WithContext(context.Value);
}

return betterSymbol;
}

return BetterSymbol.typeCache_[symbol] =
new BetterSymbolImpl<INamedTypeSymbol>(symbol, context);
return new BetterSymbolImpl<INamedTypeSymbol>(symbol, context);
}

public static IBetterSymbol FromMember(
ISymbol symbol,
SyntaxNodeAnalysisContext? context = null) {
if (BetterSymbol.memberCache_.TryGetValue(symbol, out var betterSymbol)) {
if (context != null) {
betterSymbol.WithContext(context.Value);
}

return betterSymbol;
}

return BetterSymbol.memberCache_[symbol] =
new BetterSymbolImpl(symbol, context);
return new BetterSymbolImpl(symbol, context);
}


Expand All @@ -67,15 +36,9 @@ private BetterSymbolImpl(ISymbol symbol,
public string Name => this.Symbol.Name;

public IBetterSymbol GetChild(ISymbol child) {
if (BetterSymbol.memberCache_.TryGetValue(
child,
out var betterSymbol)) {
return betterSymbol;
}

return BetterSymbol.memberCache_[child] = new BetterSymbolImpl(child,
this.diagnosticReporter_
.GetSubReporter(child));
return new BetterSymbolImpl(child,
this.diagnosticReporter_
.GetSubReporter(child));
}
}

Expand Down
14 changes: 13 additions & 1 deletion Schema/src/util/text/CurlyBracketTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ public interface ICurlyBracketTextWriter {
public ICurlyBracketTextWriter ExitBlock();
}

public sealed class CurlyBracketTextWriter : ICurlyBracketTextWriter {
public sealed class CurlyBracketTextWriter : ICurlyBracketTextWriter,
IDisposable {
private readonly TextWriter impl_;
private int indentLevel_ = 0;

public CurlyBracketTextWriter(TextWriter impl) {
this.impl_ = impl;
}

~CurlyBracketTextWriter() => this.ReleaseUnmanagedResources_();

public void Dispose() {
this.ReleaseUnmanagedResources_();
GC.SuppressFinalize(this);
}

private void ReleaseUnmanagedResources_() {
this.impl_.Dispose();
}

public ICurlyBracketTextWriter EnterBlock(string prefix = "") {
if (prefix.Length > 0) {
prefix = $"{prefix} ";
Expand Down

0 comments on commit 46c1feb

Please sign in to comment.