Skip to content

Commit

Permalink
dotnet: Microoptimizations (#18934)
Browse files Browse the repository at this point in the history
Comparing against empty string will first do a reference comparison, then value comparison.

Whitespace changes are caused by `trim_trailing_whitespace = true` in  `.editorconfig`

Closes #18934

COPYBARA_INTEGRATE_REVIEW=#18934 from Henr1k80:main 7467044
PiperOrigin-RevId: 700355679
  • Loading branch information
Henr1k80 authored and copybara-github committed Nov 26, 2024
1 parent e35ed79 commit 343a3f9
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Collections/MapField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ object IDictionary.this[object key]
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => Values;
#endregion

private class DictionaryEnumerator : IDictionaryEnumerator
private sealed class DictionaryEnumerator : IDictionaryEnumerator
{
private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;

Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Google.Protobuf/JsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public sealed class JsonFormatter {

static JsonFormatter() {
for (int i = 0; i < CommonRepresentations.Length; i++) {
if (CommonRepresentations[i] == "") {
if (CommonRepresentations[i].Length == 0) {
CommonRepresentations[i] = ((char)i).ToString();
}
}
Expand Down Expand Up @@ -291,7 +291,7 @@ private static bool IsDefaultValue(FieldDescriptor descriptor, object value) {
return descriptor.FieldType switch {
FieldType.Bool => (bool)value == false,
FieldType.Bytes => (ByteString)value == ByteString.Empty,
FieldType.String => (string)value == "",
FieldType.String => ((string)value).Length == 0,
FieldType.Double => (double)value == 0.0,
FieldType.SInt32 or FieldType.Int32 or FieldType.SFixed32 or FieldType.Enum =>
(int)value == 0,
Expand Down Expand Up @@ -497,7 +497,7 @@ private void WriteAny(TextWriter writer, IMessage value, int indentationLevel) {
WriteBracketClose(writer, ObjectCloseBracket, true, indentationLevel);
}

private void WriteDiagnosticOnlyAny(TextWriter writer, IMessage value) {
private static void WriteDiagnosticOnlyAny(TextWriter writer, IMessage value) {
string typeUrl =
(string)value.Descriptor.Fields[Any.TypeUrlFieldNumber].Accessor.GetValue(value);
ByteString data =
Expand Down
4 changes: 2 additions & 2 deletions csharp/src/Google.Protobuf/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ private static void MergeTimestamp(IMessage message, JsonToken token)
// TODO: It would be nice not to have to create all these objects... easy to optimize later though.
Timestamp timestamp = Timestamp.FromDateTime(parsed);
int nanosToAdd = 0;
if (subseconds != "")
if (subseconds.Length != 0)
{
// This should always work, as we've got 1-9 digits.
int parsedFraction = int.Parse(subseconds.Substring(1), CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -906,7 +906,7 @@ private static void MergeDuration(IMessage message, JsonToken token)
{
long seconds = long.Parse(secondsText, CultureInfo.InvariantCulture) * multiplier;
int nanos = 0;
if (subseconds != "")
if (subseconds.Length != 0)
{
// This should always work, as we've got 1-9 digits.
int parsedFraction = int.Parse(subseconds.Substring(1));
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/JsonTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private double ReadNumber(char initialCharacter)
var builder = StringBuilderCache.Acquire();
if (initialCharacter == '-')
{
builder.Append("-");
builder.Append('-');
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/DescriptorPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal IDescriptor LookupSymbol(string name, IDescriptor relativeTo)
{
// Chop off the last component of the scope.

int dotpos = scopeToTry.ToString().LastIndexOf(".");
int dotpos = scopeToTry.ToString().LastIndexOf('.');
if (dotpos == -1)
{
result = FindSymbol<IDescriptor>(name);
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
// a MapField, but that feels a tad nasty.
PropertyName = propertyName;
Extension = extension;
JsonName = Proto.JsonName == "" ? JsonFormatter.ToJsonName(Proto.Name) : Proto.JsonName;
JsonName = Proto.JsonName.Length == 0 ? JsonFormatter.ToJsonName(Proto.Name) : Proto.JsonName;
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static FileDescriptor()

private readonly Lazy<Dictionary<IDescriptor, DescriptorDeclaration>> declarations;

private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, IEnumerable<FileDescriptor> dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo)
private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, IList<FileDescriptor> dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo)
{
SerializedData = descriptorData;
DescriptorPool = pool;
Expand Down Expand Up @@ -128,7 +128,7 @@ private IDescriptor FindDescriptorForPath(IList<int> path)
return current;
}

private DescriptorBase GetDescriptorFromList(IReadOnlyList<DescriptorBase> list, int index)
private static DescriptorBase GetDescriptorFromList(IReadOnlyList<DescriptorBase> list, int index)
{
// This is fine: it may be a newer version of protobuf than we understand, with a new descriptor
// field.
Expand Down Expand Up @@ -419,7 +419,7 @@ public static FileDescriptor FromGeneratedCode(
{
ExtensionRegistry registry = new ExtensionRegistry();
registry.AddRange(GetAllExtensions(dependencies, generatedCodeInfo));

FileDescriptorProto proto;
try
{
Expand Down

0 comments on commit 343a3f9

Please sign in to comment.