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

AOT Jit Compile Workaround #1064

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "9.0.100",
"rollForward": "latestFeature"
}
{
"sdk": {
"version": "9.0.100",
"rollForward": "latestFeature"
}
}
34 changes: 32 additions & 2 deletions src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
#if NET8_0_OR_GREATER
Expand All @@ -41,6 +42,7 @@
#endif
using System.Text;
using System.Threading;
using ExecutionEngineException = System.ExecutionEngineException;

#if USE_CSHARP_SQLITE
using Sqlite3 = Community.CsharpSqlite.Sqlite3;
Expand Down Expand Up @@ -2632,6 +2634,14 @@ void OnTableChanged (TableMapping table, NotifyTableChangedAction action)
}

public event EventHandler<NotifyTableChangedEventArgs> TableChanged;

public static void RegisterFastColumnSetter (
Type type,
string name,
Action<object, Sqlite3Statement, int> setter)
{
FastColumnSetter.RegisterFastColumnSetter (type, name, setter);
}
}

public class NotifyTableChangedEventArgs : EventArgs
Expand Down Expand Up @@ -3592,7 +3602,17 @@ public IEnumerable<T> ExecuteDeferredQuery<T> (TableMapping map)
continue;

if (fastColumnSetters[i] != null) {
fastColumnSetters[i].Invoke (obj, stmt, i);
try {
fastColumnSetters[i].Invoke (obj, stmt, i);
}
#pragma warning disable CS0618 // Type or member is obsolete
catch (ExecutionEngineException) {
#pragma warning restore CS0618 // Type or member is obsolete
// Column setter has AOT Problem so don't use it.
fastColumnSetters[i] = null;
Trace.WriteLine($"FastColumnSetter AOT Jit Exception on Type {map.MappedType.FullName} Column {cols[i].Name}");
i--; // go one back and read it with default implementation
}
}
else {
var colType = SQLite3.ColumnType (stmt, i);
Expand Down Expand Up @@ -3922,6 +3942,14 @@ object ReadCol (Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clr

internal class FastColumnSetter
{
private static ConcurrentDictionary<(Type, string), Action<object, Sqlite3Statement, int>> customSetter =
new ConcurrentDictionary<(Type, string), Action<object, Sqlite3Statement, int>> ();

public static void RegisterFastColumnSetter(Type type, string name, Action<object, Sqlite3Statement, int> setter)
{
customSetter[(type, name)] = setter;
}

/// <summary>
/// Gets a <see cref="MethodInfo"/> for a generic <see cref="GetFastSetterMethodInfoUnsafe"/> method, suppressing AOT warnings.
/// </summary>
Expand Down Expand Up @@ -3954,7 +3982,9 @@ internal static MethodInfo GetFastSetterMethodInfoUnsafe (Type mappedType)
/// </returns>
internal static Action<object, Sqlite3Statement, int> GetFastSetter<T> (SQLiteConnection conn, TableMapping.Column column)
{
Action<object, Sqlite3Statement, int> fastSetter = null;
if (customSetter.TryGetValue ((typeof(T), column.Name), out var fastSetter)) {
return fastSetter;
}

Type clrType = column.PropertyInfo.PropertyType;

Expand Down
115 changes: 115 additions & 0 deletions tests/SQLite.Tests/FastColumnSetterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Linq;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#else
using NUnit.Framework;
#endif

namespace SQLite.Tests
{
[TestFixture]
public class FastColumnSetterTest
{
public class TestSetter
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }

public string Data { get; set; }

public DateTime Date { get; set; }
}

public class TestDb : SQLiteConnection
{
public TestDb (String path)
: base (path)
{
CreateTable<TestSetter> ();
}
}

[Test]
public void SetFastColumnSetters_AndReadData()
{
FastColumnSetter.RegisterFastColumnSetter(
typeof(TestSetter),
nameof(TestSetter.Id),
(obj, stmt, index) => { ((TestSetter)obj).Id = SQLite3.ColumnInt(stmt, index); });

FastColumnSetter.RegisterFastColumnSetter (
typeof (TestSetter),
nameof (TestSetter.Data),
(obj, stmt, index) => { ((TestSetter)obj).Data = SQLite3.ColumnString (stmt, index); });

FastColumnSetter.RegisterFastColumnSetter (
typeof (TestSetter),
nameof (TestSetter.Date),
(obj, stmt, index) => { ((TestSetter)obj).Date = new DateTime (SQLite3.ColumnInt64 (stmt, index)); });

var n = 20;
var cq = from i in Enumerable.Range (1, n)
select new TestSetter {
Data = Convert.ToString (i),
Date = new DateTime (2013, 1, i)
};

var db = new TestDb (TestPath.GetTempFileName ());
db.InsertAll (cq);

var results = db.Table<TestSetter> ().Where (o => o.Data.Equals ("10"));
Assert.AreEqual (results.Count (), 1);
Assert.AreEqual (results.FirstOrDefault ().Data, "10");
}

[Test]
public void SetFastColumnSetters_AndReadData_IsCalled()
{
int callCount = 0;

FastColumnSetter.RegisterFastColumnSetter (
typeof (TestSetter),
nameof (TestSetter.Id),
(obj, stmt, index) => {
((TestSetter)obj).Id = SQLite3.ColumnInt (stmt, index);
callCount++;
});

FastColumnSetter.RegisterFastColumnSetter (
typeof (TestSetter),
nameof (TestSetter.Data),
(obj, stmt, index) => {
((TestSetter)obj).Data = SQLite3.ColumnString (stmt, index);
callCount++;
});

FastColumnSetter.RegisterFastColumnSetter (
typeof (TestSetter),
nameof (TestSetter.Date),
(obj, stmt, index) => {
((TestSetter)obj).Date = new DateTime (SQLite3.ColumnInt64 (stmt, index));
callCount++;
});

var n = 20;
var cq = from i in Enumerable.Range (1, n)
select new TestSetter {
Data = Convert.ToString (i),
Date = new DateTime (2013, 1, i)
};

var db = new TestDb (TestPath.GetTempFileName ());
db.InsertAll (cq);

var results = db.Table<TestSetter> ().Where (o => o.Data.Equals ("10"));
Assert.AreEqual (results.Count (), 1);
Assert.AreEqual (results.FirstOrDefault ().Data, "10");

Assert.IsTrue(callCount > 0);
}
}
}
2 changes: 1 addition & 1 deletion tests/SQLite.Tests/SQLite.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down