About a year and a half ago, I reported on how slow the original implementation of TValue in Delphi 2010 was, touching off a storm of comments and various other blog posts as other Delphi community members conducted similar experiments. One thing that came out of it was a suggestion by Robert Love on how to improve performance by adding code to optimize for the most common cases.
I expanded on his idea and sent a suggested implementation to Barry Kelly. Unfortunately, it didn’t make it into XE, but it’s in XE2 now. It’s not exactly the code I suggested–a few things have changed in the way TValue works internally since D2010, and there’s at least one bugfix that I can see–but the basic idea is there. When storing values into a TValue and retrieving them again, there’s special code to set and retrieve the most common types directly, without having to go into the generic RTTI-based (much slower) conversion routines.
I reran the timing test under XE2, and the results were much better this time:
Variants: 2140
TValues: 2895
Still about 30% slower than Variants, but at least it’s not 2000% slower anymore. Thanks, Barry, and thanks to Robert too for the original suggestion!