1 /*
2 * Copyright 2025 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.channel.uring;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufAllocator;
20 import io.netty.util.internal.AdaptiveCalculator;
21
22 import java.util.Objects;
23
24 /**
25 * {@link IoUringBufferRingAllocator} implementation which uses an adaptive strategy to allocate buffers, which
26 * will decrease / increase the buffer size depending on if the allocated buffers were completely used or not before.
27 */
28 public final class IoUringAdaptiveBufferRingAllocator implements IoUringBufferRingAllocator {
29
30 public static final int DEFAULT_MINIMUM = 1024;
31 public static final int DEFAULT_INITIAL = 4096;
32 public static final int DEFAULT_MAXIMUM = 65536;
33
34 private final ByteBufAllocator allocator;
35 private final AdaptiveCalculator calculator;
36
37 public IoUringAdaptiveBufferRingAllocator() {
38 this(ByteBufAllocator.DEFAULT);
39 }
40
41 /**
42 * Creates new instance.
43 *
44 * @param allocator the {@link ByteBufAllocator} to use.
45 */
46 public IoUringAdaptiveBufferRingAllocator(ByteBufAllocator allocator) {
47 this(allocator, DEFAULT_MINIMUM, DEFAULT_INITIAL, DEFAULT_MAXIMUM);
48 }
49
50 /**
51 * Creates new instance.
52 *
53 * @param allocator the {@link ByteBufAllocator} to use for the allocations
54 * @param minimum the inclusive lower bound of the expected buffer size
55 * @param initial the initial buffer size when no feed back was received
56 * @param maximum the inclusive upper bound of the expected buffer size
57 */
58 public IoUringAdaptiveBufferRingAllocator(ByteBufAllocator allocator, int minimum, int initial, int maximum) {
59 this.allocator = Objects.requireNonNull(allocator, "allocator");
60 this.calculator = new AdaptiveCalculator(minimum, initial, maximum);
61 }
62
63 @Override
64 public ByteBuf allocate() {
65 return allocator.directBuffer(calculator.nextSize());
66 }
67
68 @Override
69 public void lastBytesRead(int attempted, int actual) {
70 // If we read as much as we asked for we should check if we need to ramp up the size of our next guess.
71 // This helps adjust more quickly when large amounts of data is pending and can avoid going back to
72 // the selector to check for more data. Going back to the selector can add significant latency for large
73 // data transfers.
74 if (attempted == actual) {
75 calculator.record(actual);
76 }
77 }
78 }