MicroPython 通过一些优化实现了10%的速度提升

其它相关内容
问题讨论
回复
头像
shaoziyang
帖子: 3917
注册时间: 2019年 10月 21日 13:48

MicroPython 通过一些优化实现了10%的速度提升

#1

帖子 shaoziyang »

Pimoroni在MicroPython中通过一些优化实现了10%的速度提升
Today qstr implementation scans strings sequntially.
In cases there are many strings this can become very inefficient.
This change improves qstr search performance by using binary search in
sorted qstr pools, when possible.

This change introduces an option to create a sorted string pool, which
is then searched by a binary search instead of sequential search.

qstr pool can be either "sorted" or "unsorted", whereas the unsorted is
searched sequentally as today.
Native modules (MP_ROM_QSTR) and frozen modules generate sorted pools.
Currently runtime strings are unsorted.

The constant string pools is split into two and a new pool is introduced,
"special_const_pool". This is required because the first sequence of
strings already requires special ordering therefore created unsorted,
while the rest of the constants are generated sorted.

qstr_find_strn searches strings in each pool. If the pool is sorted and
larger than a threshold, it will be search using binary search instead
of sequential search, significantly improving performance.
今天的qstr实现是按顺序扫描字符串的。
在有很多字符串的情况下,这可能会变得非常低效。
这个改动通过在qstr池中尽可能使用二进制搜索来提高qstr的搜索性能。
排序的qstr池中使用二进制搜索。
此项修改引入了一个选项,可以创建一个排序的字符串池,然后用二进制搜索的方式进行搜索。
然后通过二进制搜索而不是顺序搜索进行搜索。
qstr池可以是 "已排序的 "或 "未排序的",而未排序的则是
顺序搜索,就像现在一样。
本地模块(MP_ROM_QSTR)和冻结模块会产生排序的池。
目前运行时字符串是未排序的。
常量字符串池被分成两个,并引入一个新的池。
"special_const_pool"。这是有必要的,因为第一个序列的
字符串已经需要特殊排序,因此创建了未排序的字符串。
而其余的常量是按排序生成的。
qstr_find_strn在每个池中搜索字符串。如果这个池子是被排序的并且
大于一个阈值,它将使用二进制搜索而不是顺序搜索来搜索。
而不是顺序搜索,这将显著提高性能。

https://github.com/pimoroni/micropython ... 2ab90d7af7

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章