在为 ARM7 编译的 C 代码中,使用全局变量会提高还是降低性能? [英] Does using global variables increase or decrease performance, in C code compiled for ARM7?

查看:20
本文介绍了在为 ARM7 编译的 C 代码中,使用全局变量会提高还是降低性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为 ARM7 嵌入式平台编译时,在 C 代码中使用大量全局变量会降低还是提高性能?

Does using lots of global variables in C code decrease or increase performance, when compiled for an ARM7 embedded platform?

代码库由多个 C 源代码文件组成,它们使用 extern 关键字引用彼此的全局变量.来自不同源代码文件的不同函数引用不同的全局变量.一些变量是数组.

The code base consists of multiple C source code files which refer each other's global variables using the extern keyword. Different functions from different source code files refer to different global variables. Some of the variables are arrays.

我使用的编译器是 IAR 的 EW ARM kickstart 版 (32kb).

The compiler I'm using is IAR's EW ARM kickstart edition (32kb).

推荐答案

static 变量相比,这将始终降低性能并增加程序大小.您的问题并未具体询问您要比较的什么.我可以看到各种替代方案,

This will always decrease performance and increase program size versus static variables. Your question doesn't specifically ask what you are comparing to. I can see various alternatives,

  1. 与静态变量对比.
  2. 与按值传递的参数相比.
  3. 相对于传递的数组或结构指针中的值.

ARM 博客 详细介绍了如何将常量加载到 arm 寄存器.必须始终执行此步骤以获取全局变量的地址.编译器不会事先知道一个全局变量有多远.如果您将 gcc-lto 一起使用或使用诸如 whole-program 之类的东西,则可以执行更好的优化.基本上,这些会将全局转换为静态.

The ARM blog gives specifics on how to load a constant to an arm register. This step must always be done to get the address of a global variable. The compiler will not know a prior how far away a global is. If you use gcc with -lto or use something like whole-program, then better optimizations can be performed. Basically, these will transform the global to a static.

这里编译器可能会保留一个带有全局基址地址的寄存器,然后用偏移量加载不同的变量;例如 ldr rN, [rX, #offset].也就是说,如果你幸运的话.

Here a compiler may keep a register with the address of a global base and then different variables are loaded with an offset; such as ldr rN, [rX, #offset]. That is, if you are lucky.

RISC CPU 的设计,如 ARM,支持处理所有内存访问的加载/存储单元.通常,加载/存储指令能够采用 [register + offset] 形式.此外,所有 RISC 寄存器都是近似对称的.这意味着任何寄存器都可以用于此偏移访问.通常,如果您将 struct 或数组指针作为参数传递,那么它就变成了同样的事情.即,ldr rN, [rX, #offset].

The design of RISC CPUs, like ARM support a load/store unit which handles all memory accesses. Typically, the load/store instructions are capable of the [register + offset] form. Also, all RISC registers are approximately symmetric. Meaning any register can be used for this offset access. Typically, if you pass a struct or array pointer as a parameter, then it becomes the same thing. Ie, ldr rN, [rX, #offset].

现在,参数的优点是最终,您的例程可以通过传递不同的指针来支持多个数组结构.此外,它还为您提供了将公共数据组合在一起的优势,从而带来缓存优势.

Now, the advantage of the parameter is that eventually, your routines can support multiple arrays or structures by passing different pointers. Also, it gives you the advantage to group common data together which gives cache benefits.

我认为全局变量对 ARM 有害.你应该只使用全局指针,你的代码需要一个单例.或者你有某种同步内存.即,全局变量应该只用于全局功能,而不是用于数据.

I would argues that globals are detrimental on the ARM. You should only use global pointers, where your code needs a singleton. Or you have some sort of synchronization memory. Ie, globals should only be use for global functionality and not for data.

通过堆栈传递所有值显然是低效的,并且会遗漏内存引用或指针的值.

Passing all of the values via the stack is obviously in-efficient and misses the value of a memory reference or pointer.

这篇关于在为 ARM7 编译的 C 代码中,使用全局变量会提高还是降低性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆