Skip to content

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

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

Merged
merged 4 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,6 @@ namespace NHibernate.Test.NHSpecificTest.NH2898
public partial class BinaryFormatterCache : CacheBase
{

public override Task<object> GetAsync(object key, CancellationToken cancellationToken)
{
try
{
var entry = _hashtable[key] as byte[];
if (entry == null)
return Task.FromResult<object>(null);

var fmt = new BinaryFormatter
{
#if !NETFX
SurrogateSelector = new SerializationHelper.SurrogateSelector()
#endif
};
using (var stream = new MemoryStream(entry))
{
return Task.FromResult<object>(fmt.Deserialize(stream));
}
}
catch (System.Exception ex)
{
return Task.FromException<object>(ex);
}
}

public override Task PutAsync(object key, object value, CancellationToken cancellationToken)
{
try
Expand Down
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
4 changes: 2 additions & 2 deletions src/NHibernate/Loader/BasicLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ 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)
{
return index.ToString() + StringHelper.Underscore;
return StringHelper.GenerateSuffix(index);
}
}
}
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)
};
}
}
}