Forum > FPC development

Reference Counting and Ownership

(1/3) > >>

munair:
There's an interesting article I read the other day about an ownership model for reference counting. It was proposed for the Lobster language and it is a very interesting approach. According to the article, this reference counting is mainly done during compile-time and takes away about 95% of the runtime reference count operations.

The ownership model is interesting in that it could tackle (unexpected) reference changes like this (pseudo-code):

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var x = "hello"def f(y):    x = "world"    print y       // should it print "world" or "hello"f(x)I was wondering where FPC stands in this.

Jonas Maebe:
In FPC, ownership is determined by the programmer:
* if they declare the parameter as const, constref or var, it will print 'world' (because then either the programmer assures the compiler that the value won't change, or they are explicitly passing a reference to the string rather than its value)
* if it's a value parameter, it will print 'world' (because then the programmer explicitly told the compiler that they want the value to be passed)

munair:

--- Quote from: Jonas Maebe on December 31, 2021, 01:31:33 pm ---In FPC, ownership is determined by the programmer:
* if they declare the parameter as const, constref or var, it will print 'world' (because then either the programmer assures the compiler that the value won't change, or they are explicitly passing a reference to the string rather than its value)
* if it's a value parameter, it will print 'world' (because then the programmer explicitly told the compiler that they want the value to be passed)
--- End quote ---

But that's basically telling the compiler to either reference (to the same address) or make a copy of the string?

An ownership model instead would make a second reference (y) the new owner if the previous owner (x) changes content.

Jonas Maebe:
Strings are reference-counted, so a value parameter does not make a copy. It just increases the reference count. The copy/ownership changes (dynamically) when the caller changes the value.

If you would change the ownership immediately on calling the routine, then you would change the semantics of existing programs. And even if you consider their behaviour buggy, that won't appease anyone who relies on that behaviour (as we've experienced time and time again when FPC behaves differently from Delphi even if the Delphi behaviour is just a code generation quirk that happens to guarantee the Delphi behaviour rather than something that is defined by the language specification).

munair:
So if I understand correctly, when x is passed by value to parameter y, no copy of x is made. Instead the reference count is increased by 1, so that when x changes in the meantime, the original value is kept alive because a reference (y) still exists, like in this example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program test; var  x: string; procedure f(y: string);begin  x := 'world';  writeln(y);end; begin  writeln;   x := 'hello';  f(x);end.
btw, how do I prevent the code box from writing &#39; for a single quote?

Navigation

[0] Message Index

[#] Next page

Go to full version