Skip to content

Conversation

@CaseyCarter
Copy link
Contributor

Providing a minor runtime and codesize benefit.

Thanks to Ville Voutilainen for the suggestion.

Providing a minor runtime and codesize benefit.
@CaseyCarter CaseyCarter requested a review from a team as a code owner June 4, 2020 00:31
@CaseyCarter CaseyCarter added the performance Must go faster label Jun 4, 2020
@BillyONeal
Copy link
Member

Given that the compiler is moving to initialize all the things does this even have an effect?

@miscco
Copy link
Contributor

miscco commented Jun 4, 2020

Makes sense for me given that it is invalid to access an unassigned optional anyway.

@CaseyCarter
Copy link
Contributor Author

Given that the compiler is moving to initialize all the things does this even have an effect?

Yes, as verified by inspection of the assembly generated for:

std::optional<std::array<int, 1024>> potato() { 
    return {};
}

Before:

; Function compile flags: /Ogtpy
;	COMDAT ?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ
_TEXT	SEGMENT
___$ReturnUdt$ = 8					; size = 4
?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ PROC ; potato, COMDAT
; File C:\Users\Casey\Desktop\repro.cpp
; File c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.28919\include\optional
; Line 167
	push	4100					; 00001004H
	push	0
	push	DWORD PTR ___$ReturnUdt$[esp+4]
	call	_memset                                 ; NB: memset 4100 bytes to 0
; File C:\Users\Casey\Desktop\repro.cpp
; Line 5
	mov	eax, DWORD PTR ___$ReturnUdt$[esp+8]
; File c:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.28919\include\optional
; Line 167
	add	esp, 12					; 0000000cH
; File C:\Users\Casey\Desktop\repro.cpp
; Line 6
	ret	0
?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ ENDP ; potato
_TEXT	ENDS

with just the change to optional's default constructor:

; Function compile flags: /Ogtpy
;	COMDAT ?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ
_TEXT	SEGMENT
___$ReturnUdt$ = 8					; size = 4
?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ PROC ; potato, COMDAT
; File C:\Users\Casey\Desktop\repro.cpp
; File c:\Users\Casey\Source\Repos\STL\stl\inc\optional
; Line 68
	mov	eax, DWORD PTR ___$ReturnUdt$[esp-4]
	mov	BYTE PTR [eax], 0                       ; zero dummy char
	mov	BYTE PTR [eax+4096], 0                  ; set _Has_value to false
; File C:\Users\Casey\Desktop\repro.cpp
; Line 6
	ret	0
?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ ENDP ; potato
_TEXT	ENDS

with the _Nontrivial_dummy_type change as well:

; Function compile flags: /Ogtpy
;	COMDAT ?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ
_TEXT	SEGMENT
___$ReturnUdt$ = 8					; size = 4
?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ PROC ; potato, COMDAT
; File C:\Users\Casey\Desktop\repro.cpp
; File c:\Users\Casey\Source\Repos\STL\stl\inc\optional
; Line 68
	mov	eax, DWORD PTR ___$ReturnUdt$[esp-4]
	mov	BYTE PTR [eax+4096], 0                  ; set _Has_value to false
; File C:\Users\Casey\Desktop\repro.cpp
; Line 6
	ret	0
?potato@@YA?AV?$optional@V?$array@H$0EAA@@std@@@std@@XZ ENDP ; potato
_TEXT	ENDS

@CaseyCarter CaseyCarter self-assigned this Jun 11, 2020
@CaseyCarter CaseyCarter merged commit 2b0aa05 into microsoft:master Jun 11, 2020
@CaseyCarter CaseyCarter deleted the optopt branch June 11, 2020 21:11
@CaseyCarter CaseyCarter removed their assignment Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Must go faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants