187 Unstar Star 1.1K Fork 401

OpenHarmony / kernel_liteos_a

循环上限值每次都计算,性能不好

任务
已完成
螺丝钉  Opened this issue

该问题是怎么引起的?

代码检视

重现步骤

代码检视

报错信息

kernel_liteos_a/kernel/base/core/los_bitmap.c
line:131

循环上限值BITMAP_NUM_WORDS(numBits)应该先计算好,不要把计算式作为循环体的一部分,导致每次循环都需要算一次,影响性能。应该提前算好直接使用即可

Attachments
7989137 caoruihong 1601432415 total 3 participants

Comments (3)

bitbegin 2020-09-23 23:39

现在的编译器早就优化掉了吧

螺丝钉 2020-09-24 19:53

现在的编译器早就优化掉了吧

@bitbegin 你看,你不也是不确定的口吻么,既然不确定,代码能优化的建议优化掉呗~~

7989137 caoruihong 1601432415
Caoruihong 2020-09-24 23:47

反汇编了一下:

Disassembly of section .text.LOS_BitmapFfz:

00000000 <LOS_BitmapFfz>:
   0:   e281301f        add     r3, r1, #31
   4:   e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
   8:   e28db000        add     fp, sp, #0
   c:   e1b022a3        lsrs    r2, r3, #5
  10:   0a000017        beq     74 <LOS_BitmapFfz+0x74>
  14:   e5903000        ldr     r3, [r0]
  18:   e3730001        cmn     r3, #1
  1c:   03a0c000        moveq   ip, #0
  20:   0a00000b        beq     54 <LOS_BitmapFfz+0x54>
  24:   e3a02000        mov     r2, #0
  28:   e1e03003        mvn     r3, r3
  2c:   e6ff0f33        rbit    r0, r3
  30:   e3530000        cmp     r3, #0
  34:   e16f0f10        clz     r0, r0
  38:   03e00000        mvneq   r0, #0
  3c:   e6f20070        uxtah   r0, r2, r0
  40:   e1500001        cmp     r0, r1
  44:   23e00000        mvncs   r0, #0
  48:   e28bd000        add     sp, fp, #0
  4c:   e49db004        pop     {fp}            ; (ldr fp, [sp], #4)
  50:   e12fff1e        bx      lr
  54:   e28cc001        add     ip, ip, #1
  58:   e15c0002        cmp     ip, r2
  5c:   0a000004        beq     74 <LOS_BitmapFfz+0x74>
  60:   e5b03004        ldr     r3, [r0, #4]!
  64:   e3730001        cmn     r3, #1
  68:   0afffff9        beq     54 <LOS_BitmapFfz+0x54>
  6c:   e1a0228c        lsl     r2, ip, #5
  70:   eaffffec        b       28 <LOS_BitmapFfz+0x28>
  74:   e3e00000        mvn     r0, #0
  78:   eafffff2        b       48 <LOS_BitmapFfz+0x48>

BITMAP_NUM_WORDS(numBits)展开后就是(numBits + 31) / 32, 对应上面汇编就是前4条指令,因此并不存在因为for而重复计算的问题。

+1 9

Sign in to comment

Assignees
Labels
Not set
Projects
Milestones
Branches
Planed to start
Not set
Planed to end
Not set
Top level
Priority
C
1
https://git.oschina.net/openharmony/kernel_liteos_a.git
git@git.oschina.net:openharmony/kernel_liteos_a.git
openharmony
kernel_liteos_a
kernel_liteos_a

Search

132457 8cb2edc1 1899542 131848 70c8d3a4 1899542