Skip to content

Commit

Permalink
Hash cleanup in C# (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored May 7, 2024
1 parent 5ec7f36 commit 74efb4d
Show file tree
Hide file tree
Showing 23 changed files with 140 additions and 320 deletions.
22 changes: 14 additions & 8 deletions cpp/src/slice2cs/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2644,10 +2644,7 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p)
emitGeneratedCodeAttribute();
_out << nl << "public override int GetHashCode()";
_out << sb;
_out << nl << "int h_ = 5381;";
_out << nl << "global::Ice.Internal.HashUtil.hashAdd(ref h_, \"" << p->scoped() << "\");";
writeMemberHashCode(dataMembers);
_out << nl << "return h_;";
_out << eb;

_out << sp;
Expand Down Expand Up @@ -2902,15 +2899,24 @@ Slice::Gen::TypesVisitor::visitDataMember(const DataMemberPtr& p)
void
Slice::Gen::TypesVisitor::writeMemberHashCode(const DataMemberList& dataMembers)
{
for (DataMemberList::const_iterator q = dataMembers.begin(); q != dataMembers.end(); ++q)
_out << nl << "var hash = new global::System.HashCode();";
assert(dataMembers.size() > 0); // a Slice struct must have at least one field.

for (const auto& q : dataMembers)
{
_out << nl << "global::Ice.Internal.HashUtil.hashAdd(ref h_, " << fixId((*q)->name(), DotNet::ICloneable);
if ((*q)->optional())
TypePtr memberType = q->type();
string memberName = fixId(q->name(), DotNet::ICloneable);

if (dynamic_pointer_cast<Sequence>(memberType) || dynamic_pointer_cast<Dictionary>(memberType))
{
_out << ".Value";
_out << nl << "Ice.UtilInternal.Collections.HashCodeAdd(ref hash, this." << memberName << ");";
}
else
{
_out << nl << "hash.Add(this." << memberName << ");";
}
_out << ");";
}
_out << nl << "return hash.ToHashCode();";
}

void
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Ice/CommunicatorI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void flushBatchRequests(Ice.CompressBatch compressBatch)

public Task flushBatchRequestsAsync(Ice.CompressBatch compressBatch,
IProgress<bool> progress = null,
CancellationToken cancel = new CancellationToken())
CancellationToken cancel = default)
{
var completed = new FlushBatchTaskCompletionCallback(progress, cancel);
var outgoing = new CommunicatorFlushBatchAsync(_instance, completed);
Expand Down
10 changes: 1 addition & 9 deletions csharp/src/Ice/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,7 @@ public ACM(int timeout, ACMClose close, ACMHeartbeat heartbeat)
this.heartbeat = heartbeat;
}

public override int GetHashCode()
{
int h_ = 5381;
Ice.Internal.HashUtil.hashAdd(ref h_, "::Ice::ACM");
Ice.Internal.HashUtil.hashAdd(ref h_, timeout);
Ice.Internal.HashUtil.hashAdd(ref h_, close);
Ice.Internal.HashUtil.hashAdd(ref h_, heartbeat);
return h_;
}
public override int GetHashCode() => HashCode.Combine(timeout, close, heartbeat);

public override bool Equals(object other)
{
Expand Down
4 changes: 2 additions & 2 deletions csharp/src/Ice/ConnectionI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public void flushBatchRequests(CompressBatch compressBatch)

public Task flushBatchRequestsAsync(CompressBatch compressBatch,
IProgress<bool> progress = null,
CancellationToken cancel = new CancellationToken())
CancellationToken cancel = default)
{
var completed = new FlushBatchTaskCompletionCallback(progress, cancel);
var outgoing = new ConnectionFlushBatchAsync(this, _instance, completed);
Expand Down Expand Up @@ -602,7 +602,7 @@ public void invoke()
private readonly Ice.ConnectionI _connection;
}

public Task heartbeatAsync(IProgress<bool> progress = null, CancellationToken cancel = new CancellationToken())
public Task heartbeatAsync(IProgress<bool> progress = null, CancellationToken cancel = default)
{
var completed = new HeartbeatTaskCompletionCallback(progress, cancel);
var outgoing = new HeartbeatAsync(this, _instance, completed);
Expand Down
23 changes: 11 additions & 12 deletions csharp/src/Ice/Current.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,17 @@ public object Clone()

public override int GetHashCode()
{
int h_ = 5381;
Ice.Internal.HashUtil.hashAdd(ref h_, "::Ice::Current");
Ice.Internal.HashUtil.hashAdd(ref h_, adapter);
Ice.Internal.HashUtil.hashAdd(ref h_, con);
Ice.Internal.HashUtil.hashAdd(ref h_, id);
Ice.Internal.HashUtil.hashAdd(ref h_, facet);
Ice.Internal.HashUtil.hashAdd(ref h_, operation);
Ice.Internal.HashUtil.hashAdd(ref h_, mode);
Ice.Internal.HashUtil.hashAdd(ref h_, ctx);
Ice.Internal.HashUtil.hashAdd(ref h_, requestId);
Ice.Internal.HashUtil.hashAdd(ref h_, encoding);
return h_;
var hash = new HashCode();
hash.Add(adapter);
hash.Add(con);
hash.Add(id);
hash.Add(facet);
hash.Add(operation);
hash.Add(mode);
UtilInternal.Collections.HashCodeAdd(ref hash, ctx);
hash.Add(requestId);
hash.Add(encoding);
return hash.ToHashCode();
}

public override bool Equals(object other)
Expand Down
1 change: 0 additions & 1 deletion csharp/src/Ice/Internal/Ex.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) ZeroC, Inc.

using System.Diagnostics;
using System.Globalization;

namespace Ice.Internal;

Expand Down
33 changes: 8 additions & 25 deletions csharp/src/Ice/Internal/IPEndpointI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public IPEndpointI(ProtocolInstance instance, string host, int port, EndPoint so
port_ = port;
sourceAddr_ = sourceAddr;
connectionId_ = connectionId;
_hashInitialized = false;
}

public IPEndpointI(ProtocolInstance instance)
Expand All @@ -24,7 +23,6 @@ public IPEndpointI(ProtocolInstance instance)
port_ = 0;
sourceAddr_ = null;
connectionId_ = "";
_hashInitialized = false;
}

public IPEndpointI(ProtocolInstance instance, Ice.InputStream s)
Expand All @@ -34,7 +32,6 @@ public IPEndpointI(ProtocolInstance instance, Ice.InputStream s)
port_ = s.readInt();
sourceAddr_ = null;
connectionId_ = "";
_hashInitialized = false;
}

private sealed class InfoI : Ice.IPEndpointInfo
Expand Down Expand Up @@ -238,17 +235,16 @@ public override string options()

public override int GetHashCode()
{
lock (this)
var hash = new HashCode();
hash.Add(type());
hash.Add(host_);
hash.Add(port_);
if (sourceAddr_ is not null)
{
if (!_hashInitialized)
{
_hashValue = 5381;
HashUtil.hashAdd(ref _hashValue, type());
hashInit(ref _hashValue);
_hashInitialized = true;
}
return _hashValue;
hash.Add(sourceAddr_);
}
hash.Add(connectionId_);
return hash.ToHashCode();
}

public override int CompareTo(EndpointI obj)
Expand Down Expand Up @@ -295,17 +291,6 @@ public override void streamWriteImpl(Ice.OutputStream s)
s.writeInt(port_);
}

public virtual void hashInit(ref int h)
{
HashUtil.hashAdd(ref h, host_);
HashUtil.hashAdd(ref h, port_);
if (sourceAddr_ != null)
{
HashUtil.hashAdd(ref h, sourceAddr_);
}
HashUtil.hashAdd(ref h, connectionId_);
}

public virtual void fillEndpointInfo(Ice.IPEndpointInfo info)
{
info.host = host_;
Expand Down Expand Up @@ -417,6 +402,4 @@ protected override bool checkOption(string option, string argument, string endpo
protected int port_;
protected EndPoint sourceAddr_;
protected string connectionId_;
private bool _hashInitialized;
private int _hashValue;
}
8 changes: 1 addition & 7 deletions csharp/src/Ice/Internal/LocatorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,13 +699,7 @@ public override bool Equals(object o)
return true;
}

public override int GetHashCode()
{
int h = 5381;
HashUtil.hashAdd(ref h, _id);
HashUtil.hashAdd(ref h, _encoding);
return h;
}
public override int GetHashCode() => HashCode.Combine(_id, _encoding);

private Ice.Identity _id;
private Ice.EncodingVersion _encoding;
Expand Down
19 changes: 4 additions & 15 deletions csharp/src/Ice/Internal/OpaqueEndpointI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public OpaqueEndpointI(List<string> args)
{
throw new Ice.EndpointParseException("no -v option in endpoint " + ToString());
}

calcHashValue();
}

public OpaqueEndpointI(short type, Ice.InputStream s)
Expand All @@ -35,8 +33,6 @@ public OpaqueEndpointI(short type, Ice.InputStream s)
int sz = s.getEncapsulationSize();
_rawBytes = new byte[sz];
s.readBlob(_rawBytes);

calcHashValue();
}

//
Expand Down Expand Up @@ -245,7 +241,10 @@ public override bool equivalent(EndpointI endpoint)

public override int GetHashCode()
{
return _hashCode;
var hash = new HashCode();
hash.Add(_type);
hash.AddBytes(_rawBytes);
return hash.ToHashCode();
}

public override string options()
Expand Down Expand Up @@ -414,17 +413,7 @@ protected override bool checkOption(string option, string argument, string endpo
}
}

private void calcHashValue()
{
int h = 5381;
HashUtil.hashAdd(ref h, _type);
HashUtil.hashAdd(ref h, _rawEncoding);
HashUtil.hashAdd(ref h, _rawBytes);
_hashCode = h;
}

private short _type;
private Ice.EncodingVersion _rawEncoding;
private byte[] _rawBytes;
private int _hashCode;
}
2 changes: 1 addition & 1 deletion csharp/src/Ice/Internal/OutgoingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ public override void handleInvokeResponse(bool ok, OutgoingAsyncBase og)
public class FlushBatchTaskCompletionCallback : TaskCompletionCallback<object>
{
public FlushBatchTaskCompletionCallback(System.IProgress<bool> progress = null,
CancellationToken cancellationToken = new CancellationToken()) :
CancellationToken cancellationToken = default) :
base(progress, cancellationToken)
{
}
Expand Down
52 changes: 17 additions & 35 deletions csharp/src/Ice/Internal/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,30 +397,20 @@ public override string ToString()

public override int GetHashCode()
{
lock (this)
var hash = new HashCode();
hash.Add(_mode);
hash.Add(secure_);
hash.Add(_identity);
UtilInternal.Collections.HashCodeAdd(ref hash, _context);
hash.Add(_facet);
hash.Add(overrideCompress_);
if (overrideCompress_)
{
if (hashInitialized_)
{
return hashValue_;
}
int h = 5381;
HashUtil.hashAdd(ref h, _mode);
HashUtil.hashAdd(ref h, secure_);
HashUtil.hashAdd(ref h, _identity);
HashUtil.hashAdd(ref h, _context);
HashUtil.hashAdd(ref h, _facet);
HashUtil.hashAdd(ref h, overrideCompress_);
if (overrideCompress_)
{
HashUtil.hashAdd(ref h, compress_);
}
HashUtil.hashAdd(ref h, _protocol);
HashUtil.hashAdd(ref h, _encoding);
HashUtil.hashAdd(ref h, _invocationTimeout);
hashValue_ = h;
hashInitialized_ = true;
return hashValue_;
hash.Add(compress_);
}
// We don't hash protocol and encoding; they are usually "1.0" and "1.1" respectively.
hash.Add(_invocationTimeout);
return hash.ToHashCode();
}

public virtual bool Equals(Reference other)
Expand All @@ -444,8 +434,6 @@ public virtual bool Equals(Reference other)

public object Clone() => MemberwiseClone();

protected int hashValue_;
protected bool hashInitialized_;
private static Dictionary<string, string> _emptyContext = new Dictionary<string, string>();

private Instance _instance;
Expand Down Expand Up @@ -491,7 +479,6 @@ protected Reference(Instance instance,
_encoding = encoding;
_invocationTimeout = invocationTimeout;
secure_ = secure;
hashInitialized_ = false;
overrideCompress_ = false;
compress_ = false;
}
Expand Down Expand Up @@ -1118,16 +1105,11 @@ public override Dictionary<string, string> toProperty(string prefix)

public override int GetHashCode()
{
lock (this)
{
if (!hashInitialized_)
{
int h = base.GetHashCode(); // Initializes hashValue_.
HashUtil.hashAdd(ref h, _adapterId);
hashValue_ = h;
}
return hashValue_;
}
var hash = new HashCode();
hash.Add(base.GetHashCode());
hash.Add(_adapterId);
UtilInternal.Collections.HashCodeAdd(ref hash, _endpoints);
return hash.ToHashCode();
}

public override bool Equals(Reference other)
Expand Down
20 changes: 9 additions & 11 deletions csharp/src/Ice/Internal/TcpConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ internal TcpConnector(ProtocolInstance instance, EndPoint addr, NetworkProxy pro
_sourceAddr = sourceAddr;
_timeout = timeout;
_connectionId = connectionId;

_hashCode = 5381;
HashUtil.hashAdd(ref _hashCode, _addr);
if (_sourceAddr != null)
{
HashUtil.hashAdd(ref _hashCode, _sourceAddr);
}
HashUtil.hashAdd(ref _hashCode, _timeout);
HashUtil.hashAdd(ref _hashCode, _connectionId);
}

public override bool Equals(object obj)
Expand Down Expand Up @@ -77,7 +68,15 @@ public override string ToString()

public override int GetHashCode()
{
return _hashCode;
var hash = new HashCode();
hash.Add(_addr);
if (_sourceAddr is not null)
{
hash.Add(_sourceAddr);
}
hash.Add(_timeout);
hash.Add(_connectionId);
return hash.ToHashCode();
}

private ProtocolInstance _instance;
Expand All @@ -86,5 +85,4 @@ public override int GetHashCode()
private EndPoint _sourceAddr;
private int _timeout;
private string _connectionId;
private int _hashCode;
}
Loading

0 comments on commit 74efb4d

Please sign in to comment.