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

Context local storage SPI #5048

Merged
merged 19 commits into from
Mar 1, 2024
Merged

Context local storage SPI #5048

merged 19 commits into from
Mar 1, 2024

Conversation

vietj
Copy link
Member

@vietj vietj commented Dec 21, 2023

An SPI for interacting with local context storage.

The existing implementation uses on a concurrent hash map guarded by the context instance.

This new implementation relies on prior knowledge of the set of context locals, so the context allocates upfront the required space to hold the local storage, reducing synchronisation and simplifying lookup that uses allocated index instead of hashed string lookups.

The existing local context data is retrofit as a context local.

@vietj vietj added this to the 5.0.0 milestone Dec 21, 2023
@vietj vietj self-assigned this Dec 21, 2023
@vietj vietj force-pushed the context-local-data-rework branch from 697a562 to 1f8c781 Compare December 21, 2023 20:19
@vietj
Copy link
Member Author

vietj commented Dec 22, 2023

cc @tsegismont

@vietj
Copy link
Member Author

vietj commented Dec 22, 2023

cc @cescoffier

Copy link
Contributor

@tsegismont tsegismont left a comment

Choose a reason for hiding this comment

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

Good job @vietj !

src/main/java/io/vertx/core/Context.java Outdated Show resolved Hide resolved
src/main/java/io/vertx/core/Context.java Outdated Show resolved Hide resolved
src/test/java/io/vertx/test/faketracer/FakeTracer.java Outdated Show resolved Hide resolved
src/test/java/io/vertx/test/faketracer/FakeTracer.java Outdated Show resolved Hide resolved
src/test/java/io/vertx/test/faketracer/FakeTracer.java Outdated Show resolved Hide resolved
src/test/java/io/vertx/core/impl/ContextKeyHelper.java Outdated Show resolved Hide resolved
src/main/java/io/vertx/core/impl/ContextBase.java Outdated Show resolved Hide resolved
@vietj
Copy link
Member Author

vietj commented Dec 22, 2023 via email

@cescoffier
Copy link
Contributor

In Quarkus we don't know the key that may be used. We can imagine knowing a few of them at build time, but we will have an unknown set at runtime.

I would do something like:

  • at build time, request extension to give the keys
  • at runtime, allocate with this set of keys +1
  • the +1 is a concurrent hash map for all keys that have not being declared.

WDYT?

@franz1981
Copy link
Contributor

It is a nice approach @cescoffier which will give performance and flexibility based on the usage, I like it!

@vietj
Copy link
Member Author

vietj commented Jan 2, 2024

@cescoffier note you can have your extension that uses a map (or whatever structure you need) as a slot with a unique key, giving you opportunity to proceed as you want, which is what reactiverse context logging will do, e.g. it could be a ContextKey<Map> and the Map would be managed by Quarkus.

@franz1981
Copy link
Contributor

franz1981 commented Feb 19, 2024

I have some mixed feeling after re-reading what @cescoffier rightly said, and remind me about something I've worked recently eg quarkusio/quarkus#38761 which has a very similar problem ie at build time it can collect what are the "supposely" used keys (in the PR I've referenced are bean ids, but is the same really...), ie: what if the number of actually used keys is much smaller than the ones preallocated?

In shorts, constant lookup access and simple storage (no keys, just values) is traded against the cost of overestimating the capacity, which is not good here. In quarkusio/quarkus#38761 I've tried to keep the allocated cost at it(s) minimum by having lazily allocated parts and I think that we should do something similar for this one, here as well.

Furthermore, I suggest to add some microbenchmarks (I can help with it @vietj ) to compare this approach as it is, but using different key used/total key generated ratios vs a "normal" (but smarter) concurrent hash map, because, for small utilization ratios (<= 1/10) the cost of allocating a full blown array vs using a CHM could be not that different.

@vietj vietj force-pushed the context-local-data-rework branch from 44cdbfd to 957fbd4 Compare February 23, 2024 14:25
@vietj vietj force-pushed the context-local-data-rework branch from aecfc91 to dc57f97 Compare February 28, 2024 16:17
@vietj vietj changed the title Context local data SPI Context local storage SPI Feb 28, 2024
@vietj vietj force-pushed the context-local-data-rework branch from 54335c7 to 46904b9 Compare February 28, 2024 16:32
@vietj vietj marked this pull request as ready for review February 29, 2024 09:23
@vietj vietj merged commit 8de9656 into master Mar 1, 2024
7 checks passed
@vietj vietj deleted the context-local-data-rework branch March 1, 2024 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants