Avoid regenerating all formatters when only one object/union/enum changes by AlanLiu90 · Pull Request #1884 · MessagePack-CSharp/MessagePack-CSharp · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/MessagePack.SourceGenerator/CodeAnalysis/AnalyzerOptions.cs
44 changes: 43 additions & 1 deletion src/MessagePack.SourceGenerator/CodeAnalysis/FullModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,47 @@ public virtual bool Equals(FullModel? other)
&& Options.Equals(other.Options);
}

public override int GetHashCode() => throw new NotImplementedException();
public override int GetHashCode()
{
int hashCode = 0;

hashCode = Hash(hashCode, this.ObjectInfos.Count);
if (this.ObjectInfos.Count > 0)
{
hashCode = Hash(hashCode, this.ObjectInfos[0].GetHashCode());
}

hashCode = Hash(hashCode, this.EnumInfos.Count);
if (this.EnumInfos.Count > 0)
{
hashCode = Hash(hashCode, this.EnumInfos[0].GetHashCode());
}

hashCode = Hash(hashCode, this.GenericInfos.Count);
if (this.GenericInfos.Count > 0)
{
hashCode = Hash(hashCode, this.GenericInfos[0].GetHashCode());
}

hashCode = Hash(hashCode, this.UnionInfos.Count);
if (this.UnionInfos.Count > 0)
{
hashCode = Hash(hashCode, this.UnionInfos[0].GetHashCode());
}

hashCode = Hash(hashCode, this.CustomFormatterInfos.Count);
if (this.CustomFormatterInfos.Count > 0)
{
hashCode = Hash(hashCode, this.CustomFormatterInfos[0].GetHashCode());
}

hashCode = Hash(hashCode, this.Options.GetHashCode());

return hashCode;

static int Hash(int hashCode, int value)
{
return (hashCode * 31) + value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,18 @@ public virtual bool Equals(ObjectSerializationInfo? other)
&& NeedsCastOnBefore == other.NeedsCastOnBefore;
}

public override int GetHashCode() => throw new NotImplementedException();
public override int GetHashCode()
{
int hash = base.GetHashCode();

hash = Hash(hash, this.IsClass ? 1 : 2);
hash = Hash(hash, this.GenericTypeParameters.Length);

return hash;

static int Hash(int hashCode, int value)
{
return (hashCode * 31) + value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public static ResolverRegisterInfo Create(ITypeSymbol dataType, ResolverOptions
return generatedResolverName;
}

public override int GetHashCode() => this.DataType.GetHashCode();
Comment thread
AArnott marked this conversation as resolved.

public enum FormatterPosition
{
UnderResolver,
Expand Down
18 changes: 17 additions & 1 deletion src/MessagePack.SourceGenerator/MessagePackGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ from formatted in known.FormattableTypes
return FullModel.Combine(modelPerType.ToImmutableArray());
});

var splittedSources = source
.SelectMany(static (s, ct) =>
{
if (s is null)
{
return ImmutableArray<FullModel>.Empty;
}

var models = new List<FullModel>();
models.AddRange(s.ObjectInfos.Select(i => FullModel.Empty with { Options = s.Options, ObjectInfos = ImmutableSortedSet.Create(i) }));
models.AddRange(s.EnumInfos.Select(i => FullModel.Empty with { Options = s.Options, EnumInfos = ImmutableSortedSet.Create(i) }));
models.AddRange(s.UnionInfos.Select(i => FullModel.Empty with { Options = s.Options, UnionInfos = ImmutableSortedSet.Create(i) }));

return models.ToImmutableArray();
});

context.RegisterSourceOutput(source, static (context, source) =>
{
if (source is { Options.IsGeneratingSource: true })
Expand All @@ -138,7 +154,7 @@ from formatted in known.FormattableTypes
}
});

context.RegisterSourceOutput(source, static (context, source) =>
context.RegisterSourceOutput(splittedSources, static (context, source) =>
{
if (source is { Options.IsGeneratingSource: true })
{
Expand Down