Board index » cppbuilder » Re: inlining functions
Chris Uzdavinis (TeamB)
![]() CBuilder Developer |
Re: inlining functions2008-05-20 05:39:27 AM cppbuilder58 "Mike King" < XXXX@XXXXX.COM >writes: Quoteclass Test { re-applied anyway. Quote>Also, in the past some code I'm aware of was found to be more efficiently beyond that stage years ago. In fact, it was probably like 10 years ago when I heard from a friend who looked into generated code that the pointer/indexing code was 100% identical. (But he was using MS's compiler, and I've never been motivated to see how other compilers do it.) But as optimizers go, I'm pretty impressed with the one in g++ 4.x. I discovered that code like this: std::string s; //... s += "abcdefg"; has a pretty awesome optimization. I expected the operator += to do a strlen on the string literal, then append n bytes. I was astounded to see that the generated code was equivalent to this: s.append("abcdefg", 7); Apparently, 1) operator+=(char const * str) calls append(str) 2) append(char const * str) figures the length, then forwards it on: size_t len = strlen(str); append(str, len); 3) append(str,len) implements the real work. The inliner was smart enough to trace through the code and know that operator+= called append() with a string literal, and then the call to strlen on the string literal was recognized as something that could be done at compile time, and that value was inlined, and then the call to append(str, 7) was inlined as well. I had no idea that compilers would trace string literals through char* parameter arguments. (In g++ 3.x series, it didn't.) Anyway, that was my most recent moment of awe. :) -- Chris (TeamB); |