__const__ 속성

커널에서 는 __attribute__((__const__))를 __attribute_const__ 로 define 해서 사용합니다.
const 속성은 gcc 매뉴얼에 설명이 나와 있습니다.

gnu gcc 온라인 매뉴얼

그리고, 아래의 url 을 참고해 보시면 또한 중요한 문구가 나옵니다.

http://nshipster.com/__attribute__

pure and const are both attributes that invoke a functional programming paradigm in order to allow for significant performance optimizations. const can be thought as a stricter form of pure since it doesn't depend on global values or pointers.
For example, because the result of a function declared const does not depend on anything other than the arguments passed in, the result of the function can cache that result and return any time the function is called with that same combination of arguments. (i.e. we know that the square of a number is constant, so we only need to compute it once).

const 이외에 pure 라는 것도 있는데, 두가지 속성 모두 결국은 성능 최적화를 위한 것입니다.
즉, const 를 함수에 지정해두면 그 함수의 리턴값은 다른 global 변수 등을 참조하지 않고 함수의 인자만으로 결과가 나오는 함수이기 때문에, 함수의 결과 자체를 캐싱할 수 있고, 만약 같은 인자를 이용하여 나중에 함수가 다시 호출되면 캐싱된 결과만을 던져주면 된다는 것입니다.
성능이 무지하게 빠르겠죠.
즉, 컴파일러에게 이 함수의 결과는 인자값만으로 결정된다는 것을 알려주는 것입니다.
그러면, 컴파일러는 적절한 성능 최적화 코드를 만들어 내겠지요.

그래서, const 를 사용할 때는 포인터나 global memory 에 대한 참조를 사용하지 말아야 합니다.
그리고, 리턴 형이 void 인 것도 마찬가지이구요.

아래와 같은 코드에서 계속 square 함수를 호출할 필요가 있을까요 ?
square 함수의 결과는 항상 같을텐데 말이죠.  이런 것들을 최적화합니다.

extern int square(int n) __attribute__((const)); 
... 
for (i = 0; i < 100; i++ ) 
{ 
    total += square(5) + i;
}

http://www.unixwiz.net/techtips/gnu-c-attributes.html#const

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x