Archived

This topic is now archived and is closed to further replies.

Gladiator

VC++? Really?

Recommended Posts

Okay, I thought VC++ was a good code optimizer until I tested the following. Not only does it waste a lot of memory for no reason, but the assembly code that i got from VC sucks |)![|< because it''s not optimized AT ALL... I''ve left only the ASM code of the test() function since that''s what''s important Here is both C and ASM code:
    
//////////////////// TEST.CPP ////////////////

void test(int mine[3][3])
{
	for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				mine<i>[j] = 0xFF;
}

int main ()
{
	int test_array[3][3];

	test(test_array);

	return 0;
}
//////////////////// TEST.ASM /////////////////


?test@@YAXQAY02H@Z PROC NEAR				; test, COMDAT
; File d:\microsoft visual studio\myprojects\dsaff\sadsaf.cpp
; Line 2
	push	ebp
	mov	ebp, esp
	sub	esp, 72					; 00000048H
	push	ebx
	push	esi
	push	edi
	lea	edi, DWORD PTR [ebp-72]
	mov	ecx, 18					; 00000012H
	mov	eax, -858993460				; ccccccccH; Line 3
	mov	DWORD PTR _i$[ebp], 0
	jmp	SHORT $L218
	rep stosd

$L219:
	mov	eax, DWORD PTR _i$[ebp]
	add	eax, 1
	mov	DWORD PTR _i$[ebp], eax
$L218:
	cmp	DWORD PTR _i$[ebp], 3
	jge	SHORT $L220
; Line 4
	mov	DWORD PTR _j$221[ebp], 0
	jmp	SHORT $L222
$L223:
	mov	ecx, DWORD PTR _j$221[ebp]
	add	ecx, 1
	mov	DWORD PTR _j$221[ebp], ecx
$L222:
	cmp	DWORD PTR _j$221[ebp], 3
	jge	SHORT $L224
; Line 5
	mov	edx, DWORD PTR _i$[ebp]
	imul	edx, 12					; 0000000cH
	mov	eax, DWORD PTR _mine$[ebp]
	add	eax, edx
	mov	ecx, DWORD PTR _j$221[ebp]
	mov	DWORD PTR [eax+ecx*4], 255		; 000000ffH
	jmp	SHORT $L223
$L224:
	jmp	SHORT $L219
$L220:
; Line 6
	pop	edi
	pop	esi
	pop	ebx
	mov	esp, ebp
	pop	ebp
	ret	0
?test@@YAXQAY02H@Z ENDP					; test
    
Let''s see what the code does.... ;Line 2 ... sub esp, 72 ... 72 bytes? what the ....? The above code allocates 72 bytes... not only does allocated uneeded memory (when it can simply pass a pointer to the array [which A LOT better], but it allocates double the amount of memory needed). Let''s see... A 3x3 matrix of int would take 36 bytes... 3x3=9x4=36... 3x3 being the dimensions of the array, which is 9 elements, and an int is 4 bytes, that makes it 36 bytes.. why does it allocate 72 bytes? same as you, i have no idea... and that''s the first problem really.. memory.. here comes the speed part (optimization) part... i have the Win32 Release option on, and have compiled it with Optimize for Speed... ; Line 3 ... add eax, 1 ... This should''ve been inc eax ... also, the whoop could have used a do/while type of structure when the value is supposed to go down to zero (meaning the start value in the for loop is zero). so.. mov ecx, 3 repeat: ... do whatever dec ecx jns short repeat when i tried to use a register for the loop index values, it didnt do anything.. it still used memory and im sure it didnt even try to use the regsiters.. C only looks good from the outside but nobody knows what it does when it gets in the low level stuff... when using the above code it''s not going to use all those comparisons and branches like in: ... cmp DWORD PTR _j$221[ebp], 3 jge SHORT $L224 ... and plus it will make the source code smaller in size (well a couple bytes )... AND take a few cycles less.. (in 3D engines a single cycle matters).. ... ; Line 5 mov edx, DWORD PTR _i$[ebp] imul edx, 12 ... first of all, they should keep that "i" index value in a register all the time... that''s very important!! second... instead of using IMUL they can shift the value to left just like you did in your VGA PutPixel routine... so: "i*12" will be the same as "(i<<3)+(i<<2)"... also a lookup table might be calculated prior to the program that has all the indirect addressing values which would make it faster since you dont have to calculated it for each item every time... See? these are just a few things that a programmer can do that the compiler can''t.. the code will be much faster and much more cleaner... just my thoughts.. any comments are welcome... take care! ------------------------------- That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Dude, I''ll be honest, I''m not good at low-level stuff at all, and I didn''t even bother reading all your assembler explanations, I''ll trust ur word.

I don''t have time for this, but if u went far enough to analyze all this stuff, I guess you do. Why don''t u compile this same piece of code in VC, Borland, DJPP(or whatever it is) and in VectorC. Then look at the assembly code, and see what you can figure out. If you have a high performance timer, you could also test which code runs faster. Also, there are functions(they only work on NT) that measure exactly how many time slices the system gave to the thread.

I would really be interested in seeing results. I don''t know if u have time to do this and to post results, but if u do, I would be more then interested.

Share this post


Link to post
Share on other sites
Well, I don''t have the time, since at the time I decided to inform you about the spaghetti code that C++ generates, I was just testing about the pointer passing/value passing type of thing... And I noticed all the garbage that C++ throws in to make it easy on itself. It probably will notice to change i = i * 2 (where i is an integer) with i = i << 1 or by any 2^n values possible, but the thing is that it cant connect two expressions like (i<<3)+(i<<2) to make up for i*12 which would be slower than the shifting bit... Of course this is not very important if your game is not THAT big, but if it starts getting complicated, every single line of code MATTERS to the performance of your game (or any other type of application that needs to execute FAST enough to be user-interactive). If I have time I might try to get some more info and test a few cases to see if VC is really as smart as it claims to be, or is it just all rumors...

Anyways.. if you have any comments/thoughts no matter what they are just write them down... my ears are wide open and are listening .. im out

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Hi all.

I can''t agree with you more Gladiator. Also, in some other thread I was pointing out that VC++ 6(SP 3) generated the same assenly code for this:

A = A + 1;

as for this:

A += 1;

And someone (his nick escapes my memory) recomended me to use Borland compiler instead. But even then I don''t think a compiler can always optimize better than a good assembly language programmer.

Topgoro

Share this post


Link to post
Share on other sites
The code is correct. I mean if you pass an array to a function that gets array in the parameters, it will generate exactly the same thing in ASM. Compilers don't think.
The " add eax,1 " line, is pairing with the "mov" right before it. And as it is a immediate value, it takes only one cycle.
Your code doesn't change anything on the array that you passed to it. The function should be:
void test(int** mine)
{
...
}

And don't worry so much about this low level thing. I can say for sure that Carmack, didn't write a single line in assembly for Quake 3.

Edited by - blazter on July 24, 2000 5:46:33 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Blazter


And don''t worry so much about this low level thing. I can say for sure that Carmack, didn''t write a single line in assembly for Quake 3.




How do you know this? The engine source hasn''t been released...

Share this post


Link to post
Share on other sites
You''re probably using Standard Edition, right? It does good for only $50! If you want "optimized" code from VC++, then fork out the $300(?) upgrade fee for Professional Edition.

Plus, you must have a LOT of extra time on your hands to go through and do all of this, I wish I had that much spare time. Oh well. Peace Out.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
TITLE D:\VCProject\Learn\main.cpp
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC ''CODE''
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC ''DATA''
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC ''CONST''
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC ''BSS''
_BSS ENDS
_TLS SEGMENT DWORD USE32 PUBLIC ''TLS''
_TLS ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
PUBLIC ?test@@YAXQAY02H@Z ; test
_TEXT SEGMENT
_mine$ = 8
_i$ = -4
_j$171 = -8
?test@@YAXQAY02H@Z PROC NEAR ; test
; File D:\VCProject\Learn\main.cpp
; Line 3
push ebp
mov ebp, esp
sub esp, 8
; Line 4
mov DWORD PTR _i$[ebp], 0
jmp SHORT $L168
$L169:
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
$L168:
cmp DWORD PTR _i$[ebp], 3
jge SHORT $L170
; Line 5
mov DWORD PTR _j$171[ebp], 0
jmp SHORT $L172
$L173:
mov ecx, DWORD PTR _j$171[ebp]
add ecx, 1
mov DWORD PTR _j$171[ebp], ecx
$L172:
cmp DWORD PTR _j$171[ebp], 3
jge SHORT $L174
; Line 6
mov edx, DWORD PTR _i$[ebp]
imul edx, 12 ; 0000000cH
mov eax, DWORD PTR _mine$[ebp]
add eax, edx
mov ecx, DWORD PTR _j$171[ebp]
mov DWORD PTR [eax+ecx*4], 255 ; 000000ffH
jmp SHORT $L173
$L174:
jmp SHORT $L169
$L170:
; Line 7
mov esp, ebp
pop ebp
ret 0
?test@@YAXQAY02H@Z ENDP ; test
_TEXT ENDS
PUBLIC _main
_TEXT SEGMENT
_test_array$ = -36
_main PROC NEAR
; Line 10
push ebp
mov ebp, esp
sub esp, 36 ; 00000024H
; Line 13
lea eax, DWORD PTR _test_array$[ebp]
push eax
call ?test@@YAXQAY02H@Z ; test
add esp, 4
; Line 16
xor eax, eax
; Line 17
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END

Debug version on VC++ 5.0 enterprise!!!! You did something way wrong buddy!

Share this post


Link to post
Share on other sites
So let me get this straight: the complaint from the posters here is:
"Dude, Microsoft''s compiler doesn''t optimize code when you turn off optimization, so it sucks!"??

[getting out shotgun]

Debug = no optimization. You know that code that fills the array with 0xcccccccc?? That''s the debug code--it goes in and intentionally fills uninitialized memory with 0xcccccccc and 0xcdcdcdcd (one for pointers & one for non-pointers, if I remember correctly) so that while you''re debugging you can tell if you''re referencing uninitialized data. This is going to cost more instructions .

If you REALLY want to see what your program compiles to, do this:
1) Start a new project
2) Switch to Release build
3) Go to Project->Settings
4) In the C/C++ tab, in the category Listing Files, change Listing file type to "Assembly with Source Code".
5) Recompile
6) Open the "program.asm" file in your Release directory, where program is your project name.

Be prepared: there will be several lines of source code accounted for by seemingly fewer lines of assembly. This is called code optimization. However, if you see at least one line of assembly for every line of source code, you''re looking in the Debug directory you daft fool.

Here''s what you SHOULD get:
    
TITLE C:\projects\test\test.cpp
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC ''CODE''
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC ''DATA''
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC ''CONST''
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC ''BSS''
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 ''DEBSYM''
$$SYMBOLS ENDS
$$TYPES SEGMENT BYTE USE32 ''DEBTYP''
$$TYPES ENDS
_TLS SEGMENT DWORD USE32 PUBLIC ''TLS''
_TLS ENDS
; COMDAT ?test@@YAXQAY02H@Z
_TEXT SEGMENT PARA USE32 PUBLIC ''CODE''
_TEXT ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC ''CODE''
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
PUBLIC ?test@@YAXQAY02H@Z ; test
; COMDAT ?test@@YAXQAY02H@Z
_TEXT SEGMENT
_mine$ = 8
?test@@YAXQAY02H@Z PROC NEAR ; test, COMDAT

; 3 : {

push edi

; 4 : for (int i = 0; i < 3; i++)
; 5 : for (int j = 0; j < 3; j++)
; 6 : mine<i>[j] = 0xFF;

mov edi, DWORD PTR _mine$[esp]
mov ecx, 9
mov eax, 255 ; 000000ffH
rep stosd
pop edi

; 7 : }

ret 0
?test@@YAXQAY02H@Z ENDP ; test
_TEXT ENDS
PUBLIC _main
; COMDAT _main
_TEXT SEGMENT
_test_array$ = -36
_main PROC NEAR ; COMDAT

; 10 : {

sub esp, 36 ; 00000024H

; 11 : int test_array[3][3];
; 12 : test(test_array);

lea eax, DWORD PTR _test_array$[esp+36]
push eax
call ?test@@YAXQAY02H@Z ; test

; 13 : return 0;

xor eax, eax

; 14 : }

add esp, 40 ; 00000028H
ret 0
_main ENDP
_TEXT ENDS
END

Share this post


Link to post
Share on other sites
Qoy, I read a Carmack interview, and he said that Quake 3 has only some small ASM routines from Quake 1. And if you look to the Quake 1 source code you''ll see that the majority of the ASM code is for software rendering. Since Quake 3 doesn''t use SW, there''s almost no ASM in it.

Share this post


Link to post
Share on other sites
Stoffel, You were right... I put it on Release Mode and it had switched it back to debug by itself.. when i went to check it a second time it was on debug, and since ive never ever worked with VC's ASM output, I didn't know that THAT was an output compiled in debug mode... anyways.. thanks for letting me know how to get the optimized (REAL) ASM code...

quote:

you daft fool



let's not get to name calling.. aight? for one no body's perfect... and when you know something that I DONT KNOW then you shouldn't act like a cocky fool just 'cuz of that... just think about your behavior and the way you're acting...

If anyone has any other comments ill be more than glad to hear them... just dont start with me on name calling...

At least somebody learned something knew today... heh.. that's what it's all about.. making mistakes and learning from them all the time...

im out.....

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Edited by - Gladiator on July 25, 2000 10:04:15 AM

Share this post


Link to post
Share on other sites
I''m glad to see this got cleared up quite nicely

The Visual Studio compiler is pretty good at straightforward optimisation - you shouldn''t worry about it too much.


Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.

Share this post


Link to post
Share on other sites
Alright, I''m sorry about the name calling. I get a little peeved when people go off half-cocked about things. It wasn''t so much the original post that bothered me, as it was the other people joining in and saying, "Yeah, Microsoft sucks, yeah!"

I feel that lately, in this forum, there''s just a lot of incorrect information going around. It''s really starting to get on my nerves, but you''re right--my tone didn''t help much. I''ll try to be more positive next time.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


Frankly, I would prefer that posts like the original and some of the parrot posts that followed, got burned at the stake!

We all could use a good burn once and a while...make us think before posting ignorant drivel (much like this post ..! )



Share this post


Link to post
Share on other sites
quote:

"Yeah, Microsoft sucks, yeah!"


in fact it does... but that''s not why i posted this question... and still, many people have said it and i will repeat it again... you cant replace the hand optimized assembly with the compiler''s WORK OF ART (yeah rrrriiiighhhtt)... meaning the post is not ignorant and still serves it''s purpose... i made a mistake by not getting the code, but that code can be optimized EVEN MORE, so stop complaining about me posting such a post and get a life...

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
quote:

Frankly, I would prefer that posts like the original and some of the parrot posts that followed, got burned at the stake!



Franly, I would prefer that Anonymous Posters were disabled, so they can''t hide when they post such stupid comments! I don''t know why people are afraid to reveal their identity when they say what they really THINK about someone else''s opinion, but don''t hide have the usual opinions as everyone else. That has been bothering me quite a bit. If I want to say something I WILL say it, no matter what everyone thinks about it. It''s MY opinion so you don''t have to agree with it.

Plus, this forum is for learning reasons and MOST of the info is partialy or completely inaccurate, unless it comes out of Witchlord, or MadKeithV''s mouths. Then I know it''s ABSOLUTELY correct, otherwise I''m most likely to disagree with some people who think they GOT the FACTS!!! The point is, if you don''t agree with what I say, either flame me, and I''ll flame you back, or just ignore what I''m saying. Or if you explain what I''ve messed up nicely to me, I''m willing to listen and learn!! Otherwise, just forget about it... That''s all I wanted to say... Now it''s your turn people... Because I''m sure some people will disagree with me... Have a go on this thread (and don''t hide your identity no matter what you say...)

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Gladiator, I think I''m finally starting to understand you...
and I must say it''s pleasing.

I understand your frustration, but try to remain patient with people that flame. I have to do the same, and it''s not always easy.
I think it was a pretty big investment of effort on your part to disassemble the VC++ code and pick it apart - you didn''t know about debug vs. release so it kindof blew up in your face for a second, but you do know now.
I also know that from now on, you WILL know about debug vs. release, and if you ever discuss VC++''s generated assembly again, you''ll have a very accurate idea of what happens.

To everyone in this thread:
Nobody is perfect, there''s no need to be inconsiderate about it. I''ve made mistakes, and so will you. Stay civil about it, and learn from them. Don''t bash others because they didn''t know something you think is obvious, most of the time it''s not as obvious as you think.


Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.

Share this post


Link to post
Share on other sites
Hi there,

i am coding for almost 20 years now and i am still learning day by day. The first and important thing i make clear in every kick off meeting of a new project is, that all team members must respect the other ones. The art of project management is not to bash people to the ground, but to motivate them and get the best out of them, make them fly. People who start bashing on others are not very welcome in the industry, even if they are as inteligent as Einstein and Zuse together, because what we need are team players and not solofighters.

My experience with the different available compilers are that Watcom C++ does the best optimizations because it does not only use the "peephole" optimization approach. That means it does not only look at the current command but also analyzes the code around the command to make decisions. The compiler gives also a lot of switches to configure the way of optimizations. (unroll loops etc.)

What i would really want to know is, what compilers are currently used by id, blizzard and westwood ?

cu

Peter






Share this post


Link to post
Share on other sites
Gamasutra features "postmortems" from game designers. You can find it here:
http://www.gamasutra.com/features/index_postmortems.htm
You can generally find the tools used for development on the last page of each article.

C&C Tiberian Sun (since you asked about Westwood) was done using MSVC++.

I didn''t notice any other postmortems from companies you mentioned. But at a glance:

Myth was developed with Codewarrior (they''re Mac people), but the PC port was done with MSVC++ (though only done in C).

Thief: The Dark Project: MSVC++, Watcom C++

System Shock 2: MSVC++

Age of Empires 2: "Visual C++" (MS?)

Unreal Tournament: "Microsoft Visual Studio"

Drakan: Order of the Flame: MSVC++ 5.0 and 6.0

Those are just the ones I looked at. Maybe you''ll find some if you look that don''t use Microsoft.

(but remember, Microsoft sucks)

Share this post


Link to post
Share on other sites
READ THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

If you scrolled down to read this post, you should know, that VC++ does not optimize greatly until you tell it to optimize. There are certain optimization flags that VC++ gives you that you must specify, this can be done in the IDE if you do it under the project settings. The flag is something like /Ox where x is a various option (ie/ you can optimize and it specifically optimizes for a certain CPU), another reason it is slow is because without this special optimization, it is Compiling it for Debugging, you must also turn that off in the Project options. Trust me, VC++ will make your code optimized if you compile it right.

- Tom

Share this post


Link to post
Share on other sites
MadKeithV, thanks for the support!

quote:

It wasn''t so much the original post that bothered me , as it was the other people joining in and saying, "Yeah, Microsoft sucks, yeah! "



quote:

(but remember, Microsoft sucks)



Stoffel, I''m starting to have some disbelief in you!

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Haha Gladiator. You're gonna try and pick something apart then cry like a bitch when someone starts criticizing you. Keep it up, I need a good laugh every now and then.

Either think before you open your mouth are be prepared to endure what follows.

Oh yeah, 'Microsoft sucks'. Am I cool now or what?

Don't get me wrong, feel free to comment and question whatever you want. Next time don't act so surprised if you get a similar response.

sarcasm
Long live Michael Abrash and the X-Box
/sarcasm



-BacksideSnap-

Edited by - -BacksideSnap- on July 26, 2000 2:46:36 PM

Share this post


Link to post
Share on other sites
quote:

You''re gonna try and pick something apart then cry like a bitch when someone starts criticizing you. Keep it up, I need a good laugh every now and then.



First of all i knew he was kidding, and so was I... forgot to put the smiley face tho... anyways.. let''s keep the discussion goin...

nobody critisized me, they just CORRECTED me, and there''s a BIG difference between the two...

and yes... i had a good laugh once i read your thoughtless post...

quote:

Either think before you open your mouth are be prepared to endure what follows.


i think after im done with what i have to say... before that i dont care what i say, as long as it comes from the heart... and i mean what i say... it''s not only me.. whoever opens his/her mouth they should expect some kind of flame especially when they know that they''re (possibly) wrong... i didnt post the question to become a flamewar, but i didnt know that the compiler had switched to Debug mode, after i had changed the option to release mode... havent used VC all that much so i dont know all the detailed options, etc, etc.... blah blah...


quote:

Oh yeah, ''Microsoft sucks''. Am I cool now or what?


no.. you''re still that shitty ass bitch that you were before you used sarcastic comments... and to tell you the truth i dont give a shit about your likes and dislikes... it''s your opinion and im sure you have something to back it up with.. (you dont? not a surprise at all...)


quote:

Don''t get me wrong, feel free to comment and question whatever you want. Next time don''t act so surprised if you get a similar response.


pshhh... you think you''re all that, dont you? of course im free to comment.. i POSTED this question so i can comment... maybe your comments are unwanted.... and i said maybe, but since you''re fun to play around with, keep ''em comin...

quote:

sarcasm
Long live Michael Abrash and the X-Box
/sarcasm


now he thinks he''s a comedian from hollywood.. but you''ve got a LONG LONG way to go... if it wasnt for Michael Abrash, John Carmack, and the like, you would''ve been entering those 0''s and 1''s for the rest of your life.. but your mind is too confused to realize that...


ive said enough i guess... ive had my part laughing WITH you... now it''s time for me to go... and get some rest before you throw in some other dumb comment...

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites