01
02
03
04
05
06
07
08
09
10
11
12
13
14
伪代码:问题发现
伪代码:问题发现
void process(Message* msg)
{
Parser* p = find_parser(msg->type);
if (p != NULL)
{
p->handle(msg);
}
}
分析:
每一条信息过来,就要查找,性能的瓶颈在这里:Parser* p = find_parser(msg->type);
后来发现有一个局部性表现,短时间内有大量的消息打印,消息类型是一样的
程序的局部性原理=》缓存=》利用缓存的机制处理问题
p->handle(msg); // 每个消息都有一个线程,放到线程里,这里没有性能瓶颈
优化:突破性能瓶颈
优化:突破性能瓶颈
void process(Message* msg)
{
Static Parser* cache = NULL;
// 先判断缓存的解析器是否能够解析消息
if ( (cache != NULL) && (cache->id == message->type) )
{
cache->handle(msg);
}
else
{
Parser* p = find_parser(msg->type);
if (p != NULL)
{
p->handle(msg);
cache = p;
}
}
}
分析:避免了一些查找
后来再次优化,好嘞只有一个缓存,能不能多几个缓存?
优化:二级缓存
优化:二级缓存
void process(Message* msg)
{
Static Parser* cache1 = NULL;
Static Parser* cache2 = NULL;
// 先判断缓存的解析器是否能够解析消息
if ( (cache != NULL) && (cache->id == message->type) )
{
cache->handle(msg);
}
else
{
Parser* p = find_parser(msg->type);
if (p != NULL)
{
p->handle(msg);
cache = p;
}
}
}
分析:
解决短时间的大量消息和大量消息属于几种类型? 伪代码待完善