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

[#5770] Use heapbuffers by default when using LocalChannel and LocalS… #5792

Closed
wants to merge 1 commit into from
Closed
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 @@ -104,10 +104,12 @@ public void run() {

public LocalChannel() {
super(null);
config().setAllocator(new PreferHeapByteBufAllocator(config.getAllocator()));
}

LocalChannel(LocalServerChannel parent, LocalChannel peer) {
super(parent);
config().setAllocator(new PreferHeapByteBufAllocator(config.getAllocator()));
this.peer = peer;
localAddress = parent.localAddress();
remoteAddress = peer.localAddress();
Expand Down
Expand Up @@ -46,6 +46,10 @@ public void run() {
private volatile LocalAddress localAddress;
private volatile boolean acceptInProgress;

public LocalServerChannel() {
config().setAllocator(new PreferHeapByteBufAllocator(config.getAllocator()));
}

@Override
public ChannelConfig config() {
return config;
Expand Down
@@ -0,0 +1,132 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.local;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;

/**
* Wraps another {@link ByteBufAllocator} and use heapbuffers everywhere except when a direct buffer is explicit
* requested.
*/
final class PreferHeapByteBufAllocator implements ByteBufAllocator {
Copy link
Member

@nmittler nmittler Sep 2, 2016

Choose a reason for hiding this comment

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

Any thoughts on making this a bit more generic? Something like BufferPreferenceAllocator that can be initialized with a boolean of direct vs heap?

Don't really have any use case yet for anything other than this ... maybe you can think of one?

Copy link
Member Author

Choose a reason for hiding this comment

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

@nmittler not sure if we should... Do you think it is useful in other cases as well ? If not I would prefer to keep it package-private and we can open up once we need too. WDYT ?

Copy link
Member

Choose a reason for hiding this comment

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

meh ... it's fine as-is. Just a thought :)

private final ByteBufAllocator allocator;

PreferHeapByteBufAllocator(ByteBufAllocator allocator) {
this.allocator = allocator;
}

@Override
public ByteBuf buffer() {
return allocator.heapBuffer();
}

@Override
public ByteBuf buffer(int initialCapacity) {
return allocator.heapBuffer(initialCapacity);
}

@Override
public ByteBuf buffer(int initialCapacity, int maxCapacity) {
return allocator.heapBuffer(initialCapacity, maxCapacity);
}

@Override
public ByteBuf ioBuffer() {
return allocator.heapBuffer();
}

@Override
public ByteBuf ioBuffer(int initialCapacity) {
return allocator.heapBuffer(initialCapacity);
}

@Override
public ByteBuf ioBuffer(int initialCapacity, int maxCapacity) {
return allocator.heapBuffer(initialCapacity, maxCapacity);
}

@Override
public ByteBuf heapBuffer() {
return allocator.heapBuffer();
}

@Override
public ByteBuf heapBuffer(int initialCapacity) {
return allocator.heapBuffer(initialCapacity);
}

@Override
public ByteBuf heapBuffer(int initialCapacity, int maxCapacity) {
return allocator.heapBuffer(initialCapacity, maxCapacity);
}

@Override
public ByteBuf directBuffer() {
return allocator.directBuffer();
}

@Override
public ByteBuf directBuffer(int initialCapacity) {
return allocator.directBuffer(initialCapacity);
}

@Override
public ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
return allocator.directBuffer(initialCapacity, maxCapacity);
}

@Override
public CompositeByteBuf compositeBuffer() {
return allocator.compositeHeapBuffer();
}

@Override
public CompositeByteBuf compositeBuffer(int maxNumComponents) {
return allocator.compositeHeapBuffer(maxNumComponents);
}

@Override
public CompositeByteBuf compositeHeapBuffer() {
return allocator.compositeHeapBuffer();
}

@Override
public CompositeByteBuf compositeHeapBuffer(int maxNumComponents) {
return allocator.compositeHeapBuffer(maxNumComponents);
}

@Override
public CompositeByteBuf compositeDirectBuffer() {
return allocator.compositeDirectBuffer();
}

@Override
public CompositeByteBuf compositeDirectBuffer(int maxNumComponents) {
return allocator.compositeDirectBuffer(maxNumComponents);
}

@Override
public boolean isDirectBufferPooled() {
return allocator.isDirectBufferPooled();
}

@Override
public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
return allocator.calculateNewCapacity(minNewCapacity, maxCapacity);
}
}