Page 1 of 1
Structure Optimization
Posted: Sat Nov 20, 2004 5:48 pm
by Moonshine
I was just wondering if there are any advantages to static arrays or standard vars in a structure. For example:
Code: Select all
Structure Vector
x.f
y.f
z.f
EndStructure
Structure Vector
Position.f[3]
EndStructure
Is there any differnce/advantage to either of the methods, in terms of speed or memory management or anything like that. Just something that Id been wondering for a while.
Cheers

Posted: Tue Nov 30, 2004 11:29 pm
by Kale
I wouldn't of thought so, because both ways places the data in exactly the same configuration, they are all concurrent positions in memory. A static array just provides another (more convienient?) way to set/get the data.

I would personally go with the first more readable way of doing things. i.e.:
Code: Select all
Structure VECTOR
x.f
y.f
z.f
EndStructure
Posted: Wed Dec 01, 2004 12:14 am
by tinman
There will be differences in the generated ASM code. Using an array the accesses always seem to be based on "array address + sizeof(type) * offset" whereas with the three separate variables it will only be "address of structure + offset". Whether this makes much difference I don't know, but array accesses certainly have the edge when it comes to ease of batch processing (in terms of writing the code :).
Edit: I just checked, this example show that the same code is generated for the simple case where you use a constant offset is the same, but when using expressions as the loop index is more complex. Of course, you can't use that kind of accesses with three separate variables, so you always get the simpler code.
Code: Select all
; Structure separate
; x.f
; y.f
; z.f
; EndStructure
;
;
; Structure array
; coords.f[3]
; EndStructure
;
;
; aa.separate
; bb.array
;
; aa\x = 23
LEA ebp,[v_aa]
MOV dword [ebp],1102577664
; aa\y = 45
MOV dword [ebp+4],1110704128
; aa\z = 67
MOV dword [ebp+8],1116078080
;
; bb\coords[0] = 12
LEA ebp,[v_bb]
MOV dword [ebp],1094713344
; bb\coords[1] = 23
MOV dword [ebp+4],1102577664
; bb\coords[2] = 34
MOV dword [ebp+8],1107820544
;
; For i=0 To 2
MOV dword [v_i],0
_For1:
MOV eax,2
CMP eax,dword [v_i]
JL _Next2
; bb\coords[i] = 69
LEA ebp,[v_bb]
PUSH ebp
MOV eax,dword [v_i]
SAL eax,2
POP ebp
ADD ebp,eax
MOV dword [ebp],1116340224
; Next
_NextContinue2:
INC dword [v_i]
JMP _For1
_Next2: