Skip to content

Commit

Permalink
Fix Perf warnings (#1372)
Browse files Browse the repository at this point in the history
- use Array.Empty
- improve StringBuilder, Where
- use .Equals instead Compare
- use ConfigureAwait
  • Loading branch information
mregen committed Apr 21, 2021
1 parent 087415b commit e34eb9b
Show file tree
Hide file tree
Showing 67 changed files with 293 additions and 317 deletions.
2 changes: 1 addition & 1 deletion Applications/ReferenceServer/ReferenceNodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1992,7 +1992,7 @@ private DataItemState CreateMultiStateValueDiscreteItemVariable(NodeState parent

double number = Convert.ToDouble(value);

if (number >= variable.EnumStrings.Value.Length | number < 0)
if (number >= variable.EnumStrings.Value.Length || number < 0)
{
return StatusCodes.BadOutOfRange;
}
Expand Down
10 changes: 5 additions & 5 deletions Libraries/Opc.Ua.Client.ComplexTypes/ComplexTypeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ IList<INode> serverStructTypes
}
catch (DataTypeNotFoundException dtnfex)
{
var typeMatch = structTypesWorkList.Where(n => n.NodeId == dtnfex.nodeId).FirstOrDefault();
var typeMatch = structTypesWorkList.FirstOrDefault(n => n.NodeId == dtnfex.nodeId);
if (typeMatch == null)
{
throw;
Expand Down Expand Up @@ -795,9 +795,9 @@ private ExpandedNodeId NormalizeExpandedNodeId(ExpandedNodeId expandedNodeId)
false,
false
);
binaryEncodingId = references.Where(r => r.BrowseName.Name == BrowseNames.DefaultBinary).FirstOrDefault()?.NodeId;
binaryEncodingId = references.FirstOrDefault(r => r.BrowseName.Name == BrowseNames.DefaultBinary)?.NodeId;
binaryEncodingId = NormalizeExpandedNodeId(binaryEncodingId);
xmlEncodingId = references.Where(r => r.BrowseName.Name == BrowseNames.DefaultXml).FirstOrDefault()?.NodeId;
xmlEncodingId = references.FirstOrDefault(r => r.BrowseName.Name == BrowseNames.DefaultXml)?.NodeId;
xmlEncodingId = NormalizeExpandedNodeId(xmlEncodingId);
return references.Where(r => supportedEncodings.Contains(r.BrowseName.Name))
.Select(r => ExpandedNodeId.ToNodeId(r.NodeId, m_session.NamespaceUris)).ToList();
Expand Down Expand Up @@ -972,10 +972,10 @@ IList<INode> enumerationTypes
}
else
{
enumDescription = allEnumerationTypes.Where(node =>
enumDescription = allEnumerationTypes.FirstOrDefault(node =>
node.BrowseName.Name == item.Name &&
(node.BrowseName.NamespaceIndex == complexTypeBuilder.TargetNamespaceIndex ||
complexTypeBuilder.TargetNamespaceIndex == -1)).FirstOrDefault()
complexTypeBuilder.TargetNamespaceIndex == -1))
as DataTypeNode;
}
if (enumDescription != null)
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Opc.Ua.Client/DataDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public async Task Load(NodeId dictionaryId, string name)
Array.Resize(ref schema, zeroTerminator);
}

await Validate(schema);
await Validate(schema).ConfigureAwait(false);

ReadDataTypes(dictionaryId);

Expand Down Expand Up @@ -304,7 +304,7 @@ private async Task Validate(byte[] dictionary)

try
{
await validator.Validate(istrm);
await validator.Validate(istrm).ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
1 change: 0 additions & 1 deletion Libraries/Opc.Ua.Client/NodeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ public bool IsEncodingFor(NodeId expectedTypeId, object value)
/// Returns the data type for the specified encoding.
/// </summary>
/// <param name="encodingId">The encoding id.</param>
/// <returns></returns>
public NodeId FindDataTypeId(ExpandedNodeId encodingId)
{
ILocalNode encoding = Find(encodingId) as ILocalNode;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Client/ReverseConnectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public void ClearWaitingConnections()
throw new ServiceResultException(StatusCodes.BadTimeout, "Waiting for the reverse connection timed out.");
}

return await tcs.Task;
return await tcs.Task.ConfigureAwait(false);
}

/// <summary>
Expand Down
47 changes: 14 additions & 33 deletions Libraries/Opc.Ua.Client/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,8 @@ public int GoodPublishRequestCount
X509Certificate2Collection clientCertificateChain = null;
if (endpointDescription.SecurityPolicyUri != SecurityPolicies.None)
{
clientCertificate = await LoadCertificate(configuration);
clientCertificateChain = await LoadCertificateChain(configuration, clientCertificate);
clientCertificate = await LoadCertificate(configuration).ConfigureAwait(false);
clientCertificateChain = await LoadCertificateChain(configuration, clientCertificate).ConfigureAwait(false);
}

// initialize the channel which will be created with the server.
Expand Down Expand Up @@ -907,7 +907,7 @@ public int GoodPublishRequestCount
if (reverseConnectManager == null)
{
return await Create(configuration, endpoint, updateBeforeConnect,
checkDomain, sessionName, sessionTimeout, userIdentity, preferredLocales);
checkDomain, sessionName, sessionTimeout, userIdentity, preferredLocales).ConfigureAwait(false);
}

ITransportWaitingConnection connection = null;
Expand All @@ -916,14 +916,14 @@ public int GoodPublishRequestCount
connection = await reverseConnectManager.WaitForConnection(
endpoint.EndpointUrl,
endpoint.ReverseConnect.ServerUri,
ct);
ct).ConfigureAwait(false);

if (updateBeforeConnect)
{
await endpoint.UpdateFromServerAsync(
endpoint.EndpointUrl, connection,
endpoint.Description.SecurityMode,
endpoint.Description.SecurityPolicyUri);
endpoint.Description.SecurityPolicyUri).ConfigureAwait(false);
updateBeforeConnect = false;
connection = null;
}
Expand All @@ -938,7 +938,7 @@ public int GoodPublishRequestCount
sessionName,
sessionTimeout,
userIdentity,
preferredLocales);
preferredLocales).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -1295,6 +1295,8 @@ public IEnumerable<Subscription> Load(string filePath)
{
XmlReaderSettings settings = new XmlReaderSettings();

settings.DtdProcessing = DtdProcessing.Prohibit;
settings.XmlResolver = null;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.CloseInput = true;

Expand Down Expand Up @@ -1405,7 +1407,6 @@ public void FetchTypeTree(ExpandedNodeId typeId)
/// Returns the available encodings for a node
/// </summary>
/// <param name="variableId">The variable node.</param>
/// <returns></returns>
public ReferenceDescriptionCollection ReadAvailableEncodings(NodeId variableId)
{
VariableNode variable = NodeCache.Find(variableId) as VariableNode;
Expand Down Expand Up @@ -1466,7 +1467,6 @@ public ReferenceDescriptionCollection ReadAvailableEncodings(NodeId variableId)
/// Returns the data description for the encoding.
/// </summary>
/// <param name="encodingId">The encoding Id.</param>
/// <returns></returns>
public ReferenceDescription FindDataDescription(NodeId encodingId)
{
Browser browser = new Browser(this);
Expand All @@ -1490,7 +1490,6 @@ public ReferenceDescription FindDataDescription(NodeId encodingId)
/// Returns the data dictionary that contains the description.
/// </summary>
/// <param name="descriptionId">The description id.</param>
/// <returns></returns>
public async Task<DataDictionary> FindDataDictionary(NodeId descriptionId)
{
// check if the dictionary has already been loaded.
Expand Down Expand Up @@ -1522,7 +1521,7 @@ public async Task<DataDictionary> FindDataDictionary(NodeId descriptionId)

DataDictionary dictionaryToLoad = new DataDictionary(this);

await dictionaryToLoad.Load(references[0]);
await dictionaryToLoad.Load(references[0]).ConfigureAwait(false);

m_dictionaries[dictionaryId] = dictionaryToLoad;

Expand All @@ -1548,7 +1547,7 @@ public async Task<DataDictionary> LoadDataDictionary(ReferenceDescription dictio

// load the dictionary.
DataDictionary dictionaryToLoad = new DataDictionary(this);
await dictionaryToLoad.Load(dictionaryId, dictionaryNode.ToString());
await dictionaryToLoad.Load(dictionaryId, dictionaryNode.ToString()).ConfigureAwait(false);
m_dictionaries[dictionaryId] = dictionaryToLoad;
return dictionaryToLoad;
}
Expand All @@ -1557,7 +1556,6 @@ public async Task<DataDictionary> LoadDataDictionary(ReferenceDescription dictio
/// Loads all dictionaries of the OPC binary or Xml schema type system.
/// </summary>
/// <param name="dataTypeSystem">The type system.</param>
/// <returns></returns>
public async Task<Dictionary<NodeId, DataDictionary>> LoadDataTypeSystem(NodeId dataTypeSystem = null)
{
if (dataTypeSystem == null)
Expand Down Expand Up @@ -1597,7 +1595,7 @@ public async Task<DataDictionary> LoadDataDictionary(ReferenceDescription dictio
try
{
dictionaryToLoad = new DataDictionary(this);
await dictionaryToLoad.Load(r);
await dictionaryToLoad.Load(r).ConfigureAwait(false);
m_dictionaries[dictionaryId] = dictionaryToLoad;
}
catch (Exception ex)
Expand All @@ -1614,7 +1612,6 @@ public async Task<DataDictionary> LoadDataDictionary(ReferenceDescription dictio
/// Reads the values for the node attributes and returns a node object.
/// </summary>
/// <param name="nodeId">The nodeId.</param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
public Node ReadNode(NodeId nodeId)
{
Expand Down Expand Up @@ -2132,7 +2129,6 @@ public Node ReadNode(NodeId nodeId)
/// Reads the value for a node.
/// </summary>
/// <param name="nodeId">The node Id.</param>
/// <returns></returns>
public DataValue ReadValue(NodeId nodeId)
{
ReadValueId itemToRead = new ReadValueId();
Expand Down Expand Up @@ -2172,7 +2168,6 @@ public DataValue ReadValue(NodeId nodeId)
/// </summary>
/// <param name="nodeId">The node id.</param>
/// <param name="expectedType">The expected type.</param>
/// <returns></returns>
public object ReadValue(NodeId nodeId, Type expectedType)
{
DataValue dataValue = ReadValue(nodeId);
Expand Down Expand Up @@ -2204,7 +2199,6 @@ public object ReadValue(NodeId nodeId, Type expectedType)
/// Fetches all references for the specified node.
/// </summary>
/// <param name="nodeId">The node id.</param>
/// <returns></returns>
public ReferenceDescriptionCollection FetchReferences(NodeId nodeId)
{
// browse for all references.
Expand Down Expand Up @@ -3266,7 +3260,6 @@ public virtual StatusCode Close(int timeout)
/// Adds a subscription to the session.
/// </summary>
/// <param name="subscription">The subscription to add.</param>
/// <returns></returns>
public bool AddSubscription(Subscription subscription)
{
if (subscription == null) throw new ArgumentNullException(nameof(subscription));
Expand Down Expand Up @@ -3294,7 +3287,6 @@ public bool AddSubscription(Subscription subscription)
/// Removes a subscription from the session.
/// </summary>
/// <param name="subscription">The subscription to remove.</param>
/// <returns></returns>
public bool RemoveSubscription(Subscription subscription)
{
if (subscription == null) throw new ArgumentNullException(nameof(subscription));
Expand Down Expand Up @@ -3326,7 +3318,6 @@ public bool RemoveSubscription(Subscription subscription)
/// Removes a list of subscriptions from the sessiont.
/// </summary>
/// <param name="subscriptions">The list of subscriptions to remove.</param>
/// <returns></returns>
public bool RemoveSubscriptions(IEnumerable<Subscription> subscriptions)
{
if (subscriptions == null) throw new ArgumentNullException(nameof(subscriptions));
Expand Down Expand Up @@ -3381,7 +3372,6 @@ public bool RemoveSubscriptions(IEnumerable<Subscription> subscriptions)
/// <param name="nodeClassMask">The node class mask.</param>
/// <param name="continuationPoint">The continuation point.</param>
/// <param name="references">The list of node references.</param>
/// <returns></returns>
public virtual ResponseHeader Browse(
RequestHeader requestHeader,
ViewDescription view,
Expand Down Expand Up @@ -3444,7 +3434,6 @@ public bool RemoveSubscriptions(IEnumerable<Subscription> subscriptions)
/// <param name="nodeClassMask">The node class mask.</param>
/// <param name="callback">The callback.</param>
/// <param name="asyncState"></param>
/// <returns></returns>
public IAsyncResult BeginBrowse(
RequestHeader requestHeader,
ViewDescription view,
Expand Down Expand Up @@ -3484,7 +3473,6 @@ public bool RemoveSubscriptions(IEnumerable<Subscription> subscriptions)
/// <param name="result">The result.</param>
/// <param name="continuationPoint">The continuation point.</param>
/// <param name="references">The list of node references.</param>
/// <returns></returns>
public ResponseHeader EndBrowse(
IAsyncResult result,
out byte[] continuationPoint,
Expand Down Expand Up @@ -4040,14 +4028,7 @@ public IAsyncResult BeginPublish(int timeout)
m_acknowledgementsToSend = new SubscriptionAcknowledgementCollection();
foreach (var toSend in acknowledgementsToSend)
{
if (m_latestAcknowledgementsSent.ContainsKey(toSend.SubscriptionId))
{
m_latestAcknowledgementsSent[toSend.SubscriptionId] = toSend.SequenceNumber;
}
else
{
m_latestAcknowledgementsSent.Add(toSend.SubscriptionId, toSend.SequenceNumber);
}
m_latestAcknowledgementsSent[toSend.SubscriptionId] = toSend.SequenceNumber;
}
}

Expand Down Expand Up @@ -4560,7 +4541,7 @@ private static async Task<X509Certificate2> LoadCertificate(ApplicationConfigura
throw ServiceResultException.Create(StatusCodes.BadConfigurationError, "ApplicationCertificate must be specified.");
}

clientCertificate = await configuration.SecurityConfiguration.ApplicationCertificate.Find(true);
clientCertificate = await configuration.SecurityConfiguration.ApplicationCertificate.Find(true).ConfigureAwait(false);

if (clientCertificate == null)
{
Expand All @@ -4580,7 +4561,7 @@ private static async Task<X509Certificate2Collection> LoadCertificateChain(Appli
{
clientCertificateChain = new X509Certificate2Collection(clientCertificate);
List<CertificateIdentifier> issuers = new List<CertificateIdentifier>();
await configuration.CertificateValidator.GetIssuers(clientCertificate, issuers);
await configuration.CertificateValidator.GetIssuers(clientCertificate, issuers).ConfigureAwait(false);

for (int i = 0; i < issuers.Count; i++)
{
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Opc.Ua.Client/SessionReconnectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private async void OnReconnect(object state)
}

// do the reconnect.
if (await DoReconnect())
if (await DoReconnect().ConfigureAwait(false))
{
lock (m_lock)
{
Expand Down Expand Up @@ -163,7 +163,7 @@ private async Task<bool> DoReconnect()
var connection = await m_reverseConnectManager.WaitForConnection(
new Uri(m_session.Endpoint.EndpointUrl),
m_session.Endpoint.Server.ApplicationUri
);
).ConfigureAwait(false);

m_session.Reconnect(connection);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ private async Task<bool> DoReconnect()
var connection = await m_reverseConnectManager.WaitForConnection(
new Uri(m_session.Endpoint.EndpointUrl),
m_session.Endpoint.Server.ApplicationUri
);
).ConfigureAwait(false);

session = Session.Recreate(m_session, connection);
}
Expand Down

0 comments on commit e34eb9b

Please sign in to comment.