【优化】对于 DBool,DInt,DLong,DString,DFloat 使用对象池。略微优化了下性能。

main
walon 2021-08-11 17:26:36 +08:00
parent 1579d9bf3c
commit 9673664147
10 changed files with 98 additions and 31 deletions

View File

@ -101,9 +101,9 @@ namespace Luban.Job.Cfg.DataCreators
} }
if (CheckIsDefault(x.NamedMode, d)) if (CheckIsDefault(x.NamedMode, d))
{ {
return new DBool(false); return DBool.ValueOf(false);
} }
return new DBool(CreateBool(d)); return DBool.ValueOf(CreateBool(d));
} }
public DType Accept(TByte type, object converter, ExcelStream x, DefAssembly ass) public DType Accept(TByte type, object converter, ExcelStream x, DefAssembly ass)
@ -185,21 +185,21 @@ namespace Luban.Job.Cfg.DataCreators
} }
if (CheckIsDefault(x.NamedMode, d)) if (CheckIsDefault(x.NamedMode, d))
{ {
return DInt.Default; return DInt.ValueOf(0);
} }
var ds = d.ToString(); var ds = d.ToString();
if (converter is TEnum te) if (converter is TEnum te)
{ {
if (te.DefineEnum.TryValueByNameOrAlias(ds, out var c)) if (te.DefineEnum.TryValueByNameOrAlias(ds, out var c))
{ {
return new DInt(c); return DInt.ValueOf(c);
} }
} }
if (!int.TryParse(ds, out var v)) if (!int.TryParse(ds, out var v))
{ {
throw new InvalidExcelDataException($"{d} 不是 int 类型值"); throw new InvalidExcelDataException($"{d} 不是 int 类型值");
} }
return new DInt(v); return DInt.ValueOf(v);
} }
public DType Accept(TFint type, object converter, ExcelStream x, DefAssembly ass) public DType Accept(TFint type, object converter, ExcelStream x, DefAssembly ass)
@ -252,14 +252,14 @@ namespace Luban.Job.Cfg.DataCreators
{ {
if (te.DefineEnum.TryValueByNameOrAlias(ds, out var c)) if (te.DefineEnum.TryValueByNameOrAlias(ds, out var c))
{ {
return new DLong(c); return DLong.ValueOf(c);
} }
} }
if (!long.TryParse(ds, out var v)) if (!long.TryParse(ds, out var v))
{ {
throw new InvalidExcelDataException($"{d} 不是 long 类型值"); throw new InvalidExcelDataException($"{d} 不是 long 类型值");
} }
return new DLong(v); return DLong.ValueOf(v);
} }
public DType Accept(TFlong type, object converter, ExcelStream x, DefAssembly ass) public DType Accept(TFlong type, object converter, ExcelStream x, DefAssembly ass)
@ -305,13 +305,13 @@ namespace Luban.Job.Cfg.DataCreators
} }
if (CheckIsDefault(x.NamedMode, d)) if (CheckIsDefault(x.NamedMode, d))
{ {
return DFloat.Default; return DFloat.ValueOf(0);
} }
if (!float.TryParse(d.ToString(), out var v)) if (!float.TryParse(d.ToString(), out var v))
{ {
throw new InvalidExcelDataException($"{d} 不是 float 类型值"); throw new InvalidExcelDataException($"{d} 不是 float 类型值");
} }
return new DFloat(v); return DFloat.ValueOf(v);
} }
public DType Accept(TDouble type, object converter, ExcelStream x, DefAssembly ass) public DType Accept(TDouble type, object converter, ExcelStream x, DefAssembly ass)

View File

@ -16,7 +16,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TBool type, JsonElement x, DefAssembly ass) public DType Accept(TBool type, JsonElement x, DefAssembly ass)
{ {
return new DBool(x.GetBoolean()); return DBool.ValueOf(x.GetBoolean());
} }
public DType Accept(TByte type, JsonElement x, DefAssembly ass) public DType Accept(TByte type, JsonElement x, DefAssembly ass)
@ -36,7 +36,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TInt type, JsonElement x, DefAssembly ass) public DType Accept(TInt type, JsonElement x, DefAssembly ass)
{ {
return new DInt(x.GetInt32()); return DInt.ValueOf(x.GetInt32());
} }
public DType Accept(TFint type, JsonElement x, DefAssembly ass) public DType Accept(TFint type, JsonElement x, DefAssembly ass)
@ -46,7 +46,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TLong type, JsonElement x, DefAssembly ass) public DType Accept(TLong type, JsonElement x, DefAssembly ass)
{ {
return new DLong(x.GetInt64()); return DLong.ValueOf(x.GetInt64());
} }
public DType Accept(TFlong type, JsonElement x, DefAssembly ass) public DType Accept(TFlong type, JsonElement x, DefAssembly ass)
@ -56,7 +56,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TFloat type, JsonElement x, DefAssembly ass) public DType Accept(TFloat type, JsonElement x, DefAssembly ass)
{ {
return new DFloat(x.GetSingle()); return DFloat.ValueOf(x.GetSingle());
} }
public DType Accept(TDouble type, JsonElement x, DefAssembly ass) public DType Accept(TDouble type, JsonElement x, DefAssembly ass)

View File

@ -17,7 +17,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TBool type, object x, DefAssembly ass) public DType Accept(TBool type, object x, DefAssembly ass)
{ {
return new DBool((bool)x); return DBool.ValueOf((bool)x);
} }
public DType Accept(TByte type, object x, DefAssembly ass) public DType Accept(TByte type, object x, DefAssembly ass)
@ -37,7 +37,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TInt type, object x, DefAssembly ass) public DType Accept(TInt type, object x, DefAssembly ass)
{ {
return new DInt((int)x); return DInt.ValueOf((int)x);
} }
public DType Accept(TFint type, object x, DefAssembly ass) public DType Accept(TFint type, object x, DefAssembly ass)
@ -83,7 +83,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TLong type, object x, DefAssembly ass) public DType Accept(TLong type, object x, DefAssembly ass)
{ {
return new DLong(ToLong(x)); return DLong.ValueOf(ToLong(x));
} }
public DType Accept(TFlong type, object x, DefAssembly ass) public DType Accept(TFlong type, object x, DefAssembly ass)
@ -93,7 +93,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TFloat type, object x, DefAssembly ass) public DType Accept(TFloat type, object x, DefAssembly ass)
{ {
return new DFloat(ToFloat(x)); return DFloat.ValueOf(ToFloat(x));
} }
public DType Accept(TDouble type, object x, DefAssembly ass) public DType Accept(TDouble type, object x, DefAssembly ass)

View File

@ -14,7 +14,7 @@ namespace Luban.Job.Cfg.DataCreators
{ {
if (bool.TryParse(x, out var b)) if (bool.TryParse(x, out var b))
{ {
return new DBool(b); return DBool.ValueOf(b);
} }
else else
{ {
@ -62,7 +62,7 @@ namespace Luban.Job.Cfg.DataCreators
{ {
if (int.TryParse(x, out var b)) if (int.TryParse(x, out var b))
{ {
return new DInt(b); return DInt.ValueOf(b);
} }
else else
{ {
@ -86,7 +86,7 @@ namespace Luban.Job.Cfg.DataCreators
{ {
if (long.TryParse(x, out var b)) if (long.TryParse(x, out var b))
{ {
return new DLong(b); return DLong.ValueOf(b);
} }
else else
{ {
@ -110,7 +110,7 @@ namespace Luban.Job.Cfg.DataCreators
{ {
if (float.TryParse(x, out var b)) if (float.TryParse(x, out var b))
{ {
return new DFloat(b); return DFloat.ValueOf(b);
} }
else else
{ {

View File

@ -17,7 +17,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TBool type, XElement x, DefAssembly ass) public DType Accept(TBool type, XElement x, DefAssembly ass)
{ {
return new DBool(bool.Parse(x.Value.Trim().ToLower())); return DBool.ValueOf(bool.Parse(x.Value.Trim().ToLower()));
} }
public DType Accept(TByte type, XElement x, DefAssembly ass) public DType Accept(TByte type, XElement x, DefAssembly ass)
@ -37,7 +37,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TInt type, XElement x, DefAssembly ass) public DType Accept(TInt type, XElement x, DefAssembly ass)
{ {
return new DInt(int.Parse(x.Value.Trim())); return DInt.ValueOf(int.Parse(x.Value.Trim()));
} }
public DType Accept(TFint type, XElement x, DefAssembly ass) public DType Accept(TFint type, XElement x, DefAssembly ass)
@ -47,7 +47,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TLong type, XElement x, DefAssembly ass) public DType Accept(TLong type, XElement x, DefAssembly ass)
{ {
return new DLong(long.Parse(x.Value.Trim())); return DLong.ValueOf(long.Parse(x.Value.Trim()));
} }
public DType Accept(TFlong type, XElement x, DefAssembly ass) public DType Accept(TFlong type, XElement x, DefAssembly ass)
@ -57,7 +57,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TFloat type, XElement x, DefAssembly ass) public DType Accept(TFloat type, XElement x, DefAssembly ass)
{ {
return new DFloat(float.Parse(x.Value.Trim())); return DFloat.ValueOf(float.Parse(x.Value.Trim()));
} }
public DType Accept(TDouble type, XElement x, DefAssembly ass) public DType Accept(TDouble type, XElement x, DefAssembly ass)

View File

@ -4,7 +4,16 @@ namespace Luban.Job.Cfg.Datas
{ {
public class DBool : DType<bool> public class DBool : DType<bool>
{ {
public DBool(bool x) : base(x)
private static readonly DBool s_false = new DBool(false);
private static readonly DBool s_true = new DBool(true);
public static DBool ValueOf(bool x)
{
return x ? s_true : s_false;
}
private DBool(bool x) : base(x)
{ {
} }

View File

@ -4,9 +4,18 @@ namespace Luban.Job.Cfg.Datas
{ {
public class DFloat : DType<float> public class DFloat : DType<float>
{ {
public static DFloat Default { get; } = new DFloat(0); private static DFloat Default { get; } = new DFloat(0);
public DFloat(float x) : base(x) public static DFloat ValueOf(float x)
{
if (x == 0)
{
return Default;
}
return new DFloat(x);
}
private DFloat(float x) : base(x)
{ {
} }

View File

@ -4,9 +4,27 @@ namespace Luban.Job.Cfg.Datas
{ {
public class DInt : DType<int> public class DInt : DType<int>
{ {
public static DInt Default { get; } = new DInt(0); private const int POOL_SIZE = 128;
private static readonly DInt[] s_pool = new DInt[POOL_SIZE];
public DInt(int x) : base(x) static DInt()
{
for (int i = 0; i < POOL_SIZE; i++)
{
s_pool[i] = new DInt(i);
}
}
public static DInt ValueOf(int x)
{
if (x >= 0 && x < POOL_SIZE)
{
return s_pool[x];
}
return new DInt(x);
}
private DInt(int x) : base(x)
{ {
} }

View File

@ -5,8 +5,28 @@ namespace Luban.Job.Cfg.Datas
public class DLong : DType<long> public class DLong : DType<long>
{ {
public static DLong Default { get; } = new DLong(0); public static DLong Default { get; } = new DLong(0);
private const int POOL_SIZE = 128;
private static readonly DLong[] s_pool = new DLong[POOL_SIZE];
public DLong(long x) : base(x) static DLong()
{
for (int i = 0; i < POOL_SIZE; i++)
{
s_pool[i] = new DLong(i);
}
}
public static DLong ValueOf(long x)
{
if (x >= 0 && x < POOL_SIZE)
{
return s_pool[x];
}
return new DLong(x);
}
private DLong(long x) : base(x)
{ {
} }

View File

@ -4,6 +4,17 @@ namespace Luban.Job.Cfg.Datas
{ {
public class DString : DType<string> public class DString : DType<string>
{ {
private static readonly DString s_empty = new DString("");
public static DString ValueOf(string s)
{
if (s.Length == 0)
{
return s_empty;
}
return new DString(s);
}
public DString(string x) : base(x) public DString(string x) : base(x)
{ {
} }