Jump to content
Mike Torrettinni

I don't understand this CONST record argument behavior

Recommended Posts

11 hours ago, Kas Ob. said:

One thing though, as friendly reminder and as rule of thumb, get the Count from the container into local var to be used for the loop

In

 

for i := 0 to List.Count - 1 do

  Foo(I);

 

List.Count is evaluated exactly once. 

  • Like 1

Share this post


Link to post
2 hours ago, David Heffernan said:

List.Count is evaluated exactly once. 

Yes, but the point was that one has to use the collection's Count property for indexing and never the Length of the internal array, as the array may have more elements (preallocated for possible expansion) than the real number of elements in the collection.

Share this post


Link to post
1 minute ago, Alexander Elagin said:

Yes, but the point was that one has to use the collection's Count property for indexing and never the Length of the internal array, as the array may have more elements (preallocated for possible expansion) than the real number of elements in the collection.

There were two points in that post. The point about not using Length of the internal list is well made. 

 

But my post reacts to the other point in that post which advocated taking a copy of Count into a local variable before the for loop. 

  • Like 1

Share this post


Link to post
Guest
13 minutes ago, David Heffernan said:

There were two points in that post.

Right, there was two points.

 

I would not trust the compiler to generate one evaluation everywhere, i have seen this, and it get more complicated when you change your mind for some reason and switch from for to while.

Now, evaluate on each iteration is not bug or a problem, but will rip your code very small portion of performance ( depends on the loop complexity and length), while the performance was the target for such use.

 

Share this post


Link to post
1 hour ago, Kas Ob. said:

I would not trust the compiler to generate one evaluation everywhere, i have seen this

You have not seen this. The classic for loop evaluates the loop ranges exactly once. This is contracted by the language. 

 

http://docwiki.embarcadero.com/RADStudio/Sydney/en/Declarations_and_Statements_(Delphi)#For_Statements

 

"For purposes of controlling the execution of the loop, the expressions initialValue and finalValue are evaluated only once, before the loop begins."

 

This sort of FUD is not helpful to anybody. 

  • Like 1

Share this post


Link to post
Guest
44 minutes ago, David Heffernan said:

This is contracted by the language. 

Like there wasn't bugs in life of Delphi compilers.

 

45 minutes ago, David Heffernan said:

This sort of FUD is not helpful to anybody. 

1) It was an advice and it will make sure it will be consistence, for loop limit after evaluation will be anyway stored as local variable, so no harm there.

2) As i said, it mainly concern when later you change the code from "for" to "while" loop.

3) The compiler and i have seen it, it generate bad code, in fact it was ugly with unexplainable assembly block, while sometimes still functioning or looks like functioning logically right.

 

While i am not sure that you have faced such cases where the compiler start to generate unoptimized code, and where it does ugly comparison and mix the evaluation for boolean, also for comparison in other types not only boolean, all what i was saying is to be on the safe side and don't leave it to the compiler when the performance is priority, don't leave those details to the compiler to decide, also for self discipline and take it as a habit like a reminder when you are using a class in twisted way from the architecture design and intention. 

Share this post


Link to post
13 minutes ago, Kas Ob. said:

Like there wasn't bugs in life of Delphi compilers.

Sure. But there has never been a compiler bug where this particular language contract was not respected.

 

You say that you have seen the compiler generate code which evaluates for loop ranges more than once. But that's incorrect. You have not seen that because no version of Delphi has ever had such a bug.

14 minutes ago, Kas Ob. said:

It was an advice and it will make sure it will be consistence, for loop limit after evaluation will be anyway stored as local variable, so no harm there.

My point is that it is poor advice, a canonical example of FUD. 

 

15 minutes ago, Kas Ob. said:

As i said, it mainly concern when later you change the code from "for" to "while" loop.

For sure it's worth knowing that while loops are different. Proficient Delphi programmers do know this, and are alive to this whenever they make choices of which loop to use.

Share this post


Link to post

The issue here is not the list usage itself but using a list of records.

I bet that the difference between the for in loop and the for to loop with accessing the underlying array would be almost zero if the items were objects.

Edited by Stefan Glienke
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×