Recent

Author Topic: [CLOSED] Dynamic vs virtual  (Read 457 times)

julkas

  • Hero Member
  • *****
  • Posts: 604
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
[CLOSED] Dynamic vs virtual
« on: May 29, 2020, 08:52:00 am »
How dynamic methods implemented in Free Pascal / Delphi?
Pros and cons?
https://www.freepascal.org/docs-html/ref/refsu27.html -
Quote
Remark: The keyword ’virtual’ can be replaced with the ’dynamic’ keyword: dynamic methods behave the same as virtual methods. Unlike in Delphi, in FPC the implementation of dynamic methods is equal to the implementation of virtual methods.
« Last Edit: May 30, 2020, 08:13:11 am by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 1742
  • Compiler Developer
Re: Dynamic vs virtual
« Reply #1 on: May 29, 2020, 09:04:48 am »
In FPC there is no difference between virtual and dynamic. In Delphi according to this blog post their main benefit was in 16-bit times with classes that only overrode a few methods of the parent class.

In essence: simply use virtual and forget about dynamic.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 508
Re: Dynamic vs virtual
« Reply #2 on: May 29, 2020, 10:38:56 am »
Dunno about FPC, but in Delphi dynamic methods work the same way, message handlers work. It's made for memory economy, but it's slower. For example components may have several hundreds of event handlers. If virtual methods would be used, every descendant would have to implement whole VMT for all that methods. Even if it would override just one method. And while nobody cares about memory consumption so much now, back in old days it was critical. That's why key-value table was used for message handlers instead. If class overrides just one method of 1000, it would have just one entry in it's DMT table.

Example:
1) Virtual:
Code: Pascal  [Select][+][-]
  1.   procedure CallVirtualMethod(C:TClass;ID:Integer;args);
  2.   begin
  3.     C.VMT[ID](args);
  4.   end;
  5.  
2) Dynamic:
Code: Pascal  [Select][+][-]
  1.   procedure CallDynamicMethod(C:TClass;ID:Integer;args);
  2.     var I:Integer;
  3.   begin
  4.     //Method should exist in at least one parent class! Otherwise it's compiler error!
  5.     I := C.DMT.IndexOf(ID);
  6.     if I >= 0 then C.DMT.Methods[I](args) else CallDynamicMethod(C.Parent, ID, args);
  7.   end;
  8.  
« Last Edit: May 29, 2020, 10:57:35 am by Mr.Madguy »
DynamicData 3.0 is released!
Since now development is frozen - only optimization passes will be made at some point.
Lack of multiple inheritance turns it into abomination.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8491
  • FPC developer.
Re: Dynamic vs virtual
« Reply #3 on: May 29, 2020, 05:47:40 pm »
If I read the blogpost it sounds they didn't care as much as for memory in general, but in the datasegment.

TP was notorious for only allowing one (static) datasegment, they never implemented the multi segmented options.

So one could argue it was a hack to not having to change compiler architecture too much in what they knew to be their last 16-bit compiler series.

And of course multi segment datasegment models carry an overhead (since any global variable must be loaded as far pointer, iow with a segment), so even introducing then would cause whining that programs were slower.

I experimented with such options in Topspeed compilers on a Cyrix 686 p166+, and it was generally a few percent.

julkas

  • Hero Member
  • *****
  • Posts: 604
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
Re: Dynamic vs virtual
« Reply #4 on: May 30, 2020, 08:05:09 am »
So dynamic is relict.
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 1742
  • Compiler Developer
Re: Dynamic vs virtual
« Reply #5 on: May 30, 2020, 10:52:43 am »

 

TinyPortal © 2005-2018