20101005 - MSVC Optimizing Static Constant Data Copy?


Yes. All extra copies factored out.

#include <stdio.h>

typedef struct {
int a;
int b;
int c;
int d;
} DataA;

typedef struct {
int a;
int d;
} DataB;

static const DataA dataA = { 0, 1, 2, 3 };

static __forceinline void DataCopy(
DataB* __restrict const dataB, const DataA* __restrict const dataA) {
dataB->a = dataA->a;
dataB->d = dataA->d; }

void main(void) {
DataB dataB;
DataCopy(&dataB, &dataA);
printf("dataB.d = %d\n", dataB.d); }


Trimmed part of /FAs assembly output,

PUBLIC _main
EXTRN _printf:PROC
; Function compile flags: /Ogtpy
_TEXT SEGMENT
_main PROC

; 56 : DataB dataB;
; 57 : DataCopy(&dataB, &dataA);
; 58 : printf("dataB.d = %d\n", dataB.d); }

push 3
push OFFSET $SG3859
call _printf
add esp, 8
xor eax, eax
ret 0
_main ENDP