Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[core] initialize OfflineDatabase asynchronously in DefaultFileSource #9864

Merged
merged 1 commit into from
Aug 28, 2017

Conversation

kkaefer
Copy link
Member

@kkaefer kkaefer commented Aug 25, 2017

Sometimes, initialization can take hundreds of milliseconds, in particular when the database doesn't exist yet, or when the app/device is doing a lot of I/O already. Instead of synchronously initializing the OfflineDatabase objects, we're now asynchronously initializing the object through a self-sent message that by virtue of being the first message for this actor guarantees that the object is there when it's needed by other member functions.

For completely fresh app start with no previous database, I'm seeing the synchronous initialization time go down from ~200ms to ~1ms. Of course, the work still needs to happen, but instead it'll only block the DefaultFileSource::Impl thread rather than the main thread that initializes the DefaultFileSource.

General longer term fix is tracked in #9666.

Sometimes, initialization can take hundreds of milliseconds, in particular when the database doesn't exist yet, or when the app/device is doing a lot of I/O already. Instead of synchronously initializing the OfflineDatabase objects, we're now asynchronously initializing the object through a self-sent message that by virtue of being the first message for this actor guarantees that the object is there when it's needed by other member functions.
@kkaefer kkaefer requested a review from jfirebaugh August 25, 2017 16:03
@kkaefer kkaefer added Core The cross-platform C++ core, aka mbgl performance Speed, stability, CPU usage, memory usage, or power usage labels Aug 25, 2017
Copy link
Contributor

@jfirebaugh jfirebaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

The main thing I considered in this review was how the change will affect the failure mode in cases like #7920. Prior to this change, if the OfflineDatabase constructor throws an exception, it goes uncaught in the DefaultFileSource thread, aborting the application. Because the initializeOfflineDatabase invocation introduced in this PR is a self-invocation, and invocations do not catch exceptions, that continues to be the case. The stack trace for DefaultFileSource thread at the time of the exception will be slightly different, but not in a material way, and the end result (application abort) should be the same.

There will be a difference in what the main thread is doing at the time of the abort: prior, it will always be waiting on a future. Afterward, it may be at some other arbitrary point in execution. I wouldn't expect this to matter though.

If in the future we change invocation to have built-in exception handling (a try/catch in Mailbox::receive, say), then we will need to consider the effects on code like this carefully. Likely, such a change would need to be accompanied by an actor supervisory system that allows the "owner" of the actor (DefaultFileSource in this case) to detect and recover (e.g. by re-initializing the actor, rather than attempting to send messages to an actor whose database connection is invalid).

@kkaefer kkaefer merged commit c6ab20e into master Aug 28, 2017
@kkaefer kkaefer deleted the defaultfilesource-async-init branch August 28, 2017 11:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Core The cross-platform C++ core, aka mbgl performance Speed, stability, CPU usage, memory usage, or power usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants