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

Separate formula emit from evaluate. #340

Merged
merged 2 commits into from
Feb 27, 2024

Conversation

Laimiux
Copy link
Collaborator

@Laimiux Laimiux commented Feb 26, 2024

What

Currently, formula evaluations will have to wait until output emission is finalized. If formula output emission triggers an expensive render, it will block further processing until emission is done and likely will force subsequent updates to the main thread. To avoid that, we are separating emission from formula run.

@@ -56,8 +59,15 @@ class FormulaRuntime<Input : Any, Output : Any>(
* this [FormulaRuntime] instance. We will not accept any more [onInput] changes and will
* not emit any new [Output] events.
*/
@Volatile
Copy link
Collaborator Author

@Laimiux Laimiux Feb 26, 2024

Choose a reason for hiding this comment

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

To ensure that we see the latest value within emitOutputIfNeeded

private val inspector = FormulaPlugins.inspector(type = formula.type(), local = inspector)
private val implementation = formula.implementation()

@Volatile
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To ensure that we see the latest value within emitOutputIfNeeded

@@ -44,6 +44,7 @@ internal class FormulaManagerImpl<Input, State, Output>(
* start new actions. And then, [performTerminationSideEffects] is called to clean
* up this [formula] and its child formulas.
*/
@Volatile
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To ensure that we see the latest value within emitOutputIfNeeded

@@ -13,7 +13,9 @@ import java.util.concurrent.atomic.AtomicReference
* that there is happens-before relationship between each thread and memory changes are visible
* between them.
*/
class SynchronizedUpdateQueue {
class SynchronizedUpdateQueue(
val onEmpty: () -> Unit = {}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We add optional onEmpty so we can emit output only once all updates are processed.

@carrotkite
Copy link

carrotkite commented Feb 26, 2024

JaCoCo Code Coverage 80.32% ✅

Class Covered Meta Status
com/instacart/formula/internal/SynchronizedUpdateQueue 87% 0%
com/instacart/formula/internal/FormulaManagerImpl 87% 0%
com/instacart/formula/internal/ListenerImpl 75% 0%
com/instacart/formula/FormulaRuntime 76% 0%

Generated by 🚫 Danger

@@ -193,9 +203,6 @@ class FormulaRuntime<Input : Any, Output : Any>(
if (isExecutingEffects) return
executeTransitionEffects()

if (!manager.isTerminated()) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will be emitted once the update queue is empty

@Laimiux Laimiux force-pushed the laimonas/formula-non-blocking-emissions branch 2 times, most recently from b5433ff to fb7d19d Compare February 26, 2024 22:20
@Laimiux Laimiux force-pushed the laimonas/formula-non-blocking-emissions branch from fb7d19d to d9f6708 Compare February 26, 2024 23:08
Copy link

@jasonostrander jasonostrander left a comment

Choose a reason for hiding this comment

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

LGTM

@Laimiux Laimiux merged commit 97a0dda into master Feb 27, 2024
3 checks passed
@Laimiux Laimiux deleted the laimonas/formula-non-blocking-emissions branch February 27, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants