zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Postgresql引入SIMD指令集

postgresql 引入 指令集 SIMD
2023-06-13 09:13:11 时间

相关 【1】https://postgrespro.com/blog/pgsql/5969859#commit_37a6e5df 【2】《Postgresql源码(65)新快照体系Globalvis工作原理分析》

1 概要

快照相关逻辑一直是PG高并发场景下的瓶颈点,经过PG13的几次重大优化后,GetSnapshotData的性能已经有显著提升(解决CPU false sharing问题)。

最近社区又结合硬件能力继续对快照场景进行优化,优化代码不多但效果非常好,思路也值得借鉴(PG代码中有大量应用场景)。

2 优化内容

Optimized lookups in snapshots commit: 37a6e5df, b6ef1675, 82739d4a

The patch optimizes linear searches of arrays (first commit) for x86-64 (second commit) and ARM (third commit). The new algorithm using SIMD instructions was applied to snapshot->xip array search. At a large number (hundreds) of concurrent writers, it significantly increases the visibility check speed, which is an overall performance boost.


37a6e5df, b6ef1675, 82739d4a三个patch对数组的线性搜索有很大优化:

优化前:原来snapshot->xip存的是排序后的事务ID,判断一个XID是不是在这个数组中,需要一个一个比较:

		for (i = 0; i < snapshot->xcnt; i++)
		{
			if (TransactionIdEquals(xid, snapshot->xip[i]))
				return true;

patch引入SIMD指令集(支持X86和ARM),使大量并发写的场景下,对整体性能有大幅度提升。

优化后:

		if (pg_lfind32(xid, snapshot->xip, snapshot->xcnt))
			return true;

DIFF