Avoid potential packet allocation in CachedPacket#packet

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-04 00:54:31 +01:00
parent 9542de784b
commit bef4fe37b3
2 changed files with 3 additions and 1 deletions

View File

@ -54,6 +54,9 @@ public final class CachedPacket implements SendablePacket {
* but {@link #retrieve()} should be privileged otherwise.
*/
public @NotNull ServerPacket packet() {
FramedPacket cache;
if (updated == 1 && (cache = packet.get()) != null)
return cache.packet(); // Avoid potential packet allocation
return packetSupplier.get();
}

View File

@ -14,7 +14,6 @@ import java.nio.ByteBuffer;
@ApiStatus.Internal
public record FramedPacket(@NotNull ServerPacket packet,
@NotNull ByteBuffer body) implements SendablePacket {
public FramedPacket {
body = body.position(0).asReadOnlyBuffer();
}