Thanks for the feedback. Unfortunately I am not able to reproduce the cache structure like you did. It doesn't matter how many times I call the function, the speed is always about the same i.e. slow. FYI, I'm using IC 6.1.3.
This is what I observed (and before I get to this conclusion, I was scratching my head for half a day, wondering why is my script suddenly 10x slower). First I ran my script in L environment, it takes about 8 seconds to run. Next I switch to XL environment for the same layout, the script takes less than 0.5 second to run. Then, I switch back to L environment, and now the script takes 8 seconds to run again. Undo the script, run it again in L, still 8 seconds, it doesn't speed up at all as what I would expect from the cache structure you mention.
btw, as long as one of the layout windows is in XL mode, the script takes less than 0.5 second to run. Once all of them are in L, the script slows down again.
I'm not sure if this has anything to do with some of the context optimization done in XL to speed some of the stuff up, but definitely there's something different that is causing the huge difference in function execution speed.