Reputation: 32885
What does it take, to take advantage of the performance gain from ColdFusion 2016's new setting searchImplicitScopes="false"
:
... bypasses searching for a variable in the implicit scopes thus finding the variables defined in the application faster.
Should we start scoping Variables
& This
scopes inside a CFC with searchImplicitScopes="false"
in ColdFusion 2016?
I cannot find any documentation on what are considered implicit scopes in CF2016. I am pretty sure local
and arguments
scopes are fine inside a function, but what about frequently used scopes like variables
and this
in a CFC?
Upvotes: 3
Views: 545
Reputation: 32885
Just installed CF 2016 Express and I can conclude that with searchImplicitScopes="false"
, the Variables
scope is still being searched when a variable is not scoped, but not the This
scope.
Upvotes: 1
Reputation: 14859
Scope all the things!!!
The variables
scope inside a CFC is global to all functions inside the CFC.
The this
scope inside a CFC is global to all functions inside the CFC and can also be referenced from the caller of the CFC.
If you do not scope a variable inside a CFC, it defaults to the variables
scope.
<--- this_test.cfc --->
<cfcomponent>
<cfset variables.foo = "This is my CFC global variable." />
<cfset this.bar = "This variable is global to my CFC and can be referenced externally." />
<cfset narf = "Global variable! Point!" />
<cffunction name="getNarf" access="public">
<cfreturn narf />
</cffunction>
</cfcomponent>
Test call:
<cfset test = new this_test() />
<cfoutput> <li>#test.foo#</li> <li>#test.bar#</li> <li>#test.narf#</li> <li>#test.getNarf()#</li> </cfoutput>
foo
is in the variables
scope, you'll get an error: Element FOO is undefined in TEST.
bar
is in the this
scope, so you get the output for test.bar
.narf
is in the variables
scope, you'll get an error: Element NARF is undefined in TEST.
narf
is to have a function return it.Update
That searchImplicitScopes
can turn off CF's scope search capability. So if you don't scope something, it won't look up the hierarchy of scopes. Question is, does an un-scoped variable end up in the variables
scope by default? I would say, yes, as that's been the default since CFCs debuted.
Regardless of using that setting, I still say scope everything. The whole reason they created the implicit local
scope was because:
var
scoping function local variablesvar local = structNew()
, so they only had to var
scope one variable.var a = 0
became the same as local.a = 0
, you could remove all instances of var local = structNew()
, so long as you also "scoped" and referenced those private variables as local.a
.The bottom line
is that there is some performance overhead if CF has to look up variable scopes on every request. If you turned off that lookup using searchImplicitScopes=false
, you should get some performance boost. But then, that should really rely on your application and average request load.
Upvotes: 3