<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body smarttemplateinserted="true">
<div id="smartTemplate4-template">Hi,<br>
<br>
<p> that is a great idea<br>
</p>
It especially useful for generics, where each specialization can
create a huge amount of identical methods<br>
<br>
Best,<br>
Benito </div>
<div class="moz-cite-prefix">On 13.10.21 19:33, J. Gareth Moreton
via fpc-devel wrote:<br>
</div>
<blockquote type="cite"
cite="mid:31cf33d7-545c-0c1d-50e4-5d0bf62fc7f8@moreton-family.com">Hi
everyone,
<br>
<br>
So one optimisation that has cropped up a couple of times is
finding ways to merge subroutines that, while containing different
source code, compile into the exact same assembly language. For
example, TStream.WriteData has implementations for numerous input
types, and the compilation of "function TStream.WriteData(const
Buffer: WideChar): NativeInt;", "function TStream.WriteData(const
Buffer: Int16): NativeInt;", "function TStream.WriteData(const
Buffer: UInt16): NativeInt;" all produce the following:
<br>
<br>
leaq -40(%rsp),%rsp
<br>
.seh_stackalloc 40
<br>
.seh_endprologue
<br>
movq %rcx,%rax
<br>
movw %dx,32(%rsp)
<br>
leaq 32(%rsp),%rdx
<br>
movl $2,%r8d
<br>
movq (%rax),%rax
<br>
call *264(%rax)
<br>
movslq %eax,%rax
<br>
nop
<br>
leaq 40(%rsp),%rsp
<br>
ret
<br>
.seh_endproc
<br>
<br>
As you can imagine, having the compiler only keep one copy of the
code and redirect all relevant calls to it will reduce code size
significantly for no speed penalty.
<br>
<br>
One thought I had was to keep a "code hash" for each subroutine
that's computed via digesting the assembly language, although I'm
not quite sure how it should handle labels. If two hashes are
identical, the procedures are checked more closely to determine if
they are actually identical and it wasn't just a collision.
<br>
<br>
I figure this would be a whole-program optimization though due to
inter-unit calls and comparisons and the like.
<br>
<br>
Gareth aka. Kit
<br>
<br>
<br>
</blockquote>
</body>
</html>