sqlite-net icon indicating copy to clipboard operation
sqlite-net copied to clipboard

AOT Jit Compile Workaround

Open inforithmics opened this issue 4 years ago • 9 comments
trafficstars

This Pull request is a workaround for this Xamarin iOS AOT Bug.

https://github.com/xamarin/xamarin-macios/issues/12545

There are two Fixes

  1. This catches the exception and sets the Fast Mapper to null and outputs a Trace so that it can be seen in a released Version.
if (fastColumnSetters[i] != null) {
	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($"FastMapper AOT Jit Exception on Type {map.MappedType.FullName} Column {cols[i].Name}");
	}
}
  1. A Method where Fast Column Setters can be set to workround the iOS AOT Bug and still have the same-better Performance
SQLiteConnection.RegisterFastColumnSetter(
            typeof(CacheDto), 
            nameof(CacheDto.Date), 
            (obj, stmt, index) => { ((CacheDto)obj).Date = new DateTime(SQLite3.ColumnInt64(stmt, index)); });

Enhancements:

  • [x] Remove the Explicit Call to SQLiteInitializers.Init() With [ModuleInitializer]
  • [x] Handle Classes that are inherited from Sqlite Classes
  • [x] Skip Static Attributes
  • [x] Ignore Obsolete Warnings

inforithmics avatar Sep 06 '21 00:09 inforithmics

As a tangentially related remark, I'm using a similar exception catching mechanism in a different internal library, but I'm a bit worried by ExecutionEngineException being marked as Obsolete. What are the odds of a new runtime version suddenly deciding it'll throw a different kind of exception, instantly breaking everything that relies on this?

ElteHupkes avatar Nov 16 '21 17:11 ElteHupkes

The real workaround would be to use RegisterFastColumnSetter (I have written a SourceGenerator that Generates the code for it).

inforithmics avatar Oct 13 '22 14:10 inforithmics

@praeclarum is there any chance this will be merged at some point? Using the latest sqlite-net with release builds for Xamarin.iOS is basically broken at the moment (which is a pity seeing the library is mentioned on https://learn.microsoft.com/en-us/xamarin/ios/data-cloud/data/using-sqlite-orm and the readme.md mentioning Xamarin.iOS as the initial target platform :blush: )

This PR seems like a reasonable fallback for iOS for now. And iOS library users wanting to benefit from the possible performance gains can still provide their own setter via RegisterFastColumnSetter (which could be manually written or generated like @inforithmics suggests). And even without custom fast column setters on iOS, no performance gain is also still better than not being able to use the current version at all :wink:

pver avatar Jan 05 '23 14:01 pver

Is there some Interest in this Pull Request. If there is some Interest I would update this pull request with a SourceGenerator that Generates the Code for Using the FastColumn Setters

inforithmics avatar Jul 26 '23 14:07 inforithmics

I would be interested to see this updated and merged, yes

Dokug avatar Aug 10 '23 12:08 Dokug

Is there any chance we will see this Pull Request being updated? Even if it won't get merged, having this solution in here would allow everyone to copy the code and fixing this issue this way.

0ut0fRange avatar Dec 04 '23 11:12 0ut0fRange