Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily initialize NativeDatagramPacketArray and IovArray in EpollEventLoop #8160

Merged
merged 1 commit into from Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -61,8 +61,10 @@ final class EpollEventLoop extends SingleThreadEventLoop {
private final IntObjectMap<AbstractEpollChannel> channels = new IntObjectHashMap<AbstractEpollChannel>(4096);
private final boolean allowGrowing;
private final EpollEventArray events;
private final IovArray iovArray = new IovArray();
private final NativeDatagramPacketArray datagramPacketArray = new NativeDatagramPacketArray();

// These are initialized on first use
private IovArray iovArray;
private NativeDatagramPacketArray datagramPacketArray;

private final SelectStrategy selectStrategy;
private final IntSupplier selectNowSupplier = new IntSupplier() {
Expand Down Expand Up @@ -144,15 +146,23 @@ public Integer call() throws Exception {
* Return a cleared {@link IovArray} that can be used for writes in this {@link EventLoop}.
*/
IovArray cleanIovArray() {
iovArray.clear();
if (iovArray == null) {
iovArray = new IovArray();
} else {
iovArray.clear();
}
return iovArray;
}

/**
* Return a cleared {@link NativeDatagramPacketArray} that can be used for writes in this {@link EventLoop}.
*/
NativeDatagramPacketArray cleanDatagramPacketArray() {
datagramPacketArray.clear();
if (datagramPacketArray == null) {
datagramPacketArray = new NativeDatagramPacketArray();
} else {
datagramPacketArray.clear();
}
return datagramPacketArray;
}

Expand Down Expand Up @@ -458,8 +468,14 @@ protected void cleanup() {
}
} finally {
// release native memory
iovArray.release();
datagramPacketArray.release();
if (iovArray != null) {
iovArray.release();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also null out reference.

iovArray = null;
}
if (datagramPacketArray != null) {
datagramPacketArray.release();
datagramPacketArray = null;
}
events.free();
}
}
Expand Down
Expand Up @@ -100,8 +100,8 @@ void release() {
@SuppressWarnings("unused")
static final class NativeDatagramPacket {
// Each NativeDatagramPackets holds a IovArray which is used for gathering writes.
// This is ok as NativeDatagramPacketArray is always obtained via a FastThreadLocal and
// so the memory needed is quite small anyway.
// This is ok as NativeDatagramPacketArray is always obtained from an EpollEventLoop
// field so the memory needed is quite small anyway.
private final IovArray array = new IovArray();

// This is the actual struct iovec*
Expand Down