EF Core SQLite slow star-up performance on Xamarin

Viewed 948

I faced with slow start-up experience of my Xamarin.Android application.

The first DbContext creation takes ~4.5 seconds (context has 24 tables). At first I thought that the reason for this is that EF Core needs time to scan the entity classes from DbContext and build the model (this makes sense for a large DbContext).

So, I created a test DbContext with only one table to check the relationship between the time to create a model on the number of tables in the model:

public class Log
{
    [Key]
    public int EntityId { get; set; }
    public string Timestamp { get; set; }
    public string Level { get; set; }
    public string Exception { get; set; }
    public string RenderedMessage { get; set; }
    public string Properties { get; set; }
}

public class ApplicationLogDbContext : DbContext
{
    public ApplicationLogDbContext(DbContextOptions<ApplicationLogDbContext> options)
        : base(options) { }

    public DbSet<Log> Logs { get; set; }
}

DbContext test code:

var options = new DbContextOptionsBuilder<ApplicationLogDbContext>()
    .UseSqlite("Data Source=/storage/emulated/0/Android/data/***/files/ApplicationLog.db")
    .Options;
var logContext = new ApplicationLogDbContext(options);
logContext.Database.EnsureCreated();

This code execution takes 3.3 seconds on my Samsung Galaxy S7. This test showed the inappropriateness of trying some solutions:

  • Use fewer entity classes (reduce connections between tables);
  • Split one application DbContext into several small context.

I also learn that:

  • EF Core doesn't have automatic migrations (I don't need to disable it);
  • I removed code first approach but it only partially helped to reduce the DbContext initialization time to 4 seconds;
  • If I delete the line with EnsureCreated, the execution time of the first request will increase by the same time;
  • There are many similar problems (here, GitHub), but they have no solution. Such issues have been reported even for EF6;
  • I didn't notice any significant changes after installing Microsoft.EntityFrameworkCore.Sqlite 5.0.0-preview.6.20312.4 (application has the same execution time: ~3.3 seconds);
  • PRAGMA journal_mode=WAL; increased initialization speed (EF Core 2.2). This is not stable, but sometimes the initialization time is reduced by 300-400 ms.
  • If application has 2 DbContext for different databases (1st with one entity, 2nd with 25 entities) EF Core initialization time for the second context is drastically reduced to ~1 second. First context initialization is still slow;
  • Initialization completes faster (~30%) without an attached debugging session.

Example with EF Core 5 Preview 6 GitHub

enter image description here enter image description here

What approach can I use to somehow hide this initialization time from the user, but get data for the main activity?

Is there any configuration tips that can help to increase default configuration initialization time?

Will performance improve when running .NET 5?

0 Answers
Related