Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some string.Intern's for minimizing duplicate strings count #3640

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions src/NHibernate/Criterion/EntityProjection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Loader;
using NHibernate.Loader.Criteria;
using NHibernate.Persister.Entity;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;

using IQueryable = NHibernate.Persister.Entity.IQueryable;

namespace NHibernate.Criterion
Expand Down Expand Up @@ -204,7 +205,7 @@ private void SetFields(ICriteriaQuery criteriaQuery)
subcriteria,
Persister.IdentifierPropertyName ?? string.Empty);

ColumnAliasSuffix = BasicLoader.GenerateSuffix(criteriaQuery.GetIndexForAlias());
ColumnAliasSuffix = StringHelper.GenerateSuffix(criteriaQuery.GetIndexForAlias());

_identifierColumnAliases = Persister.GetIdentifierAliases(ColumnAliasSuffix);

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ internal string GetSuffix(FromElement fromElement)
return suffix;
}

suffix = _suffixes.Count == 0 ? string.Empty : _suffixes.Count.ToString() + '_';
suffix = _suffixes.Count == 0 ? string.Empty : StringHelper.GenerateSuffix(_suffixes.Count);
_suffixes.Add(fromElement, suffix);

return suffix;
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Hql/Ast/ANTLR/Tree/FromElementType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private static string GetSuffix(int size, int sequence)

private static string GenerateSuffix(int size, int k)
{
String suffix = size == 1 ? "" : k.ToString() + '_';
String suffix = size == 1 ? "" : StringHelper.GenerateSuffix(k);
return suffix;
}

Expand Down
7 changes: 1 addition & 6 deletions src/NHibernate/Loader/BasicLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,10 @@ public static string[] GenerateSuffixes(int seed, int length)

for (int i = 0; i < length; i++)
{
suffixes[i] = GenerateSuffix(i + seed);
suffixes[i] = StringHelper.GenerateSuffix(i + seed);
}

return suffixes;
}

public static string GenerateSuffix(int index)
AlexanderKot marked this conversation as resolved.
Show resolved Hide resolved
{
return index.ToString() + StringHelper.Underscore;
}
}
}
6 changes: 3 additions & 3 deletions src/NHibernate/Mapping/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IEnumerable<ISelectable> ColumnIterator
public string Name
{
get { return name; }
set { name = value; }
set { name = value == null ? null : string.Intern(value); }
}

public bool IsComposite
Expand Down Expand Up @@ -122,7 +122,7 @@ public CascadeStyle CascadeStyle
public string Cascade
{
get { return cascade; }
set { cascade = value; }
set { cascade = value == null ? null : string.Intern(value); }
}

public bool IsUpdateable
Expand Down Expand Up @@ -168,7 +168,7 @@ public bool IsOptional
public string PropertyAccessorName
{
get { return propertyAccessorName; }
set { propertyAccessorName = value; }
set { propertyAccessorName = value == null ? null : string.Intern(value); }
}

public IGetter GetGetter(System.Type clazz)
Expand Down
10 changes: 6 additions & 4 deletions src/NHibernate/Mapping/SimpleValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public IDictionary<string, string> IdentifierGeneratorProperties
public string IdentifierGeneratorStrategy
{
get { return identifierGeneratorStrategy; }
set { identifierGeneratorStrategy = value; }
set { identifierGeneratorStrategy = value == null ? null : string.Intern(value); }
}

public virtual bool IsComposite
Expand Down Expand Up @@ -127,7 +127,7 @@ public string TypeName
if ((typeName == null && value != null) || (typeName != null && !typeName.Equals(value)))
{
// the property change
typeName = value;
typeName = value == null ? null : string.Intern(value);
type = null; // invalidate type
}
}
Expand Down Expand Up @@ -353,7 +353,8 @@ public virtual void SetTypeUsingReflection(string className, string propertyName
}
try
{
typeName = ReflectHelper.ReflectedPropertyClass(className, propertyName, accesorName).AssemblyQualifiedName;
var aqn = ReflectHelper.ReflectedPropertyClass(className, propertyName, accesorName).AssemblyQualifiedName;
typeName = aqn == null ? null : string.Intern(aqn);
}
catch (HibernateException he)
{
Expand All @@ -372,7 +373,8 @@ public virtual void SetTypeUsingReflection(System.Type propertyOwnerType, string
}
try
{
typeName = ReflectHelper.ReflectedPropertyClass(propertyOwnerType, propertyName, accessorName).AssemblyQualifiedName;
var aqn = ReflectHelper.ReflectedPropertyClass(propertyOwnerType, propertyName, accessorName).AssemblyQualifiedName;
typeName = aqn == null ? null : string.Intern(aqn);
}
catch (HibernateException he)
{
Expand Down
8 changes: 4 additions & 4 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2376,14 +2376,14 @@ protected void InitSubclassPropertyAliasesMap(PersistentClass model)
{
if (entityMetamodel.HasNonIdentifierPropertyNamedId)
{
subclassPropertyAliases[EntityPersister.EntityID + "." + idPropertyNames[i]] = new string[] { idAliases[i] };
subclassPropertyColumnNames[EntityPersister.EntityID + "." + IdentifierPropertyName + "." + idPropertyNames[i]] = new string[] { idColumnNames[i] };
subclassPropertyAliases[string.Intern(EntityPersister.EntityID + "." + idPropertyNames[i])] = new string[] { idAliases[i] };
subclassPropertyColumnNames[string.Intern(EntityPersister.EntityID + "." + IdentifierPropertyName + "." + idPropertyNames[i])] = new string[] { idColumnNames[i] };
}
// if (hasIdentifierProperty() && !ENTITY_ID.equals( getIdentifierPropertyName() ) ) {
if (HasIdentifierProperty)
{
subclassPropertyAliases[IdentifierPropertyName + "." + idPropertyNames[i]] = new string[] { idAliases[i] };
subclassPropertyColumnNames[IdentifierPropertyName + "." + idPropertyNames[i]] = new string[] { idColumnNames[i] };
subclassPropertyAliases[string.Intern(IdentifierPropertyName + "." + idPropertyNames[i])] = new string[] { idAliases[i] };
subclassPropertyColumnNames[string.Intern(IdentifierPropertyName + "." + idPropertyNames[i])] = new string[] { idColumnNames[i] };
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Persister/Entity/AbstractPropertyMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private static string ExtendPath(string path, string property)
if (string.IsNullOrEmpty(path))
return property;

return StringHelper.Qualify(path, property);
return string.Intern(StringHelper.Qualify(path, property));
}

public string[] GetColumnNames(string propertyName)
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Tuple/Entity/EntityMetamodel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private void MapIdentifierPropertyTypes(string path, IType propertyType)
for (var i = 0; i < componentType.PropertyNames.Length; i++)
{
MapIdentifierPropertyTypes(
!string.IsNullOrEmpty(path) ? $"{path}.{componentType.PropertyNames[i]}" : componentType.PropertyNames[i],
!string.IsNullOrEmpty(path) ? string.Intern($"{path}.{componentType.PropertyNames[i]}") : componentType.PropertyNames[i],
componentType.Subtypes[i]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Type/PersistentEnumType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public class EnumType<T> : PersistentEnumType
public EnumType() : base(typeof (T))
{
System.Type type = GetType();
typeName = type.FullName + ", " + type.Assembly.GetName().Name;
typeName = string.Intern(type.FullName + ", " + type.Assembly.GetName().Name);
}

public override string Name
Expand Down
10 changes: 10 additions & 0 deletions src/NHibernate/Util/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -871,5 +871,15 @@ public static bool IsAnyNewLine(this string str, int index, out int newLineLengt
newLineLength = 0;
return false;
}

public static string GenerateSuffix(int index)
{
return index switch
{
0 => "0_",
1 => "1_",
_ => string.Intern(index.ToString() + Underscore)
};
}
}
}
Loading