Netty5源码学习之buffer篇(一):PooledHeapByteBuf

PooledHeapByteBuf,带有池的堆内buffer,顾名思义,肯定比一般通过new出来的buffer性能好。把对象放入对象池缓存起来,一般都是因为创建该对象开销比较大,常见的有线程池(ThreadPool)、连接池(ConnectionPool)等。

PooledHeapByteBuf继承关系如下:
PooledHeapByteBuf –》 PooledByteBuf –》 AbstractReferenceCountedByteBuf –》 AbstractByteBuf –》 ByteBuf。
继承关系比较简单清晰。

先介绍几个相关的类:

PooledByteBufAllocator:buffer分配器,用来分配buffer(包括堆内和堆外)。

PoolArena:一块逻辑上的内存池,用来管理和组织buffer的,内部数据结构较复杂。

FastThreadLocal:较快的ThreadLocal(相对于jdk自带的),实现:线程T扩展于FastThreadLocalAccess,InternalThreadLocalMap是它的成员变量,set()时放入InternalThreadLocalMap的成员变量数组,下标是index,get()时从InternalThreadLocalMap的成员变量数组中下标是index处取。

Recycler:回收器,基于FastThreadLocal,缓存的本地对象是个stack,stack里装的是Handler,handler里装的buffer。新建buffer时,从Recycler中的FastThreadLocal取,FastThreadLocal取出当前线程的本地变量stack,stack.pop()弹出handler,handler调用newObject()生成。

练习代码debug下:

1

2

3

4

5

<dependency>

<groupId>io.netty</groupId>

<artifactId>netty-all</artifactId>

<version>5.0.0.Alpha2</version>

</dependency>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

package com.zoo;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.PooledByteBufAllocator;

import io.netty.util.concurrent.FastThreadLocalThread;

/**

*

* @author yankai913@gmail.com

* @date 2015-3-31

*/

public class PooledHeapByteBufTest {

static PooledByteBufAllocator alloc = new PooledByteBufAllocator();

static void singleThread() {

new FastThreadLocalThread(new Runnable() {

@Override

public void run() {

for (int i = 0; i < 3; i++) {

String name = Thread.currentThread().getName();

long start = System.currentTimeMillis();

ByteBuf buf = alloc.buffer(102400);

long end = System.currentTimeMillis();

System.out.println(name + "\t" + buf + ", time:" + (end - start));

}

}

}).start();

}

public static void main(String[] args) throws Exception {

singleThread();

}

}

打印结果:
Thread-1 SimpleLeakAwareByteBuf(PooledHeapByteBuf(ridx: 0, widx: 0, cap: 102400)), time:62
Thread-1 PooledHeapByteBuf(ridx: 0, widx: 0, cap: 102400), time:0
Thread-1 PooledHeapByteBuf(ridx: 0, widx: 0, cap: 102400), time:0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值