From: Michael C. <cor...@gm...> - 2005-10-14 18:19:35
|
I was casually looking through gaim's code (from current CVS) for something that I could fix (I'm a veteran C programmer, but very new to GTK / linux development, and I'm not very familiar with gaim's code). I came upon a piece of code that looked to me like it wasn't written as best as it should= : In file util.c, function gaim_home_dir(), the current function in CVS looks like: const char * gaim_home_dir(void) { #ifndef _WIN32 if(g_get_home_dir()) return g_get_home_dir(); else return NULL; #else return wgaim_data_dir(); #endif } Looking at this code without compiler optimizations, we should get the following behavior (for non-win32 platforms): we call g_get_home_dir() to see if its return value isn't NULL. Then if it is NULL, we go and return NULL under the else. If it isn't NULL, we call the function again, and return what it returned. Correct me if I'm wrong, but it seems we can get the same behavior with greatly simplified code if we just do this: const char * gaim_home_dir(void) { #ifndef _WIN32 return g_get_home_dir(); #else return wgaim_data_dir(); #endif } This should avoid an unnecessary "if", and a 2nd call to g_get_home_dir() i= n the event that the first call succeeded. The result as far as I know should be exactly the same. The program should end up a bit faster and smaller, or possibly the same speed and same size, depending on the compiler and its optimizations. However, after compiling, it seems I must be doing something wrong: I found that this new code increases the executable size of the program by 32 bytes= . This makes me wonder now if whoever wrote the function this way knew that gcc would do this and wrote it this way on purpose - can anyone tell me if and why my correction isn't correct, or explain why gcc was able to make my new function 32 bytes BIGGER? I am no expert in assembly, but I tried examining the assembler diff, and it's terribly long, I think because the function locations changed when the function sizes changed. Please someone, help a new free software developer learn from his mistakes, tell me if my change is good or bad, and why! |