You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/06/30 21:50:38 UTC
[lucenenet] 02/27: Lucene.Net.Util.AttributeSource: Optimize
creation of built-in attributes (#295, #261)
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit c2d23de891ff9154b63afa1913eb74dcc7939ebf
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun Jun 28 13:13:44 2020 +0700
Lucene.Net.Util.AttributeSource: Optimize creation of built-in attributes (#295, #261)
---
src/Lucene.Net/Util/AttributeImpl.cs | 1 -
src/Lucene.Net/Util/AttributeSource.cs | 35 +++++++++++++++++++++++++++++-----
2 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/src/Lucene.Net/Util/AttributeImpl.cs b/src/Lucene.Net/Util/AttributeImpl.cs
index 8df9683..4c52483 100644
--- a/src/Lucene.Net/Util/AttributeImpl.cs
+++ b/src/Lucene.Net/Util/AttributeImpl.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Linq;
using System.Reflection;
using System.Text;
diff --git a/src/Lucene.Net/Util/AttributeSource.cs b/src/Lucene.Net/Util/AttributeSource.cs
index 17435dd..6e27b51 100644
--- a/src/Lucene.Net/Util/AttributeSource.cs
+++ b/src/Lucene.Net/Util/AttributeSource.cs
@@ -1,3 +1,4 @@
+using Lucene.Net.Analysis.TokenAttributes;
using System;
using System.Collections;
using System.Collections.Generic;
@@ -5,6 +6,7 @@ using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
+using FlagsAttribute = Lucene.Net.Analysis.TokenAttributes.FlagsAttribute;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Util
@@ -68,7 +70,11 @@ namespace Lucene.Net.Util
{
try
{
- return (Attribute)Activator.CreateInstance(GetClassForInterface<S>());
+ Type attributeType = GetClassForInterface<S>();
+
+ // LUCENENET: Optimize for creating instances of the most common attributes
+ // directly rather than using Activator.CreateInstance()
+ return CreateInstance(attributeType) ?? (Attribute)Activator.CreateInstance(attributeType);
}
catch (Exception e)
{
@@ -76,6 +82,27 @@ namespace Lucene.Net.Util
}
}
+ // LUCENENET: optimize known creation of built-in types
+ private Attribute CreateInstance(Type attributeType)
+ {
+ if (ReferenceEquals(typeof(CharTermAttribute), attributeType))
+ return new CharTermAttribute();
+ if (ReferenceEquals(typeof(FlagsAttribute), attributeType))
+ return new FlagsAttribute();
+ if (ReferenceEquals(typeof(OffsetAttribute), attributeType))
+ return new OffsetAttribute();
+ if (ReferenceEquals(typeof(PayloadAttribute), attributeType))
+ return new PayloadAttribute();
+ if (ReferenceEquals(typeof(PositionIncrementAttribute), attributeType))
+ return new PositionIncrementAttribute();
+ if (ReferenceEquals(typeof(PositionLengthAttribute), attributeType))
+ return new PositionLengthAttribute();
+ if (ReferenceEquals(typeof(TypeAttribute), attributeType))
+ return new TypeAttribute();
+
+ return null;
+ }
+
internal static Type GetClassForInterface<T>() where T : IAttribute
{
var attClass = typeof(T);
@@ -91,10 +118,8 @@ namespace Lucene.Net.Util
lock (attClassImplMap)
#endif
{
- var @ref = attClassImplMap.GetValue(attClass, (key) =>
- {
- return CreateAttributeWeakReference(key, out clazz);
- });
+ var @ref = attClassImplMap.GetValue(attClass, createValueCallback: (key) =>
+ CreateAttributeWeakReference(key, out clazz));
if (!@ref.TryGetTarget(out clazz))
{