280 Star 1.8K Fork 778

OpenHarmony / kernel_liteos_a

 / 详情

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

Done
Task
Opened this issue  
2020-09-23 11:09

该问题是怎么引起的?

代码检视

重现步骤

代码检视

报错信息

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

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

Comments (3)

螺丝钉 created任务
螺丝钉 set related repository to OpenHarmony/kernel_liteos_a
Expand operation logs

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

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

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

反汇编了一下:

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而重复计算的问题。

螺丝钉 changed issue state from 待办的 to 已完成

Sign in to comment

Status
Assignees
Projects
Milestones
Pull Requests
Successfully merging a pull request will close this issue.
Branches
Planed to start   -   Planed to end
-
Top level
Priority
Duration (hours)
Confirm
参与者(3)
7989137 caoruihong 1601432415
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

181749 a2d7925e 1850385 181749 9f8568a7 1850385