Wednesday 1 May 2019

Case sensitivity

Found this piece of code in a message (now deleted) on Stack Overflow, for the language Go:

func addGmp(a, b, c, d, e, f, x, y, X, Y, P *big.Int) (*big.Int, *big.Int) {
    // a,b,c,d,e,f this var (fast in func)
    a.Sub(Y, y)
    b.Sub(X, x)
    ...

The problem is not only that all the parameters have single-letter names that don't say anything about them, the much bigger problem is that there are parameters named x and y as well as X and Y. This is, IMO even worse than something like (in C):

int SomeApi(HWND hwnd, HDC hdc);

Let me reiterate that I am so happy that Delphi (and Pascal) are case-insensitive, and that such abominable parameter names are not possible.

Wednesday 27 February 2019

New in Rio: custom PRNG

I don't know if anyone noticed, but there is something new in the System unit of Delphi 10.3 Rio that is not documented yet.

In Rio, if you want to improve the default pseudo random number generator (PRNG), you can now supply your own 32 bit random function. There is a procedural variable, Random32Proc of type function: UInt32, to which you can assign your own random function. Random will call this function to get a new random value instead of the default function.

The default setting of this variable is System.DefaultRandom32. This is the same as the old Delphi PRNG.

Because your new function may need a different way to set the random seed too, there is something similar for Randomize: RandomizeProc.

Here is the corresponding part of System.pas:

{ random functions }

type
  TRandom32Proc = function: UInt32;
  TRandomizeProc = procedure(NewSeed: UInt64);

function DefaultRandom32: UInt32;
procedure DefaultRandomize(NewSeed: UInt64);

var
  Random32Proc: TRandom32Proc = DefaultRandom32;
  RandomizeProc: TRandomizeProc = DefaultRandomize;

procedure Randomize;

function Random(const ARange: Integer): Integer; overload;
function Random: Extended; overload;

I tried this with the XorShift implementation in my DelphiBigNumbers GitHub repository and it worked as expected.

Much fun with this!

Saturday 2 February 2019

Delphi-Praxis.net

Just a short thanks.

I personally still prefer Usenet-like NNTP newsgroups and a newsreader like XanaNews, Forté Agent, Gravity or even Mozilla Thunderbird over any web-based forums.

But since we don't have those anymore, I would like to thank the fine people at Delphi-Praxis.net. They have done an excellent job replacing the various Google+ as well as the old Embarcadero forums. They have created a well balanced set of groups to discuss things pertaining Delphi and C++Builder.

Sure, there is the new Idera Community, the official replacement for the Embarcadero forums, but I think the Delphi-Praxis.net forums have a, je ne sais quoi, different "feel" to it.

Note that both platforms require you to sign up. I am pretty sure that helps (a little) keeping away the spam and other unwanted input.

If you haven't, go and visit the place. It is a good one. (Also visit the Idera forums, and see which you prefer. I regularly visit both.)

Also recommended:

Tuesday 30 October 2018

Inline declaration of variables

There will be a new interesting feature in the upcoming Delphi 10.3 Rio: inline declared variables. I have permission to blog a little about it.

Marco Cantù already wrote about this new feature. I am not going to explain it, as I think he already did a good job doing that.

I just want to demo one of the main advantages of them: they are block-local, i.e. they are initialized at the point of declaration and are finalized at the end of the begin-end block they were declared in. This means that an interface or other managed variable declared that way is finalized at the end of the block.

To demo this, I made a little talkative interface, which tells us what it is doing, and when it is being created and being destroyed. It also has a name. Here is the code:

type
  ITalking = interface
    procedure Talk;
  end;

  TTalking = class(TInterfacedObject, ITalking)
  private
    FName: string;
  public
    constructor Create(const Name: string);
    procedure Talk;
    destructor Destroy; override;
  end;

implementation

{ TTalking }

constructor TTalking.Create(const Name: string);
begin
  FName := Name;
  Writeln(Format('Creating %s', [Name]));
end;

destructor TTalking.Destroy;
begin
  Writeln(Format('Destroying %s', [FName]));
  inherited;
end;

procedure TTalking.Talk;
begin
  Writeln(Format('Hi, %s talking here!', [FName]));
end;

Demo

And here is the demo:

procedure Test;
begin
  var I1: IInterface := TTalking.Create('One'); // one way to declare an interface
  Writeln('// Before block');
  Writeln;
  begin
    Writeln('// Before first inline var...');
    var I2 := TTalking.Create('Two') as ITalking; // the other way: type inference
    I2.Talk;
    var I3: ITalking := TTalking.Create('Three');
    I3.Talk;
    Writeln('// Do something useful here');
  end;
  Writeln;
  Writeln('// After block');
  var I4: ITalking := TTalking.Create('Four');
  Writeln('// After declaration of Four');
end;

And the output is:

Creating One
// Before block

// Before first inline var...
Creating Two
Hi, Two talking here!
Creating Three
Hi, Three talking here!
// Do something useful here
Destroying Three
Destroying Two

// After block
Creating Four
// After declaration of Four
Destroying Four
Destroying One

As you can see, the two interfaces declared and initialized inside the inner block are destroyed at the end of the same block. The interfaces declared and initialized in the outer block are destroyed at the end of the function.

This creates some interesting opportunities, e.g. for RAII using interfaces, or other auto-finalized structures. RAII doesn't have to be used for smart pointers. It can also be used for many other things, like locks, mutexes, temporary changes of cursors, etc. i.e. anything that is changed temporarily and restored at the end of a certain period. Just declare the RAII object inside the block and the change will be restored at the end of it. Just pass it an anonymous function to tell it what it must do at the end of the block (or in case of an exception).

But more about this later. FWIW, I also like the type inference, or that it can be used for for-loops.

Friday 5 October 2018

Why I like Stack Exchange

Because you can get extremely interesting answers, like the excellent answer to the question "What is Chirped Pulse Amplification, and why is it important enough to warrant a Nobel Prize?" on Physics Stack Exchange:

The 2018 Nobel Prize in Physics has just been announced, with half going to Arthur Ashkin for his work on optical tweezers and half going to Gérard Mourou and Donna Strickland for developing a technique called "Chirped Pulse Amplification". While normally the Wikipedia page is a reasonable place to turn to, in this case it's pretty flat and not particularly informative. So:

  • What is Chirped Pulse Amplification? What is the core of the method that really makes it tick?
  • What pre-existing problems did its introduction solve?
  • What technologies does it enable, and what research fields have become possible because of it?

I am not a physicist and just know the usual school physics, but have no problem understanding the answer. Excellent!

Monday 1 October 2018

The current state of generics in Delphi

To avoid duplication of generated code, the compiler builders of Embarcadero have done a nice job. They introduced new instrinsics like IsManagedType, GetTypeKind and IsConstantType (see this Stackoverflow answer), so they could make a function like the following generate a call to the exact function for the parametric type directly. This means that the code below "runs" completely inside the compiler, even the code in the called InternalAddMRef and similar dispatching routines.

function TList.Add(const Value: T): Integer;
begin
  if IsManagedType(T) then
  begin
    if (SizeOf(T) = SizeOf(Pointer)) and (GetTypeKind(T) <> tkRecord) then
      Result := FListHelper.InternalAddMRef(Value, GetTypeKind(T))
    else if GetTypeKind(T) = TTypeKind.tkVariant then
      Result := FListHelper.InternalAddVariant(Value)
    else
      Result := FListHelper.InternalAddManaged(Value);
  end else
  case SizeOf(T) of
    1: Result := FListHelper.InternalAdd1(Value);
    2: Result := FListHelper.InternalAdd2(Value);
    4: Result := FListHelper.InternalAdd4(Value);
    8: Result := FListHelper.InternalAdd8(Value);
  else
    Result := FListHelper.InternalAddN(Value);
  end;
end;

That, in its turn, means that if you code something like:

var
  List: TList<string>;
begin
  List := TList<string>.Create;
  List.Add('Hello');

then

List.Add('Hello');

is in fact directly compiled as

List.FListHelper.DoAddString('Hello');

i.e. TList.Add does not have any runtime code at all, the result of "calling" it (see above) is the generation of a direct call to the DoAddString function. That is a simple method, not generic, not virtual or dynamic, like all the other methods of TListHelper, so the unused functions can be eliminated by the linker. This also means there is hardly any duplication of generated code anymore, i.e. if another part of the same executable also uses TList<string>.Add it will use the same function.

No code duplication?

Well, this means that if the class is coded like above, there is no unnecessary duplication of generated code anymore. That is cool and can probably reduce the expected bloat caused by using generics.

But it also means that it greatly reduces one of the advantages associated with generics: no unnecessary duplication of source code. You write a routine once, and only the type it works on is parametrized. The compiler takes care of generating code for each used routine and parametric type.

But then you still get the dreaded bloat. So you either duplicate a lot of source code (take a look at the code of TListHelper in System.Generics.Collections to see what I mean; for example, the functions DoAddWideString, DoAddDynArray, DoAddInterface and DoAddString contain exactly the same code, except for the lines where FItems^ is cast each to a different pointer type), which is almost as much work as writing a separate TList for each type, or you use the naïve approach, writing code only once, as generics should actually be used. But then you could get bloat again, i.e. it is well possible that the same routine gets generated multiple times, in different units of the same executable.

What to do?

There is not much we can do, except to emphatically ask Embarcadero to put the kind of logic that was implemented in TList<T> and other generic classes to avoid duplication of generated code, in the compiler and linker, so we don't have to write huge helper classes nor get the bloat.

In the meantime, you can do what Embarcadero did: if your class is used a lot for many different types, do a lot of "copy and paste generics". Otherwise, if you only have to deal with a few types and in a few places, simply use the naïve approach. Or you use a hybrid approach, using the intrinsics mentioned above and implementing the most used helper functions for the types you need most.

Let's hope they get a lot of requests and implement these things in the compiler and linker, like other languages with generics, e.g. C# or C++, do.

Rudy Velthuis

Friday 14 September 2018

Making Delphi operator overloads compatible with C++Builder

Delphi operator overloads

A few days ago, I made a simple test program to test my BigIntegers in C++Builder. The important part looked like this:

#include "Velthuis.BigIntegers.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
    BigInteger a = 17;
    BigInteger b = "123";
    BigInteger c = a + b;

But that did not compile. I got the following error message for the line that did the addition:

[bcc32c Error] File13.cpp(16): invalid operands to binary expression ('Velthuis::Bigintegers::BigInteger' and 'Velthuis::Bigintegers::BigInteger')

Now, BigIntegers, as I defined them, have overloads for many arithmetic and relational operators. A sample:

    class operator Add(const Left, Right: BigInteger): BigInteger;
    class operator Subtract(const Left, Right: BigInteger): BigInteger;
    class operator Multiply(const Left, Right: BigInteger): BigInteger;

But taking a look in the (Delphi-)generated Velthuis.BigInteger.hpp file, I saw that these are declared as simple static functions with an _op_ prefix:

static BigInteger __fastcall _op_Addition(const BigInteger &Left, 
    const BigInteger &Right);
static BigInteger __fastcall _op_Subtraction(const BigInteger &Left, 
    const BigInteger &Right);
static BigInteger __fastcall _op_Multiply(const BigInteger &Left, 
    const BigInteger &Right);

Lines wrapped to make them more readable in this blog

So, to use them, you have to call them explicitly:

    BigInteger c = BigInteger::_op_Addition(a, b);

That is ugly, and unusual for someone used to C++'s operator overloading. It makes using BigIntegers pretty inconvenient. OK, I defined static backup functions for the arithmetic overloads, so in the case of BigIntegers, you can just as well use a more or less Java-style method call:

    BigInteger c = BigInteger::Add(a, b);

C++ operator overloads

I wanted my operator overloads to be usable as in the first code sample. I read up a bit on C++ operator overloading, and came up with the following solution:

inline BigInteger operator +(const BigInteger& left, const BigInteger& right) 
{ return BigInteger::Add(left, right); }

I added that line to the C++ file before the main function, and all of a sudden it compiled and even produced the right result. So I wrote a file called Velthuis.BigIntegers.operators.hpp, with lines similar to the above for all supported operator overloads and included that too:

#include "Velthuis.BigIntegers.hpp"
#include "Velthuis.BigIntegers.operators.hpp"

Ok, that worked. But I didn't find that solution very satisfactory. To use BigIntegers with overloaded operators, you had to include an additional header file. And I could not just include that file in the Velthuis.BigIntegers.hpp file, because that is generated by the Delphi compiler, and any re-generation would overwrite any edits made to it. So what to do?

Including new header in generated header

But then I remembered that there is something like {$HPPEMIT} in Delphi. That allows you to add extra lines of C++ code to a generated .hpp file. So I added:

{$HPPEMIT '#include "Velthuis.BigIntegers.operators.hpp"'}

But that did not compile at all! I looked at the generated .hpp file and saw that the line was added somewhere near the top, well before the declaration of BigInteger. But it had to be included near the bottom of the generated hpp. Fortunately, the online help shows that nowadays (according to Remy Lebeau, even since Delphi 2009 or 2010), you can add an END attribute to the directive:

{$HPPEMIT END '#include "Velthuis.BigIntegers.operators.hpp"'}

And now it was added somewhere near the bottom of the header, well after the declaration of BigInteger. Success! Now the code sample at the top of this post compiled and worked as expected.

Conclusion

To recap: To make Delphi-defined operator overloads usable in C++, you will have to

  • write C++ operator overload wrappers for them.
  • You can pack those in a header of its own
  • and then include that header in the generated header for your Delphi file using {$HPPEMIT END '#include "NameOfYourHeader"'}.

I added these changes to my GitHub repository DelphiBigNumbers.

Rudy Velthuis