Skip to content

Commit f66ea0f

Browse files
committed
netty: starts of custom byte buf
1 parent ab3e21b commit f66ea0f

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.internal.netty;
7+
8+
import edu.umd.cs.findbugs.annotations.NonNull;
9+
import io.jooby.Context;
10+
import io.netty.buffer.ByteBuf;
11+
12+
public class NettyOutputByteArrayStaticNew implements NettyByteBufRef {
13+
private final ByteBuf buf;
14+
private final NettyString contentLength;
15+
private final int length;
16+
17+
protected NettyOutputByteArrayStaticNew(byte[] bytes, int offset, int len) {
18+
this.length = len - offset;
19+
this.buf = new NettyUnsafeHeapByteBuf(this.length, this.length);
20+
this.buf.writeBytes(bytes, offset, length);
21+
this.contentLength = new NettyString(Integer.toString(size()));
22+
}
23+
24+
@Override
25+
public int size() {
26+
return length;
27+
}
28+
29+
@NonNull public ByteBuf byteBuf() {
30+
return buf.slice(0, length);
31+
}
32+
33+
@Override
34+
public void send(Context ctx) {
35+
if (ctx.getClass() == NettyContext.class) {
36+
((NettyContext) ctx).send(buf.slice(), contentLength);
37+
} else {
38+
ctx.send(asByteBuffer());
39+
}
40+
}
41+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.internal.netty;
7+
8+
import io.netty.buffer.ByteBuf;
9+
import io.netty.buffer.UnpooledByteBufAllocator;
10+
import io.netty.buffer.UnpooledUnsafeHeapByteBuf;
11+
12+
/** An un-releasable, un-pooled, un-instrumented, un-safe heap {@code ByteBuf}. */
13+
public final class NettyUnsafeHeapByteBuf extends UnpooledUnsafeHeapByteBuf {
14+
15+
private static final byte[] EMPTY = new byte[0];
16+
17+
public NettyUnsafeHeapByteBuf(int initialCapacity, int maxCapacity) {
18+
super(UnpooledByteBufAllocator.DEFAULT, initialCapacity, maxCapacity);
19+
}
20+
21+
@Override
22+
protected byte[] allocateArray(int initialCapacity) {
23+
if (initialCapacity == 0) {
24+
return EMPTY;
25+
}
26+
return super.allocateArray(initialCapacity);
27+
}
28+
29+
@Override
30+
public ByteBuf retain(int increment) {
31+
return this;
32+
}
33+
34+
@Override
35+
public ByteBuf retain() {
36+
return this;
37+
}
38+
39+
@Override
40+
public ByteBuf touch() {
41+
return this;
42+
}
43+
44+
@Override
45+
public ByteBuf touch(Object hint) {
46+
return this;
47+
}
48+
49+
@Override
50+
public boolean release() {
51+
return false;
52+
}
53+
54+
@Override
55+
public boolean release(int decrement) {
56+
return false;
57+
}
58+
}

modules/jooby-netty/src/test/java/io/jooby/netty/ByteBufBench.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
*/
66
package io.jooby.netty;
77

8+
import java.nio.ByteBuffer;
89
import java.nio.charset.StandardCharsets;
910
import java.util.concurrent.TimeUnit;
1011

1112
import org.openjdk.jmh.annotations.*;
1213

14+
import io.netty.buffer.ByteBuf;
1315
import io.netty.buffer.Unpooled;
1416

1517
@Fork(5)
@@ -20,10 +22,16 @@
2022
@State(Scope.Benchmark)
2123
public class ByteBufBench {
2224
private String string;
25+
private byte[] bytes;
26+
private ByteBuf buf;
27+
private ByteBuffer buffer;
2328

2429
@Setup
2530
public void setup() {
2631
string = "Hello World!";
32+
bytes = string.getBytes(StandardCharsets.UTF_8);
33+
buf = Unpooled.wrappedBuffer(bytes);
34+
buffer = ByteBuffer.wrap(bytes);
2735
}
2836

2937
@Benchmark
@@ -41,8 +49,28 @@ public void stringUtf8Bytes() {
4149
Unpooled.wrappedBuffer(string.getBytes(StandardCharsets.UTF_8));
4250
}
4351

52+
@Benchmark
53+
public void byteBufWrapper() {
54+
Unpooled.wrappedBuffer(bytes);
55+
}
56+
57+
@Benchmark
58+
public void byteBufferWrapper() {
59+
ByteBuffer.wrap(bytes);
60+
}
61+
4462
@Benchmark
4563
public void stringUSAsciiBytes() {
4664
Unpooled.wrappedBuffer(string.getBytes(StandardCharsets.US_ASCII));
4765
}
66+
67+
@Benchmark
68+
public void bufferSlice() {
69+
buffer.slice();
70+
}
71+
72+
@Benchmark
73+
public void byteBufSlice() {
74+
buf.slice();
75+
}
4876
}

0 commit comments

Comments
 (0)