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

"Flatten" the tasks in the light client #349

Merged
merged 6 commits into from
Mar 27, 2023
Merged

Conversation

tomaka
Copy link
Contributor

@tomaka tomaka commented Mar 27, 2023

cc #345

The light client spawns various tasks that are run in the background.
It also creates two FuturesUnordered (one in the networking service and one in the JSON-RPC service), pushes tasks to it, and then spawns a background task that runs this FuturesUnordered.
In other words, there's a "hierarchy" of tasks: the tasks spawned in the background, and the tasks that are run by tasks spawned in the background.

The advantage of doing so and the reason why this was done so is that the "second level" of the "hierarchy" doesn't have the same priority as the "first layer". The first layer contains important tasks, and the second layer the less important ones.

Unfortunately, the FuturesUnordered doesn't work well when it comes to yielding. It's only after tasks have yielded twice that the FuturesUnordered yields. Because the wasm node implements background tasks with a FuturesUnordered as well, this means that all tasks have to yield a total of 4 times in order to "actually" yield to the browser/NodeJS/Deno.
At the moment, yielding is implemented by sleeping twice, for exactly this reason.

See rust-lang/futures-rs#2053 for a discussion about this.

In order to solve that problem, this PR removes the layers of hierarchy. This means that all tasks now have the same execution priority, which isn't great in the absolute but in practice isn't a problem. If we need to restore priority levels, I think a better solution is to add a parameter when spawning a background task indicating the priority.

@github-actions
Copy link

github-actions bot commented Mar 27, 2023

twiggy diff report

Difference in .wasm size before and after this pull request.


 Delta Bytes │ Item
─────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────
      +10359 ┊ smoldot_light::network_service::tasks::connection_task::{{closure}}::h89ed143d320ef28a
      -10359 ┊ smoldot_light::network_service::tasks::connection_task::{{closure}}::h9b6ac1a752be546a
       -3597 ┊ smoldot_light::sync_service::SyncService<TPlat>::storage_query::{{closure}}::ha7241b4cdca61fd9
       +3597 ┊ smoldot_light::sync_service::SyncService<TPlat>::storage_query::{{closure}}::ha9f1c70c7ab5574f
       -3362 ┊ smoldot_light::sync_service::SyncService<TPlat>::storage_query::{{closure}}::h4bb1b97f70ee39d5
       +3362 ┊ smoldot_light::sync_service::SyncService<TPlat>::storage_query::{{closure}}::hd89b1c34f36f3308
       +2573 ┊ smoldot_light::network_service::NetworkService<TPlat>::blocks_request::{{closure}}::hcbe957a65a360970
       -2573 ┊ smoldot_light::network_service::NetworkService<TPlat>::blocks_request::{{closure}}::hcdba85108b71db3e
       +2380 ┊ smoldot::json_rpc::requests_subscriptions::SubscriptionStart<TSubMsg>::start::{{closure}}::h4eeea666fa593a18
       -2380 ┊ smoldot::json_rpc::requests_subscriptions::SubscriptionStart<TSubMsg>::start::{{closure}}::h859fcc5f4226b4ed
       +2138 ┊ smoldot_light::json_rpc_service::background::start::haf715da9ef48a2c7
       +2125 ┊ futures_util::abortable::Abortable<T>::try_poll::h05395081a639f6a5
       -2072 ┊ smoldot_light::json_rpc_service::background::Background<TPlat>::run::{{closure}}::{{closure}}::ha705609b9a1ebe2c
       -1842 ┊ futures_util::abortable::Abortable<T>::try_poll::h1de2dfd766b71ae6
       +1842 ┊ futures_util::abortable::Abortable<T>::try_poll::hfa0b62c336eb5bb3
       +1629 ┊ futures_util::abortable::Abortable<T>::try_poll::hc69260a39906c13b
       -1629 ┊ futures_util::abortable::Abortable<T>::try_poll::hd19a257316c29fcd
       -1267 ┊ smoldot_light::sync_service::SyncService<TPlat>::block_query_unknown_number::{{closure}}::h151ba8148c0b56ee
       +1267 ┊ smoldot_light::sync_service::SyncService<TPlat>::block_query_unknown_number::{{closure}}::hc0167e83a79eebd7
       -1250 ┊ <core::pin::Pin<P> as core::future::future::Future>::poll::h70f0a8dfc0e04a15
       -3241 ┊ ... and 350 more.
       -7173 ┊ Σ [370 Total Rows]

@tomaka tomaka added this pull request to the merge queue Mar 27, 2023
Merged via the queue into smol-dot:main with commit f78b14c Mar 27, 2023
@tomaka tomaka deleted the flatten-tasks branch March 27, 2023 14:23
tomaka added a commit to tomaka/smoldot that referenced this pull request Mar 28, 2023
tomaka added a commit that referenced this pull request Mar 28, 2023
* Add CHANGELOG entry for #349

* Improve phrasing
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.

1 participant