James Harris
02/28/2024, 2:43 PMvar to create a pseudo local scope vs. using the built in local scope.
Here's a CF Fiddle. The use-case in this Fiddle is not real by any means, but each code block is doing the same thing (unless someone sees something we missed) and producing much different results than expected, with the third being the most wild. The code examples were designed to perform access and assignment operations.
We are seeing this on all versions of CF that CF Fiddle supports, with CF 2023 being the most affected.
Does anyone have any feedback about why ColdFusion handles situation 1 and 3 more efficiently than 2?
Thanks for your time!Rodney
02/28/2024, 2:57 PMJames Harris
02/28/2024, 3:00 PMaliaspooryorik
var scoping, scopes the variable to the current function scope. So it's not really having to hunt through all the scopes to find it. Personally I think the inbuilt "local' scope is horrible - just use var. I'd also write your function without using the local scope at all. Something like:aliaspooryorik
function d() {
var result = {};
for (var j = 1; j <= 10000; j++) {
result[j] = j;
if (result[j] > 0) {
result[j] = result[j];
}
}
return result;
}aliaspooryorik
aliaspooryorik
James Harris
02/28/2024, 4:10 PMvar keyword simply leverages the local scope to keep the variable local to the function.
https://cffiddle.org/app/file?filepath=9b87dd30-202b-41f3-828c-762521f0ae55/819e5e6[ā¦]df9-9ad1-bbb5363e7aa6/a8dfaa03-ab0a-4095-b15c-8206a1c7a3e9.cfmaliaspooryorik
local. It's something I've only seen in CFML so esoteric and it doesn't solve anything that just using var doesn't already do and var does better.James Harris
02/28/2024, 4:16 PMlocal prefix assists with visually separating what is available within your controller's action vs. what is available in the view.
Opinions aside, though, I am really just trying to get an understanding about why using the local scope is slower when var is using local scope anyhow. This behavior contradicts the CFML documentation, which recommends always scoping your variables to avoid scope lookups.aliaspooryorik
var is essentially localaliaspooryorik
James Harris
02/28/2024, 4:25 PMvar utilizes the local scope, not that they are the same. In my eyes, using var and then not specifying scope on the variable would in fact cause ColdFusion to perform a scope lookup. Granted in this instance it is the first scope evaluated, hence the speed is great. However, to me, that does not jive with the CFML docs.
And again - I am not after opinions on which to use, rather I am seeking to try and understand what's happening under the hood.Dave Merrill
02/28/2024, 4:29 PMJames Harris
02/28/2024, 4:37 PMIf you use a variable name without a scope prefix, ColdFusion checks the scopes in the following order to find the variable:aliaspooryorik
var is scoping the variable none of that applies, or that's how I read it anyway.Rodney
02/28/2024, 4:47 PMRodney
02/28/2024, 4:49 PMJames Harris
02/28/2024, 5:06 PMseancorfield
local scope was introduced. Prior to local scope, a lot of people defined a local variable as an empty struct and then used local.foo for a local variable. When both ACF and Lucee introduced the local scope, in order to be backward compatible, the engine still had to do a lookup on local first to establish whether there was a legacy-style "local scope" variable defined, before assuming local meant the newly-introduced scope.
So local.foo now does two lookups, effectively, where foo does only one.seancorfield
James Harris
02/28/2024, 5:48 PMseancorfield
James Harris
02/28/2024, 5:54 PMarguments. off, or is that more of a readability thing?seancorfield
ARGUMENTS.foo (or arguments.foo) just plain ugly. Whereas other (outer, increasingly global) scopes add useful semantic hints (this, variables, request, application, server, url, form, etc).seancorfield
variables.foo in a .cfm either -- again, because it adds no semantic value.seancorfield
local scope also happens to improve performance...James Harris
02/28/2024, 6:02 PMDave Merrill
02/28/2024, 6:06 PMJames Harris
02/28/2024, 6:11 PMseancorfield
Avoids dumb scoping errors and the scope search performance hitIf you enable the restricted scope lookup, most of that performance hit for
arguments. scope goes away. Of course, not all (legacy) code is compatible with that restricted scope lookup (all mine is tho' š ).James Harris
02/28/2024, 6:33 PMRodney
02/28/2024, 6:42 PMJames Harris
02/28/2024, 6:45 PMseancorfield
davequested
02/28/2024, 6:58 PMI believe this is due to a backward compatibility issue when@Mark Takata (Adobe) could we get a setting added to cfadmin to stop this backward compatibility check on local scope? We use local everywhere, and I'm guessing others do too. A quick setting could make a substantial improvement for many apps with the flick of a setting. Based on that fiddle that's a 2x improvement on CF2023.scope was introduced. Prior tolocalscope, a lot of people defined alocalvariable as an empty struct and then usedlocalfor a local variable. When both ACF and Lucee introduced thelocal.fooscope, in order to be backward compatible, the engine still had to do a lookup onlocalfirst to establish whether there was a legacy-style "local scope" variable defined, before assuminglocalmeant the newly-introduced scope.local
Mark Takata (Adobe)
02/28/2024, 7:11 PMDave Merrill
02/28/2024, 7:21 PMseancorfield
form scope vars without the scope-qualifier and one place that referred to a request scope var. And both were in internal-facing code, not customer-facing. Given the 250kloc codebase, that wasn't too bad...Rodney
02/28/2024, 7:32 PM