Skip to content

Commit 5be2094

Browse files
authored
Merge pull request #18 from servicetitan/string-comparison
Use StringComparison.Ordinal to speedup string comparison
2 parents 37fc6ee + c9f8c8c commit 5be2094

17 files changed

Lines changed: 129 additions & 123 deletions

File tree

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/DriverFactory.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
using System.Data.Common;
1010
using System.Data.SqlClient;
1111
using System.Linq;
12-
using System.Text.RegularExpressions;
1312
using Xtensive.Core;
1413
using Xtensive.Orm;
1514
using Xtensive.Sql.Info;
1615
using SqlServerConnection = System.Data.SqlClient.SqlConnection;
17-
using JetBrains.Annotations;
1816

1917
namespace Xtensive.Sql.Drivers.SqlServer
2018
{
@@ -56,7 +54,7 @@ private static bool IsAzure(SqlServerConnection connection)
5654
{
5755
using (var command = connection.CreateCommand()) {
5856
command.CommandText = "SELECT @@VERSION";
59-
return ((string) command.ExecuteScalar()).Contains("Azure");
57+
return ((string) command.ExecuteScalar()).IndexOf("Azure", StringComparison.Ordinal) >= 0;
6058
}
6159
}
6260

@@ -66,7 +64,7 @@ protected override string BuildConnectionString(UrlInfo url)
6664
SqlHelper.ValidateConnectionUrl(url);
6765

6866
var builder = new SqlConnectionStringBuilder();
69-
67+
7068
// host, port, database
7169
if (url.Port==0)
7270
builder.DataSource = url.Host;

Orm/Xtensive.Orm/Core/Extensions/StringExtensions.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public static class StringExtensions
2222
/// </summary>
2323
/// <param name="value">The original string value.</param>
2424
/// <param name="suffix">The suffix to cut.</param>
25-
/// <returns>String without <paramref name="suffix"/> if it was found;
25+
/// <returns>String without <paramref name="suffix"/> if it was found;
2626
/// otherwise, original <paramref name="value"/>.</returns>
2727
public static string TryCutSuffix(this string value, string suffix)
2828
{
29-
if (!value.EndsWith(suffix))
29+
if (!value.EndsWith(suffix, StringComparison.Ordinal))
3030
return value;
3131
return value.Substring(0, value.Length - suffix.Length);
3232
}
@@ -36,11 +36,11 @@ public static string TryCutSuffix(this string value, string suffix)
3636
/// </summary>
3737
/// <param name="value">The original string value.</param>
3838
/// <param name="prefix">The prefix to cut.</param>
39-
/// <returns>String without <paramref name="prefix"/> if it was found;
39+
/// <returns>String without <paramref name="prefix"/> if it was found;
4040
/// otherwise, original <paramref name="value"/>.</returns>
4141
public static string TryCutPrefix(this string value, string prefix)
4242
{
43-
if (!value.StartsWith(prefix))
43+
if (!value.StartsWith(prefix, StringComparison.Ordinal))
4444
return value;
4545
return value.Substring(prefix.Length);
4646
}
@@ -412,7 +412,7 @@ public static bool Like(this string value, string sqlLikePattern, char escapeCha
412412
if (match.Value=="_") {
413413
return ".";
414414
}
415-
if(match.Value.StartsWith(escapeCharacter.ToString())) {
415+
if(match.Value.StartsWith(escapeCharacter.ToString(), StringComparison.Ordinal)) {
416416
return match.Value[1].ToString();
417417
}
418418
return Regex.Escape(match.Value);
@@ -429,12 +429,20 @@ public static string StripRoundBrackets(this string value)
429429
{
430430
var start = 0;
431431
var end = value.Length - 1;
432-
var actualLenght = value.Length;
433-
for (start = 0; value[start++]=='(' && value[end--]==')'; actualLenght -= 2) { }
432+
var actualLength = value.Length;
433+
for (start = 0; value[start++]=='(' && value[end--]==')'; actualLength -= 2) { }
434434

435435
return start == 1
436436
? value
437-
: value.Substring(start - 1, actualLenght);
437+
: value.Substring(start - 1, actualLength);
438+
}
439+
440+
internal static bool Contains(this string str, string value, StringComparison comparison)
441+
{
442+
ArgumentValidator.EnsureArgumentNotNull(str, nameof(str));
443+
ArgumentValidator.EnsureArgumentNotNull(value, nameof(value));
444+
445+
return str.IndexOf(value, comparison) >= 0;
438446
}
439447

440448
}

Orm/Xtensive.Orm/Orm/Building/Builders/ModelBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private void BuildPrefetchActions()
156156
var action = actionContainer.BuildPrefetchAction();
157157
domain.PrefetchActionMap.Add(type, action);
158158
}
159-
}
159+
}
160160

161161
private void BuildTypes(IEnumerable<TypeDef> typeDefs)
162162
{
@@ -177,7 +177,7 @@ private void BuildTypes(IEnumerable<TypeDef> typeDefs)
177177
private void PreprocessAssociations()
178178
{
179179
foreach (var typeInfo in context.Model.Types.Where(t => t.IsEntity && !t.IsAuxiliary)) {
180-
180+
181181
// pair integrity escalation and consistency check
182182
typesWithProcessedInheritedAssociations.Add(typeInfo);
183183
var refFields = typeInfo.Fields.Where(f => f.IsEntity || f.IsEntitySet).ToList();
@@ -368,12 +368,12 @@ private void BuildAuxiliaryTypes(IEnumerable<AssociationInfo> associations)
368368
// Updating fields names only if types differ.
369369
if (masterType != slaveType) {
370370
try {
371-
if (!masterType.Name.Contains("."))
371+
if (!masterType.Name.Contains(".", StringComparison.Ordinal))
372372
masterFieldDef.MappingName = context.NameBuilder.ApplyNamingRules(masterType.Name);
373373
}
374374
catch(DomainBuilderException){}
375375
try {
376-
if (!slaveType.Name.Contains("."))
376+
if (!slaveType.Name.Contains(".", StringComparison.Ordinal))
377377
slaveFieldDef.MappingName = context.NameBuilder.ApplyNamingRules(slaveType.Name);
378378
}
379379
catch (DomainBuilderException){}

Orm/Xtensive.Orm/Orm/Building/Builders/StorageMappingBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012 Xtensive LLC.
1+
// Copyright (C) 2012 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Denis Krjuchkov
@@ -25,7 +25,7 @@ private struct MappingRequest : IEquatable<MappingRequest>
2525

2626
public bool Equals(MappingRequest other)
2727
{
28-
return Equals(Assembly, other.Assembly) && string.Equals(Namespace, other.Namespace);
28+
return Equals(Assembly, other.Assembly) && string.Equals(Namespace, other.Namespace, StringComparison.Ordinal);
2929
}
3030

3131
public override bool Equals(object obj)
@@ -131,7 +131,7 @@ private static bool RuleMatch(MappingRule rule, Type type)
131131
var assemblyMatch =
132132
rule.Assembly==null || rule.Assembly==type.Assembly;
133133
var namespaceMatch =
134-
string.IsNullOrEmpty(rule.Namespace) || type.FullName.StartsWith(rule.Namespace + ".");
134+
string.IsNullOrEmpty(rule.Namespace) || type.FullName.StartsWith(rule.Namespace + ".", StringComparison.Ordinal);
135135
return assemblyMatch && namespaceMatch;
136136
}
137137

Orm/Xtensive.Orm/Orm/Configuration/Internals/ConnectionInfoParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2013 Xtensive LLC.
1+
// Copyright (C) 2013 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Denis Krjuchkov
@@ -35,7 +35,7 @@ private static string ExpandConnectionString(System.Configuration.Configuration
3535
{
3636
const string prefix = "#";
3737

38-
if (!connectionString.StartsWith(prefix))
38+
if (!connectionString.StartsWith(prefix, StringComparison.Ordinal))
3939
return connectionString;
4040

4141
string connectionStringName = connectionString.Substring(prefix.Length);

Orm/Xtensive.Orm/Orm/Configuration/Internals/DomainTypeRegistrationHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
namespace Xtensive.Orm.Configuration
1212
{
1313
/// <summary>
14-
/// <see cref="ITypeRegistrationProcessor"/> for processing <see cref="SessionBound"/>
15-
/// and <see cref="IEntity"/> descendants registration in
14+
/// <see cref="ITypeRegistrationProcessor"/> for processing <see cref="SessionBound"/>
15+
/// and <see cref="IEntity"/> descendants registration in
1616
/// <see cref="DomainConfiguration.Types"/> registry.
1717
/// </summary>
18-
/// <remarks>This implementation provides topologically sorted list
18+
/// <remarks>This implementation provides topologically sorted list
1919
/// of <see cref="Type"/>s.</remarks>
2020
internal sealed class DomainTypeRegistrationHandler : TypeRegistrationProcessorBase
2121
{
@@ -32,7 +32,7 @@ protected override bool IsAcceptable(TypeRegistration registration, Type type)
3232
{
3333
// Disallow implicit (via assembly scan) registration of types in Orm.Providers namespace
3434
return base.IsAcceptable(registration, type)
35-
&& (registration.Type!=null || !type.FullName.StartsWith(providersNamespace));
35+
&& (registration.Type!=null || !type.FullName.StartsWith(providersNamespace, StringComparison.Ordinal));
3636
}
3737

3838
/// <inheritdoc/>
@@ -42,7 +42,7 @@ protected override void Process(TypeRegistry registry, TypeRegistration registra
4242
return;
4343
if (!DomainTypeRegistry.IsInterestingType(type))
4444
return;
45-
// The type is interesting;
45+
// The type is interesting;
4646
// If it is a persistent type, let's register all its bases
4747
if (DomainTypeRegistry.IsPersistentType(type))
4848
Process(registry, registration, type.BaseType);

Orm/Xtensive.Orm/Orm/EntitySetBase.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected void EnsureOwnerIsNotRemoved()
159159
if (Owner.IsRemoved)
160160
throw new InvalidOperationException(Strings.ExEntityIsRemoved);
161161
}
162-
162+
163163
#region System-level event-like members
164164

165165
private void SystemInitialize()
@@ -369,7 +369,7 @@ protected void NotifyCollectionChanged(NotifyCollectionChangedAction action, Ent
369369
var subscriptionInfo = GetSubscription(EntityEventBroker.CollectionChangedEventKey);
370370
if (subscriptionInfo.Second != null) {
371371
var handler = (NotifyCollectionChangedEventHandler) subscriptionInfo.Second;
372-
if (action==NotifyCollectionChangedAction.Reset)
372+
if (action==NotifyCollectionChangedAction.Reset)
373373
handler.Invoke(this, new NotifyCollectionChangedEventArgs(action));
374374
else if (!index.HasValue) {
375375
if (action==NotifyCollectionChangedAction.Remove) {
@@ -378,11 +378,11 @@ protected void NotifyCollectionChanged(NotifyCollectionChangedAction action, Ent
378378
foreach (var @delegate in invocationList) {
379379
var typedDelegate = (NotifyCollectionChangedEventHandler) @delegate;
380380
var subscriberAssemblyName = @delegate.Method.DeclaringType.Assembly.FullName;
381-
if (subscriberAssemblyName.StartsWith(presentationFrameworkAssemblyPrefix))
381+
if (subscriberAssemblyName.StartsWith(presentationFrameworkAssemblyPrefix, StringComparison.Ordinal))
382382
// WPF can't handle "Remove" event w/o item index
383383
typedDelegate.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
384384
#if DEBUG
385-
else if (subscriberAssemblyName.StartsWith(storageTestsAssemblyPrefix))
385+
else if (subscriberAssemblyName.StartsWith(storageTestsAssemblyPrefix, StringComparison.Ordinal))
386386
typedDelegate.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
387387
#endif
388388
else
@@ -543,15 +543,15 @@ internal bool Add(Entity item, SyncContext syncContext, RemovalContext removalCo
543543
Session.EntitySetChangeRegistry.Register(state);
544544
index = GetItemIndex(state, itemKey);
545545
};
546-
546+
547547
operations.NotifyOperationStarting();
548548
if (association.IsPaired)
549-
Session.PairSyncManager.ProcessRecursively(syncContext, removalContext,
549+
Session.PairSyncManager.ProcessRecursively(syncContext, removalContext,
550550
OperationType.Add, association, Owner, item, finalizer);
551551
else
552552
finalizer.Invoke();
553553

554-
// removalContext is unused here, since Add is never
554+
// removalContext is unused here, since Add is never
555555
// invoked in reference cleanup process directly
556556

557557
if (!skipOwnerVersionChange)
@@ -626,7 +626,7 @@ internal bool Remove(Entity item, SyncContext syncContext, RemovalContext remova
626626
removalContext.EnqueueFinalizer(() => {
627627
try {
628628
try {
629-
index = GetItemIndex(State, itemKey); // Necessary, since index can be already changed
629+
index = GetItemIndex(State, itemKey); // Necessary, since index can be already changed
630630
if (!skipOwnerVersionChange)
631631
Owner.UpdateVersionInfo(Owner, Field);
632632
SystemRemove(item, index);
@@ -662,7 +662,7 @@ internal bool Remove(Entity item, SyncContext syncContext, RemovalContext remova
662662
throw;
663663
}
664664
}
665-
665+
666666
/// <summary>
667667
/// Clears this collection.
668668
/// </summary>
@@ -814,8 +814,8 @@ private bool Contains(Key key, Entity item)
814814
if (foundInCache)
815815
return true;
816816
var ownerState = Owner.PersistenceState;
817-
var itemState = item == null
818-
? PersistenceState.Synchronized
817+
var itemState = item == null
818+
? PersistenceState.Synchronized
819819
: item.PersistenceState;
820820
if (PersistenceState.New.In(ownerState, itemState) || State.IsFullyLoaded)
821821
return false;
@@ -901,7 +901,7 @@ private static EntitySetTypeState BuildEntitySetTypeState(object key, EntitySetB
901901
ArrayUtils<Type>.EmptyArray);
902902
return new EntitySetTypeState(seek, seekTransform, itemCtor, entitySet.GetItemCountQueryDelegate(field));
903903
}
904-
904+
905905
private int? GetItemIndex(EntitySetState state, Key key)
906906
{
907907
if (!state.IsFullyLoaded)
@@ -912,7 +912,7 @@ private static EntitySetTypeState BuildEntitySetTypeState(object key, EntitySetB
912912
if (subscriptionInfo.Second==null)
913913
return null;
914914

915-
// Ok, it seems there is a reason
915+
// Ok, it seems there is a reason
916916
// to waste linear time on calculating this...
917917
int i = 0;
918918
foreach (var cachedKey in state) {
@@ -934,7 +934,7 @@ internal static void ExecuteOnValidate(EntitySetBase target)
934934
// Initialization
935935

936936
/// <summary>
937-
/// Performs initialization (see <see cref="Initialize()"/>) of the <see cref="EntitySetBase"/>
937+
/// Performs initialization (see <see cref="Initialize()"/>) of the <see cref="EntitySetBase"/>
938938
/// if type of <see langword="this" /> is the same as <paramref name="ctorType"/>.
939939
/// Automatically invoked in the epilogue of any constructor of this type and its ancestors.
940940
/// </summary>

Orm/Xtensive.Orm/Orm/Providers/NameBuilder.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace Xtensive.Orm.Providers
2727
{
2828
/// <summary>
29-
/// Name builder for <see cref="Orm.Model.DomainModel"/> nodes
29+
/// Name builder for <see cref="Orm.Model.DomainModel"/> nodes
3030
/// Provides names according to a set of naming rules contained in
3131
/// <see cref="NamingConvention"/>.
3232
/// </summary>
@@ -258,15 +258,15 @@ public string BuildColumnName(FieldInfo field, ColumnInfo baseColumn)
258258
}
259259

260260
/// <summary>
261-
/// Gets the name for <see cref="ColumnInfo"/> object concatenating
261+
/// Gets the name for <see cref="ColumnInfo"/> object concatenating
262262
/// <see cref="Node.Name"/> of its declaring type with the original column name.
263263
/// </summary>
264264
/// <param name="column">The <see cref="ColumnInfo"/> object.</param>
265265
/// <returns>Column name.</returns>
266266
public string BuildColumnName(ColumnInfo column)
267267
{
268268
ArgumentValidator.EnsureArgumentNotNull(column, "column");
269-
if (column.Name.StartsWith(column.Field.DeclaringType.Name + "."))
269+
if (column.Name.StartsWith(column.Field.DeclaringType.Name + ".", StringComparison.Ordinal))
270270
throw new InvalidOperationException();
271271
string result = string.Concat(column.Field.DeclaringType.Name, ".", column.Name);
272272
return ApplyNamingRules(result);
@@ -426,9 +426,9 @@ public string BuildPartialIndexName(IndexDef index, Type filterType, string filt
426426
/// <returns>Association name.</returns>
427427
public string BuildAssociationName(AssociationInfo target)
428428
{
429-
return ApplyNamingRules(string.Format(AssociationPattern,
430-
target.OwnerType.Name,
431-
target.OwnerField.Name,
429+
return ApplyNamingRules(string.Format(AssociationPattern,
430+
target.OwnerType.Name,
431+
target.OwnerField.Name,
432432
target.TargetType.Name));
433433
}
434434

@@ -441,9 +441,9 @@ public string BuildAssociationName(AssociationInfo target)
441441
/// <returns>Association name.</returns>
442442
public string BuildAssociationName(TypeInfo ownerType, FieldInfo ownerField, TypeInfo targetType)
443443
{
444-
return ApplyNamingRules(string.Format(AssociationPattern,
445-
ownerType.Name,
446-
ownerField.Name,
444+
return ApplyNamingRules(string.Format(AssociationPattern,
445+
ownerType.Name,
446+
ownerField.Name,
447447
targetType.Name));
448448
}
449449

@@ -455,9 +455,9 @@ public string BuildAssociationName(TypeInfo ownerType, FieldInfo ownerField, Typ
455455
/// <returns>Auxiliary type mapping name.</returns>
456456
public string BuildAuxiliaryTypeMappingName(AssociationInfo target)
457457
{
458-
return ApplyNamingRules(string.Format(AssociationPattern,
459-
target.OwnerType.MappingName ?? target.OwnerType.Name,
460-
target.OwnerField.MappingName ?? target.OwnerField.Name,
458+
return ApplyNamingRules(string.Format(AssociationPattern,
459+
target.OwnerType.MappingName ?? target.OwnerType.Name,
460+
target.OwnerField.MappingName ?? target.OwnerField.Name,
461461
target.TargetType.MappingName ?? target.TargetType.Name));
462462
}
463463

Orm/Xtensive.Orm/Orm/Providers/StorageDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private void FixExtractionResultSqlite(SqlExtractionResult result)
118118
result.Catalogs
119119
.SelectMany(c => c.Schemas)
120120
.SelectMany(s => s.Tables)
121-
.Where(t => t.Name.EndsWith("-Generator")
121+
.Where(t => t.Name.EndsWith("-Generator", StringComparison.Ordinal)
122122
&& t.TableColumns.Count==1
123123
&& t.TableColumns[0].SequenceDescriptor==null);
124124

Orm/Xtensive.Orm/Orm/Upgrade/Hints/RenameTypeHint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public bool Equals(RenameTypeHint other)
3737
return false;
3838
if (ReferenceEquals(this, other))
3939
return true;
40-
return base.Equals(other)
40+
return base.Equals(other)
4141
&& other.NewType==NewType
4242
&& other.OldType==OldType;
4343
}
@@ -78,7 +78,7 @@ public RenameTypeHint(string oldType, Type newType)
7878
ArgumentValidator.EnsureArgumentNotNull(newType, "newType");
7979
ArgumentValidator.EnsureArgumentNotNullOrEmpty(oldType, "oldType");
8080

81-
if (!oldType.Contains("."))
81+
if (!oldType.Contains(".", StringComparison.Ordinal))
8282
oldType = newType.Namespace + "." + oldType;
8383
OldType = oldType;
8484
NewType = newType;

0 commit comments

Comments
 (0)