Thoughts about D
Iain Buclaw
ibuclaw at gdcproject.org
Sat Dec 2 12:35:52 UTC 2017
On 1 December 2017 at 04:23, Walter Bright via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On 11/30/2017 3:51 PM, Nicholas Wilson wrote:
>>
>> On Thursday, 30 November 2017 at 18:18:41 UTC, Jonathan M Davis wrote:
>>>
>>> But I have a hard time believing that the cost of assertions relates to
>>> constructing an AssertError unless the compiler is inlining a bunch of stuff
>>> at the assertion site. If that's what's happening, then it would increase
>>> the code size around assertions and potentially affect performance.
>>>
>>> - Jonathan M Davis
>>
>>
>> Indeed, if DMD is not marking the conditional call to _d_assert (or
>> whatever it is) 'cold' and the call itself `pragma(inline, false)` then it
>> needs to be changed to do so.
>
>
> Instead of speculation, let's look at what actually happens:
>
> ---------------------------------
> void test(int i) {
> assert(i, "message");
> }
> ---------------------------------
> dmd -c -m64 -O test
> obj2asm -x test.obj
> ---------------------------------
>
> __a6_746573742e64:
> db 074h,065h,073h,074h,02eh,064h,000h ;test.d.
> __a7_6d657373616765:
> db 06dh,065h,073h,073h,061h,067h,065h,000h ;message.
>
> _D4test4testFiZv:
> 0000: push RBP
> 0001: mov RBP,RSP
> 0004: sub RSP,040h
> 0008: mov 010h[RBP],ECX
> 000b: cmp dword ptr 010h[RBP],0
> 000f: jne $+3Ah
> --- start of inserted assert failure code ---
> 0011: mov R8D,5 // line number
> 0017: lea RAX,FLAT:_BSS[00h][RIP]
> 001e: mov -018h[RBP],RAX // filename.ptr
> 0022: mov qword ptr -020h[RBP],6 // filename.length
> 002a: lea RDX,-020h[RBP] // &filename[]
> 002e: lea RCX,FLAT:_BSS[00h][RIP]
> 0035: mov -8[RBP],RCX // msg.ptr
> 0039: mov qword ptr -010h[RBP],7 // msg.length
> 0041: lea RCX,-010h[RBP] // &msg[]
> 0045: call L0
> --- end of inserted assert failure code ---
> 004a: mov RSP,RBP
> 004d: pop RBP
> 004e: ret
> -------------------------------------------
>
Wouldn't it be more optimal if dmd instead emitted the following?
---------------------------------
__a6_746573742e64:
db 074h,065h,073h,074h,02eh,064h,000h ;test.d.
__a7_6d657373616765:
db 06dh,065h,073h,073h,061h,067h,065h,000h ;message.
_D4test4testFiZv:
0000: push RBP
0001: mov RBP,RSP
0004: sub RSP,040h
0008: mov 010h[RBP],ECX
000b: cmp dword ptr 010h[RBP],0
000f: je $+3Ah ; <--- Using `je` instead of `jne`
0011: mov RSP,RBP
0014: pop RBP
0015: ret
--- start of inserted assert failure code ---
...
> --- end of inserted assert failure code ---
-------------------------------------------
More information about the Digitalmars-d
mailing list