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

concurrent-api: shave some allocations from new Scope #3185

Conversation

bryce-anderson
Copy link
Contributor

Motivation:

In the allocation profile we see anonymous instances of the new Scope being created. While not a big deal, we can do better.

Modifications:

We can shave these off of the default implementation by making CopyOnWriteContextMap, and really any context map, an instance of Scope. The close() method just needs to then ask the current provider to set the context to itself.

Result:

Less allocations.

Motivation:

In the allocation profile we see anonymous instances of the new Scope being
created. While not a big deal, we can do better.

Modifications:

We can shave these off of the default implementation by making CopyOnWriteContextMap,
and really any context map, an instance of Scope. The close() method just needs
to then ask the current provider to set the context to itself.

Result:

Less allocations.
* Set the current {@link ContextMap}
* @param contextMap the {@link ContextMap} to set in the thread local state.
*/
void setContextMap(ContextMap contextMap);
Copy link
Member

Choose a reason for hiding this comment

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

Then it looks more like ContextMapHolder, should we just make this interface extend the holder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did do it, but it's a bit itchy due to the @Nullable annotations and the return type of .context(ContextMap).

Copy link
Member

@idelpivnitskiy idelpivnitskiy left a comment

Choose a reason for hiding this comment

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

Nice improvement!
I'm going to merge it as-is, my optional comments may become part of your other PR if you like them:

@idelpivnitskiy idelpivnitskiy merged commit b625ab7 into apple:main Feb 6, 2025
11 checks passed
@bryce-anderson bryce-anderson deleted the bl_anderson/ShaveSomeScopeAllocations branch February 6, 2025 00:48
bryce-anderson added a commit to bryce-anderson/servicetalk that referenced this pull request Feb 6, 2025
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.

2 participants