Netty中的缓冲区为什么比原生NIO更高效

System.out.println();

//6.再次写入一段内容,比第一段内容少

byte[] bytes2 = {1,2,3};

buf.writeBytes(bytes2);

System.out.println(“再写入的bytes2为:” + Arrays.toString(bytes2));

System.out.println(“再写入一段内容后ByteBuf为:” + buf);

System.out.println(“6.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//7.将ByteBuf清空

buf.setZero(0, buf.capacity());

System.out.println(“内容清空后ByteBuf为:” + buf);

System.out.println(“7.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

//8.再次写入一段超过容量的内容

byte[] bytes3 = {1,2,3,4,5,6,7,8,9,10,11};

buf.writeBytes(bytes3);

System.out.println(“写入超量的bytes3为:” + Arrays.toString(bytes3));

System.out.println(“写入超量内容后ByteBuf为:” + buf);

System.out.println(“8.ByteBuf中的内容为:” + Arrays.toString(buf.array()));

System.out.println();

}

}

复制代码

从这些api的使用中就可以体会到ByteBuf比ByteBuffer的强大之处,我们可以深入研究一下它在写入超量数据时的扩容机制,也就是buf.writeBytes(byte[])方法


ByteBuf动态扩容


容量默认值为256字节,最大值为Integer.MAX_VALUE,也就是2GB

实际调用AbstractByteBuf.writeBytes,如下:

AbstractByteBuf.writeBytes

@Override

public ByteBuf writeBytes(byte[] src) {

writeBytes(src, 0, src.length);

return this;

}

复制代码

AbstractByteBuf.writeBytes(src, 0, src.length);

@Override

public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {

ensureWritable(length); //检查是否有足够的可写空间,是否需要扩容

setBytes(writerIndex, src, srcIndex, length);

writerIndex += length;

return this;

}

复制代码

AbstractByteBuf.ensureWritable(length);

@Override

public ByteBuf ensureWritable(int minWritableBytes) {

ensureWritable0(checkPositiveOrZero(minWritableBytes, “minWritableBytes”));

return this;

}

复制代码

AbstractByteBuf.ensureWritable0(checkPositiveOrZero(minWritableBytes, “minWritableBytes”));

final void ensureWritable0(int minWritableBytes) {

final int writerIndex = writerIndex(); //获取当前写下标

final int targetCapacity = writerIndex + minWritableBytes; //计算最少需要的容量

// using non-short-circuit & to reduce branching - this is a hot path and targetCapacity should rarely overflow

if (targetCapacity >= 0 & targetCapacity <= capacity()) { //判断当前容量是否够用

ensureAccessible(); //检查ByteBuf的引用计数,如果为0则不允许继续操作

return;

}

if (checkBounds && (targetCapacity < 0 || targetCapacity > maxCapacity)) { //判断需要的容量是否是合法值,不合法为true直接抛出越界异常

ensureAccessible();//检查ByteBuf的引用计数,如果为0则不允许继续操作

throw new IndexOutOfBoundsException(String.format(

“writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s”,

writerIndex, minWritableBytes, maxCapacity, this));

}

// Normalize the target capacity to the power of 2.(标准化为2的次幂)

final int fastWritable = maxFastWritableBytes();

int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
alloc().calculateNewCapacity(targetCapacity, maxCapacity); //计算扩容后容量(只要扩容最小64)

// Adjust to the new capacity.

capacity(newCapacity); //设置新的容量

}

复制代码

alloc().calculateNewCapacity(targetCapacity, maxCapacity) -> AbstractByteBufAllocator

@Override

public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {

checkPositiveOrZero(minNewCapacity, “minNewCapacity”); //最小所需容量

if (minNewCapacity > maxCapacity) { //判断最小所需容量是否合法

throw new IllegalArgumentException(String.format(

“minNewCapacity: %d (expected: not greater than maxCapacity(%d)”,

minNewCapacity, maxCapacity));

}

final int threshold = CALCULATE_THRESHOLD; // 4 MiB page 阈值超过4M以其他方式计算

if (minNewCapacity == threshold) { //等于4M直接返回4M

return threshold;

}

// If over threshold, do not double but just increase by threshold.

if (minNewCapacity > threshold) { //大于4M,不需要加倍,只需要扩大阈值即可

int newCapacity = minNewCapacity / threshold * threshold;

if (newCapacity > maxCapacity - threshold) {

newCapacity = maxCapacity;

} else {

newCapacity += threshold;

}

return newCapacity;

}

// 64 <= newCapacity is a power of 2 <= threshold

final int newCapacity = MathUtil.findNextPositivePowerOfTwo(Math.max(minNewCapacity, 64)); //计算不少于所需容量的最小的2次幂的值

return Math.min(newCapacity, maxCapacity); //取容量所允许的最大值和计算的2次幂的最小值,当然在这儿就是newCapacity=64

}

复制代码

总结一下就是最小所需容量是否等于阈值,如果是直接返回阈值此后直接扩大阈值,否则以64为最小2次幂为基础每次扩大二倍直到阈值.


选择合适的ByteBuf实现


netty针对ByteBuf提供了8中具体的实现方式,如下:

| 堆内/堆外 | 是否池化 | 访问方式 | 具体实现类 | 备注 |

| — | — | — | — | — |

| heap堆内 | unpool | safe | UnpooledHeapByteBuf | 数组实现 |

| heap堆内 | unpool | unsafe | UnpooledUnsafeHeapByteBuf | Unsafe类直接操作内存 |

| heap堆内 | pool | safe | PooledHeapByteBuf | |

| heap堆内 | pool | unsafe | PooledUnsafeHeapByteBuf | ~ |

| direct堆外 | unpool | safe | UnpooledDirectByteBuf | NIO DirectByteBuffer |

| direct堆外 | unpool | unsafe | UnpooleUnsafedDirectByteBuf | ~ |

| direct堆外 | pool | safe | PooledDirectByteBuf | ~ |

| direct堆外 | pool | unsafe | PooledUnsafeDirectByteBuf | ~ |

在使用时,都是通过ByteBufAllocator分配器进行申请,同时分配器具有内存管理的功能。

在这儿堆内和堆外没有什么区别,对api的使用时一样的,仅仅是通过Unpooled申请的不一样.

那个safe和unsafe有什么区别呢?

以UnpooledHeapByteBuf和UnpooledUnsafeHeapByteBuf中的getByte(int index)方法为例进行分析

UnpooledHeapByteBuf

@Override

public byte getByte(int index) {

ensureAccessible();

return _getByte(index); //真正的获取字节的方法

}

@Override

protected byte _getByte(int index) {

return HeapByteBufUtil.getByte(array, index); //通过HeapByteBufUtil工具类获取数据

}

复制代码

HeapByteBufUtil

static byte getByte(byte[] memory, int index) {

return memory[index];

}

复制代码

UnpooledHeapByteBuf从堆内数组中获取数据,这是安全的

UnpooledUnsafeHeapByteBuf

@Override

public byte getByte(int index) {

checkIndex(index);

return _getByte(index);

}

@Override

protected byte _getByte(int index) {

return UnsafeByteBufUtil.getByte(array, index);

}

复制代码

PlatformDependent0

static byte getByte(byte[] data, int index) {

return UNSAFE.getByte(data, BYTE_ARRAY_BASE_OFFSET + index);

}

复制代码

UnpooledUnsafeHeapByteBuf是通过UNSAFE来操作内存的


现在我们来研究一下Unsafe

Unsafe的实现

Unsafe意味着不安全的操作,但是更底层的操作会带来性能提升和特殊功能,Netty中会尽力使用unsafe以提升系统性能

Java语言很重要的特性就是一次编译到处运行,所以它针对底层的内存或者其他操作做了很多封装,而unsafe提供了一系列我们操作底层的方法,可能会导致不兼容或不可知的异常.

比如:

  • 返回一些低级的内存信息

  • addressSize

  • pageSize

  • 提供用于操作对象及其字段的方法

  • allocateInstance

  • objectFieldOffset

  • 提供用于操作类及其静态字段的方法

  • staticFieldOffset

  • defineClass

  • defineAnonymousClass

  • ensureClassInitialized

  • 低级的同步原语

  • monitorEnter

  • tryMonitorEnter

  • monitorExit

  • compareAndSwapInt

  • putOrderedInt

  • 直接访问内存的方法

  • allocateMomery

  • copyMemory

  • freeMemory

  • getAddress

  • getInt

  • putInt

  • 操作数组

  • arrayBaseOffset

  • arrayIndexScale

既然这些东西都是jdk封装好的,而是netty也是直接使用的,所以我们无论在使用safe还是unsafe的时候都是无感知的,我们无需关系底层的操作逻辑,因为api都是一样的,只是实现不一样


是否还有一个疑问,池化和非池化是什么意思?

池化和非池化

比如在使用Unpooled.buffer(10)申请一个缓存区的时候,默认非池化申请的一个缓冲区.

池化和非池化的区别主要是申请内存缓存空间以及缓存空间的使用上,体现为内存复用.

  • 在申请内存缓存空间方面:

  • pool:池化申请的时候会申请一个比当前所需内存空间更大的内存空间,这就好比一个快递柜,为此netty提供了buf分配管理器专门用来处理这种事情,来创建或复用ByteBuf.

  • unpool:非池化申请只会申请特定大小能够使用的内存缓存空间,使用完之后立刻释放,这就像直接把快递放到你的手中,你所在的位置就是开辟的内存空间.

  • 在缓存空间使用方面:

  • pool:池化申请的内存空间有一定扩容容积,也就是这个快递柜可以存放多个快递,只需要找到对应的方格即可存放,同样buf分配管理器来复用已经创建好的内存空间,在创建ByteBuf的时候已经开辟3中大小的内存块

  • normal:16MN

  • small:8KB

  • tiny:512B

  • unpool:毫无疑问,非池化的方式必然是每次都会再去开辟内存空间的.

理论如此,netty中是如何做到内存复用的?

在netty中每一个EventLoopThread由PooledBytebufAllocator内存分配器实力维护了一个线程变量叫做PoolThreadCache,在这个变量中维护了3种规格的MemoryRegionCache数组用作内存缓存,MemoryRegionCache内部是链表,队列里面存Chunk.

首先内存内存分配器会寻找合适的ByteBuf对象进行复用;

之后从内存数组中找到合适的内存空间进行复用;

PoolChunk里面维护了内存引用,内存复用的做法就是把ByteBuf的memory指向chunk的memory.

如果没有找到对应的缓存空间,则直接向内存申请unpool的缓存空间.

netty中默认(池化)也是这样做的,这也是netty性能高效的一个原因,但是就像example例子一样,如果我们自己创建的话,netty推荐我们使用unpool.

image.png

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-SL5ue6W5-1715853819972)]

[外链图片转存中…(img-phIUWD92-1715853819973)]

[外链图片转存中…(img-hkSBbjYv-1715853819973)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值