Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't compiler optimizing away this code

I have a code using third-party-tool iterating over a collection of points.

for (int i = 0; i < pcoll.PointCount; i++) { /* ... */ }

When doing profiling via dotTrace I noticed that the PointCount-proerty is accessed every iteration (see picture above)

.

I expected that the value for this property is optimized away by the compiler but obviously that doesn't happen. Maybe this is actually a problem within the COM-based 3rd-party lib or also within dotTrace self when collecting the information.

I'm not sure if this topic wouldn't fit better to Gis.StackExchange. However maybe someone has any idea under which circumstances optimzation won't take place or how it might happen.

like image 787
MakePeaceGreatAgain Avatar asked Dec 19 '22 02:12

MakePeaceGreatAgain


1 Answers

Simply put, how is the compiler to know whether pcoll.PointCount will change between invocations? It can't safely make the assumption that the value will remain unchanged, so it can't optimise this code by caching the value of the first call to pcoll.PointCount.

like image 181
David Arno Avatar answered Dec 27 '22 23:12

David Arno