Saving memory and performance with const

When defining parameters it’s possible to define them either as default (none), var, out or const. The difference between all these is the way the value is committed to the called function. The cue’s for this are:

– Call-by-reference
– Call-by-value

The above mentioned keywords (none, var, out, const) define the way variables are called:

// Call-by-value
procedure ParamDefault(AValue: Integer);

// Call-by-reference
procedure ParamVar(var AValue: Integer);
procedure ParamOut(out AValue: Integer);
procedure ParamConst(const AValue: Integer);

But why bothering defining parameters as call-by-reference?
By defining a parameter as call-by-reference it’s not the value that is committed, but the address of the value. This can be extremely useful, when the value itself is many times bigger than the pointer (pointer-sizes depend on the architecture).

So let’s look at an example using an Integer-parameter:

{ Call-by-value

Size of Integer is 32-Bit }
procedure CallByValue(AValue: Integer);

{ Call-by-reference

Size of Integer is:
 - 32-Bit system: 32-Bit
 - 64-Bit system: 64-Bit }
procedure CallByReference(const AValue: Integer);

In this example we can see that defining the parameter as call-by-reference it gets even worse. But when we take a type whose size is bigger it quickly becomes clear, why it’s sometimes better to use call-by-reference.

// Size of TSize = 96-Bit
TSize = record
  FWidth: Integer; // 32-Bit
  FHeight: Integer; // 32-Bit
  FDepth: Integer; // 32-Bit
end;

// Size of Sizes = 288-Bit (96-Bit * 3)
var
  Sizes: Array[0..2] of TSize;

Types like records (depending on the fields) and arrays can use quite a big amount of memory where it’s more efficient to use call-by-reference.

The same goes for String. String essentially is an object, but when you commit it to a function as parameter you don’t commit the address like with every other object. Instead it creates a copy of the String in the memory and commits the address.

So a general recommendation is, to use call-by-reference (var, out, const) for record-, array- and String-parameter whenever it’s possible to save memory and performance.

 

TL;DR

Use const/var/out parameters for Arrays, Records and Strings whenever it’s possible.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.