内存访问优化 [英] Memory access optimization

查看:61
本文介绍了内存访问优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




请看一下这个示例代码:


class MyClass {

private :

static int length;

public:

static void setLength(int newLength);

void do( );

}


int MyClass :: length;


void MyClass :: setLength(int newLength) {

length = newLength;

}


void MyClass :: do(){

for(int i = 0; i< length; i ++){

//一些简单的浮点加法和乘法...

}

}


我预计编译器(用于ARM的Microsoft EVC 4,完整的

优化)

将优化for-方法的循环do to

register int t = length;

for(int i = 0; i< t; i ++){

...

}

但这不会发生。相反,生成的机器代码

重新获取长度。在for循环的每次迭代中。为什么?

编译器是否恐惧 for-loop运行时,另一个线程可以调用setLength

吗? "长度"是*不*声明为volatile。


是否有正式的规范允许编译器缓存多长时间?
寄存器中的非局部变量?例如,可以代码如

a =长度;

b =长度;

总是应该优化为

注册int t = length;

a = t;

b = t;




rs

Hi,

please take a look to this sample code:

class MyClass {
private:
static int length ;
public:
static void setLength(int newLength) ;
void do() ;
}

int MyClass::length ;

void MyClass::setLength(int newLength) {
length=newLength ;
}

void MyClass::do() {
for(int i=0;i<length;i++) {
// some simple floating point additions and multiplications ...
}
}

I expected that the compiler (Microsoft EVC 4 for ARM, full
optimization)
would optimize the for-loop of the method "do" to
register int t=length ;
for(int i=0;i<t;i++) {
...
}
But this does not happen. Instead, the generated machine code
refetches "length" in each iteration of the for-loop. Why?
Does the compiler "fear" that another thread could call "setLength"
while the for-loop runs? "length" is *not* declared volatile.

Is there a formal specification how long a compiler is allowed to cache
non-local variables in registers? For example, can code like
a = length ;
b = length ;
always be expected to be optimized to
register int t=length ;
a=t ;
b=t ;
?

rs

推荐答案

ro **** *@gmx.de 写道:


请看一下这个示例代码:

class MyClass {<私有:
static int length;
public:
static void setLength(int newLength);
void do();
}

int MyClass :: length;

void MyClass :: setLength(int newLength){
length = newLength;
}

void MyClass :: do(){
for(int i = 0; i< length; i ++){
//一些简单的浮点加法和乘法......
}
}

我预计编译器(用于ARM的Microsoft EVC 4,完全优化)
会opti mize方法的for循环do to
注册int t = length;
for(int i = 0; i< t; i ++){
...
}
但这不会发生。相反,生成的机器代码重新获得长度。在for循环的每次迭代中。为什么?
编译器是否恐惧 for-loop运行时,另一个线程可以调用setLength
吗? "长度"是*不*声明为volatile。

是否有正式的规范允许编译器在寄存器中缓存非本地变量多长时间?例如,可以代码如
a = length;
b = length;
总是应该优化到
注册int t = length;
a = t ;
b = t;


rs
Hi,

please take a look to this sample code:

class MyClass {
private:
static int length ;
public:
static void setLength(int newLength) ;
void do() ;
}

int MyClass::length ;

void MyClass::setLength(int newLength) {
length=newLength ;
}

void MyClass::do() {
for(int i=0;i<length;i++) {
// some simple floating point additions and multiplications ...
}
}

I expected that the compiler (Microsoft EVC 4 for ARM, full
optimization)
would optimize the for-loop of the method "do" to
register int t=length ;
for(int i=0;i<t;i++) {
...
}
But this does not happen. Instead, the generated machine code
refetches "length" in each iteration of the for-loop. Why?
Does the compiler "fear" that another thread could call "setLength"
while the for-loop runs? "length" is *not* declared volatile.

Is there a formal specification how long a compiler is allowed to cache
non-local variables in registers? For example, can code like
a = length ;
b = length ;
always be expected to be optimized to
register int t=length ;
a=t ;
b=t ;
?

rs




经验法则是缓存任何全局变量局部变量为

轻松优化编译器。如果有一个寄存器,它将为本地变​​量分配
。如果没有可用的话,本地

varialbe很可能会在堆栈上分配,因此大多数b $ b b可能比其全局对应物具有更好的缓存行为。

编译器甚至可以消除局部变量。


我不能说你的情况下缺少优化的原因是什么,

因为你省略了for循环的内容。但我建议

分配一个局部变量,它是对象属性的副本。


HTH,

Tom



rule of thumb is to cache any global variable in a local variable to
ease optimization for the compiler. If a register is available, it will
be allocated for the local variable. If none is available, the local
varialbe will most likely be allocated on stack and therefore will most
likely have a better cache behaviour than its global counterpart. The
compiler might even be capable of eliminating the local variable.

I cannot say what the reason for missing optimization is your case,
because you omitted the contents of the for loop. But I recomend to
allocate a local variable that is the copy of the object attribute.

HTH,
Tom


ro*****@gmx.de 写道:


请看一下这个示例代码:

class MyClass {
private:
static int length;
public:
static void setLength(int newLength);
void do();
}
int MyClass :: length;

void MyClass :: setLength(int newLength){
length = newLength;
}
void MyClass :: do(){
for(int i = 0; i< length; i ++){
//一些简单的浮点加法和乘法...
}

我希望编译器(用于ARM的Microsoft EVC 4,完全优化)
将优化方法的循环do。 to
注册int t = length;
for(int i = 0; i< t; i ++){
...
}
但这不会发生。相反,生成的机器代码重新获得长度。在for循环的每次迭代中。为什么?
编译器是否恐惧 for-loop运行时,另一个线程可以调用setLength
吗? "长度"是*不*声明为volatile。

是否有正式的规范允许编译器在寄存器中缓存非本地变量多长时间?例如,可以代码如
a = length;
b = length;
总是应该优化到
注册int t = length;
a = t ;
b = t;


rs
Hi,

please take a look to this sample code:

class MyClass {
private:
static int length ;
public:
static void setLength(int newLength) ;
void do() ;
}

int MyClass::length ;

void MyClass::setLength(int newLength) {
length=newLength ;
}

void MyClass::do() {
for(int i=0;i<length;i++) {
// some simple floating point additions and multiplications ...
}
}

I expected that the compiler (Microsoft EVC 4 for ARM, full
optimization)
would optimize the for-loop of the method "do" to
register int t=length ;
for(int i=0;i<t;i++) {
...
}
But this does not happen. Instead, the generated machine code
refetches "length" in each iteration of the for-loop. Why?
Does the compiler "fear" that another thread could call "setLength"
while the for-loop runs? "length" is *not* declared volatile.

Is there a formal specification how long a compiler is allowed to cache
non-local variables in registers? For example, can code like
a = length ;
b = length ;
always be expected to be optimized to
register int t=length ;
a=t ;
b=t ;
?

rs




b中没有关于此类优化的正式规范br />
标准。你可能最好在特定于微软的小组中发布

,其中有更多的专家潜伏。几个这样的团体列在

这个FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9


干杯! --M



There is no formal specification for such optimizations in the
Standard. You might be better off posting in Microsoft-specific group
where more of their experts lurk. Several such groups are listed in
this FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Cheers! --M


>在
Standard中没有关于此类优化的正式规范。你可能最好在特定于微软的小组中发帖,其中有更多的专家潜伏。
[...]
Standard. You might be better off posting in Microsoft-specific group
where more of their experts lurk. Several such groups are listed in
[...]




列出了几个这样的团体我知道它可能是特定于MS的东西,但我想知道什么是

clc ++考虑一下。你是否同意我的说法,除非变量

被声明为volatile,否则规格中没有任何东西会阻止编译器进行优化?


rs



I know that it may be a MS-specific thing but I wanted to know what
c.l.c++ thinks about it. Do you agree with me that nothing in the specs
prevents the compiler from doing the optimization unless the variable
is declared volatile?

rs


这篇关于内存访问优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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