关键改进是将正则匹配函数从boost::regex_match替换为boost::regex_search并配合boost::match_continuous标志。regex_match会对整个日志行进行全量匹配,而regex_search配合match_continuous标志只需匹配日志行的前缀部分。具体实现步骤是:首先自动移除用户行首正则中的.*后缀,然后在正则前添加^锚定符。例如用户配置的cnt.*会被转换为^cnt。核心代码变更为:将boost::regex_match(buffer, buffer + size, reg)替换为boost::regex_search(buffer, buffer + size, what, reg, boost::match_continuous)。测试表明,regex_search的耗时基本稳定,不会随日志长度增加而增长。在实际测试中,Logtail 2.1.1版本优化后的采集速度达到633MB/s,相比优化前的90MB/s提升了约7倍。




