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

Boxing interface usage is allocating unnecessary heap data #907

Open
mgravell opened this issue Mar 28, 2024 · 3 comments
Open

Boxing interface usage is allocating unnecessary heap data #907

mgravell opened this issue Mar 28, 2024 · 3 comments

Comments

@mgravell
Copy link

mgravell commented Mar 28, 2024

scenario: using the async API but fully sync in reality after checking completion; each call issues an InternalFasterSession boxed on the heap;

You can see this by running a memory profiler on this branch (branch: faster-boxing; project: /test/Benchmarks) in DEBUG mode (release mode runs BDN; you don't want that). The test runs 25,000 operations in a loop; the only heap alloc is:

Type Allocations
| - FASTER.core.ClientSession<FASTER.core.SpanByte, FASTER.core.SpanByte, Input, Output, FASTER.core.Empty, CacheFunctions>.InternalFasterSession 25,000

Since InternalFasterSession is a struct, this is unlikely to be intentional.

This is almost certainly where InternalFasterSession is being passed as a IFasterSession<Key, Value, Input, Output, Context> - for example DoSlowOperation, AsyncOperationInternal, etc, which will force it to be boxed. To avoid boxing, the type must be either:

  • typed as itself, InternalFasterSession
  • "constrained", i.e. passed (and used as) a generic TSession where TSession : IFasterSession<Key, Value, Input, Output, Context> (used in a few places, such as FasterSession in RmwAsync)

but: any time it is assigned / passed as IFasterSession<...>: it will box, i.e. allocate

@badrishc
Copy link
Contributor

Your findng is accurate, but the reason we have not addressed it is because there is a standard pattern we use to avoid this:

var status = sync-call
if (status.IsPending)
   (await or sync-wait) complete-pending

An example for Read along these lines is in Garnet here: https://github.com/microsoft/garnet/blob/main/libs/server/Storage/Session/MainStore/MainStoreOps.cs#L18

@mgravell
Copy link
Author

mgravell commented Mar 31, 2024 via email

@mgravell
Copy link
Author

mgravell commented Apr 2, 2024

@badrishc I've spiked a switchover to the synchronous API exclusively, with any mention of async only in the (rare) IsPending scenario; I'll run the numbers when my machine is working

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

No branches or pull requests

2 participants