본문 바로가기

Programming/C Programming

The cost of inline functions

The cost of inline functions

The kernel makes heavy use of inline functions. In many cases, inline expansion of functions is necessary; some of these functions employ various sorts of assembly language trickery that must be part of the calling function. In many other cases, though, inline functions are used as a way of improving performance. The thinking is that, by eliminating the overhead of performing actual function calls, inline functions can make things go faster.
inline function의 사용은 kernel 을 무겁게 만든다. 많은 경우에 함수의 inline 확장은 필요하다.; 이 함수들중 몇몇은 함수의 부분으로서 호출되기 위해서 다양한 유형의 어셈블 언어의 트릭(?)을 사용한다. 비록 다른 많은 경우에서 inline 함수는 성능을 향상시키는 방향으로 사용된다. inline 함수는 실제 함수 호출의 overhead 를 제거하여서, 속도를 향상한다고 생각된다. 


The truth turns out not to be so simple. Consider, for example, this patch from Stephen Hemminger which removes the inline attribute from a set of functions for dealing with socket buffers ("SKBs", the structure used to represent network packets inside the kernel). Stephen ran some benchmarks after applying his patch; those benchmarks ran 3% faster than they did with the functions being expanded inline.
진실은 단순하지 않다. 예를 들어 Stephen Hemminger 이 제출한 패치를 고려해 보자. 이 패치는 소켓 버퍼를 다루기 위한 함수들의 set 으로 부터 inline 속성을 제거한다.("SKBs"는 커널내의 네트웍 패킷을 대표하는 구조체임) Stephen 은 그의 패치를 적용후에 몇몇 밴치마트를 동려서 확장된 inline 함수보다 3%가량 더 빠름을 밝혔다.


The problem with inline functions is that they replicate the function body every time they are called. Each use of an inline function thus makes the kernel executable bigger. A bigger executable means more cache misses, and that slows things down. The SKB functions are called in many places all over the networking code. Each one of those calls creates a new copy of the function; Denis Vlasenko recently
discovered that many of them expand to over 100 bytes of code. The result is that, while many places in the kernel are calling the same function, each one is working with its own copy. And each copy takes space in the processor instruction cache. That cache usage hurts; each cache miss costs more than a function call.
inline 함수는 문제는 그들이 호출할 때마다 함수의 body를 복제하는 것이다. 따라서 inline 함수의 사용은 커널 실행파일을 크게 만든다. 큰 실행파일은 cache miss 가 더 잘 발생하고 느려짐을 뜻한다. SKB 함수는 networking code 곳곳에서 호출된다. 각각의 호출은 함수의 새로운 복사본을 생성한다. Denis Vlasenko는 최근에 코드의 100 byte 가 넘는 부분이 확장되었음을 발견하였다. 커널이 동일한 함수를 여러곳에서 호출할때 각각은 자신의 복사본을 가지고 동작한다. 그리고 각각의 복사본은 processor 의 i-cache 안에 공간을 차지한다. 함수를 호출 할때보다 더 cache miss 비용이 든게 되어 cache 의 사용성이 나빠진다.



Thus, the kernel hackers are taking a harder look at inline function declarations than they used to. An inline function may seem like it should be faster, but that is not necessarily the case. The notion of a "time/space tradeoff" which is taught in many computer science classes turns out, often, to not hold in the real world. Many times, smaller is also faster.

따라서, 커널 해커들은 inline 함수 선언을 살펴보고 있다. inline function 은 빠르게 보일 수도 있으나, 경우에 따라 필요 없다. 많은 컴퓨터공학과목에서 다루어 지는"시간과 공간의 tradeoff" 개념이 실세계 안에서 결정되지 못하고 있다. 대부분 작은것이 더 빠르다.

'Programming > C Programming' 카테고리의 다른 글

#if 로 define 을 하면 좋은점.  (0) 2015.04.20
volatile keyword  (0) 2013.07.17
배열 초기화 선언  (1) 2010.10.29
c 와 c++ 의 const 내용정리  (0) 2010.10.08
c 와 c++ 의 static 내용정리  (0) 2010.10.08