RTTI data is cached using a reference counted pool. The reason your code speeds up so much is because the TRttiContext.Create.Free
statement creates a temp TRttiContext
that stays in scope for the lifetime of your app, maintaining an active reference to that pool. Subsequent TRttiContext
instances created inside of GetPropertyIndex()
are re-using the existing pool and not wasting time re-creating new pools each time. When the DPR code reaches the end.
statement, the temp TRttiContext
goes out of scope and releases its reference to the pool.
When you remove the TRttiContext.Create.Free
statement, the temp TRttiContext
disappears, and a new pool is created and destroyed for each TRttiContext
that GetPropertyIndex()
uses, wasting time re-creating the same cache over and over.
You can easily see this in action by enabling Debug DCUs in the Project Options and then stepping into the TRttiContext
code in the debugger.