On Friday, 1 December 2017 at 03:23:23 UTC, Walter Bright 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
-------------------------------------------
26 bytes of inserted Bloaty McBloatface code and 15 bytes of
data. My proposal:
_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 $+01h
0011: hlt // look ma,
1 byte!
0012: mov RSP,RBP
0015: pop RBP
0016: ret
1 byte of inserted code, and the data strings are gone as well.
I see you are concerned with the total size, which I understand.
I think we misunderstood each other.
What I meant in terms of icache pollution is with the 'cold' is
instead of generating:
if(!cond)
_d_assert(__FILE__, __LINE__,message);
//rest of code
it should actually generate,
if (!cond)
goto failed;
//rest of code
failed:
_d_assert(__FILE__, __LINE__,message);//call is cold & out
of line. no icache pollution
I'm not sure that it does that given the triviality of the
example, but it looks like it doesn't.