Skip to content

Conversation

bartelink
Copy link
Collaborator

@bartelink bartelink commented Feb 9, 2021

There's an unbounded Dictionary that maintains, for each stream that's ever been processed:
a) the backlog of events that have yet to be processed. Definitely necessary state.
b) the write position. Provides performance benefits as it enables:
i) deduplication of repeated events coming from the input source (the Ingester can throw away the events before they even get to the scheduler)
ii) proactive discarding because a projection has an awareness of the index of the first event in the stream that's going to be useful. For example, for replication to a warm standby that's already got data, but also when catching up or replaying events for arbitrary projection scenarios. (The underlying mechanism is the same as used for the previous scenario - the handler can yield a response that indicates the lowest position for which we are interested in processing events)

However, being unbounded and ever growing, that obviously risks running out of memory.

This PR optimizes the in-memory representation to delay (not prevent) this consequence.

The existing memory layout was:

Type layout for 'StreamState`1'
Size: 24 bytes. Paddings: 7 bytes (%29 of empty space)
|========================================|
| Object Header (8 bytes)                |
|----------------------------------------|
| Method Table Ptr (8 bytes)             |
|========================================|
|   0-7: FSharpOption`1 write@ (8 bytes) |
|----------------------------------------|
|  8-15: StreamSpan`1[] queue@ (8 bytes) |
|----------------------------------------|
|    16: Boolean isMalformed@ (1 byte)   |
|----------------------------------------|
| 17-23: padding (7 bytes)               |
|========================================|

This PR changes from an Object / type to a struct/Struct with the following layout:

Type layout for 'StreamState`1'
Size: 16 bytes. Paddings: 0 bytes (%0 of empty space)
|========================================|
|   0-7: StreamSpan`1[] queue@ (8 bytes) |
|----------------------------------------|
|  8-15: Int64 write@ (8 bytes)          |
|========================================|

@bartelink bartelink changed the title Steams: Reduce memory usage for streams with only write positions Streams: Reduce memory usage for streams with only write positions Feb 9, 2021
@bartelink bartelink merged commit 4224c3e into master Feb 11, 2021
@bartelink bartelink deleted the reduce-memory-usage branch February 11, 2021 14:25
bartelink added a commit that referenced this pull request Feb 11, 2021
@bartelink bartelink mentioned this pull request Feb 12, 2021
bartelink added a commit that referenced this pull request Feb 15, 2021
bartelink added a commit that referenced this pull request Feb 15, 2021
bartelink added a commit that referenced this pull request Mar 12, 2021
bartelink added a commit that referenced this pull request Mar 12, 2021
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.

1 participant