Skip to content

Sinks.many().replay().latest() holds two values in cache #3340

@MikkelHJuul

Description

@MikkelHJuul

The caching used for Sinks.many().replay().latest() holds both head and tail in the cache, although only one is available for replay (as it should). This means that the extra reference holds unnecessary memory, and holds onto a reference that cannot be reclaimed.

Expected Behavior

The cache holds only a single reference in memory.

Actual Behavior

The https://github.com/reactor/reactor-core/blob/main/reactor-core/src/main/java/reactor/core/publisher/FluxReplay.java#L774 SizeBoundReplayBuffer holds two items in cache.

Steps to Reproduce

package reactor.core.publisher;

@Test
void reproCase() {
     var replayBuffer = new FluxReplay.SizeBoundReplayBuffer<Integer>(1);
     replayBuffer.add(11);
     replayBuffer.add(22);
     assert replayBuffer.head != replayBuffer.tail;
     assert replayBuffer.head.value != replayBuffer.tail.value;
}

Possible Solution

package reactor.core.publisher;

@Test
void fixCase() {
     var replayBuffer = new FluxReplay.SizeBoundReplayBuffer<Integer>(0);
     replayBuffer.add(11);
     replayBuffer.add(22);
     assert replayBuffer.head == replayBuffer.tail;
     assert replayBuffer.head.value == replayBuffer.tail.value;
     assert replayBuffer.head.value == 22;
}

Your Environment

doesn't bloody matter

  • Reactor version(s) used: 3.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/declinedWe feel we shouldn't currently apply this change/suggestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions