decentraland / decentraland/sdk
Reduce crdts memory allocations
- Dominant language
- No language data
- Stars
- 4
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
Epic: https://github.com/decentraland/sdk/issues/895
Dependency: https://github.com/decentraland/sdk/issues/896
Every time a crdt message is received or sent, a byte array with it data is created, generating a new allocation.
This behavior can be problematic. In an extreme scenario it could crash the client due to memory fragmentation, In common scenario it will generate hiccups related to garbage collection.
The proposed solution is to add object pooling to contain component's serialized data, object type should be analized to find a proper type that can be converted to and from Protobuf's `ByteString` and converted to a `ReadOnlySpan` (maybe a `MomoryStream`?)
Following the considerations:
- the only fixed component data we have is for Transform (44 bytes) for other component length is uncertain (could be 0 bytes or, currently, ~8388608 bytes, but that could change, so we can't assume that value as max size)
- the crdt protocol could use the size of the data or read it's bytes to resolve certain conflicts, meaning that if, for example, a `MemoryStream` is ussed which is get from a pool having a byte array of 60 bytes and now only 30 of those bytes are used for the current message, it should only take into acount those 30 bytes
- since bytes length is uncertain, we should avoid the situation of storing an object with a big buffer for a state that needs a far more small buffer size. for example a 1000 bytes buffer is get from a pool for a state that only needs a 1 byte buffer
- object containing component's serialized data should get released to the pool after being replaced by new component's data in the crdt procotol state (LWW components)
Contributor guide
Assessment
This issue has not been assessed yet.