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

[WASM][ST] Use Queue instead of ConcurrentQueue in single-threaded thread pool #111245

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pavelsavara
Copy link
Member

@pavelsavara pavelsavara commented Jan 9, 2025

Motivation: performance and size improvements for single-threaded WASM

  • introduce FEATURE_SINGLE_THREAD define symbol
  • use Queue instead of ConcurrentQueue in single-threaded thread pool
  • protect ConcurrentQueue ctor from trimming when used with JsonSerializer.Deserialize

- use Queue instead of ConcurrentQueue in single-threaded thread pool
- protect ConcurrentQueue ctor from trimming when used with `JsonSerializer.Deserialize`
@pavelsavara pavelsavara added arch-wasm WebAssembly architecture area-VM-threading-mono size-reduction Issues impacting final app size primary for size sensitive workloads labels Jan 9, 2025
@pavelsavara pavelsavara added this to the 10.0.0 milestone Jan 9, 2025
@pavelsavara pavelsavara self-assigned this Jan 9, 2025
@pavelsavara pavelsavara changed the title [browser][ST] Use Queue instead of ConcurrentQueue in single-threaded thread pool [WASM][ST] Use Queue instead of ConcurrentQueue in single-threaded thread pool Jan 9, 2025
@@ -11,6 +12,7 @@ internal sealed class ConcurrentQueueOfTConverter<TCollection, TElement>
{
internal override bool CanPopulate => true;

[DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(ConcurrentQueue<>))]
Copy link
Member

Choose a reason for hiding this comment

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

Why is this necessary? We try really hard to not use DynamicDependency unless it is a last resort.

Copy link
Member Author

Choose a reason for hiding this comment

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

Until now the runtime itself was using ConcurrentQueue ctor and so it was never trimmed.

If we want to keep it friction-less this is one option how.
If we are OK to force users to start doing it themself, we could avoid this DynamicDependency.
Obviously only browser/wasi people will be impacted as thread pool on other platforms will keep it alive anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

See src\libraries\System.Text.Json\tests\System.Text.Json.Tests\TrimmingTests\StackOrQueueNotRootedTest.cs

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a big deal, who needs to deserialize into ConcurrentQueue anyway ?
😉

  • I can disable this test on the browser.
  • Or I can add DynamicDependency in the test, I'm not sure if that would be defeating the test purpose slightly.

I think it's testing trimming of non-generic collections too, which can keep working.

Please let me know @eerhardt, I don't have strong opinion.

Copy link
Member

Choose a reason for hiding this comment

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

I would just work around it in the tests, and not change the product code for it. If you are using the JsonSerializer with trimming/native AOT, the source generator is the only support route.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly architecture area-VM-threading-mono size-reduction Issues impacting final app size primary for size sensitive workloads
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants