Reduce some allocations

This commit is contained in:
cvium
2021-05-16 14:49:11 +02:00
parent 5acb4e9491
commit 1b49435a0e
17 changed files with 778 additions and 595 deletions

View File

@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using SQLitePCL.pretty;
@@ -96,21 +97,42 @@ namespace Emby.Server.Implementations.Data
DateTimeStyles.None).ToUniversalTime();
}
public static DateTime? TryReadDateTime(this IResultSetValue result)
public static bool TryReadDateTime(this IReadOnlyList<IResultSetValue> reader, int index, [NotNullWhen(true)] out DateTime? result)
{
var dateText = result.ToString();
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
var dateText = item.ToString();
if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out var dateTimeResult))
{
return dateTimeResult.ToUniversalTime();
result = dateTimeResult.ToUniversalTime();
return true;
}
return null;
return false;
}
public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index)
public static bool TryGetGuid(this IReadOnlyList<IResultSetValue> reader, int index, [NotNullWhen(true)] out Guid? result)
{
return result[index].SQLiteType == SQLiteType.Null;
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
result = item.ReadGuidFromBlob();
return true;
}
private static bool IsDbNull(this IResultSetValue result)
{
return result.SQLiteType == SQLiteType.Null;
}
public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
@@ -118,14 +140,48 @@ namespace Emby.Server.Implementations.Data
return result[index].ToString();
}
public static bool TryGetString(this IReadOnlyList<IResultSetValue> reader, int index, out string result)
{
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
result = item.ToString();
return true;
}
public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
{
return result[index].ToBool();
}
public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index)
public static bool TryGetBoolean(this IReadOnlyList<IResultSetValue> reader, int index, [NotNullWhen(true)] out bool? result)
{
return result[index].ToInt();
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
result = item.ToBool();
return true;
}
public static bool TryGetInt(this IReadOnlyList<IResultSetValue> reader, int index, out int? result)
{
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
result = item.ToInt();
return true;
}
public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
@@ -133,9 +189,43 @@ namespace Emby.Server.Implementations.Data
return result[index].ToInt64();
}
public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index)
public static bool TryGetLong(this IReadOnlyList<IResultSetValue> reader, int index, out long? result)
{
return result[index].ToFloat();
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
result = item.ToInt64();
return true;
}
public static bool TryGetFloat(this IReadOnlyList<IResultSetValue> reader, int index, out float? result)
{
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
result = item.ToFloat();
return true;
}
public static bool TryGetDouble(this IReadOnlyList<IResultSetValue> reader, int index, out double? result)
{
result = null;
var item = reader[index];
if (item.IsDbNull())
{
return false;
}
result = item.ToDouble();
return true;
}
public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)