Forum Discussion
nitass
Employee
Oct 08, 2011Braced expressions
Hi there,
I have read about braced expressions in articles below but I still do not understand why we should enclose expr argument with brace, what different it is. Could anyone advise please?
iRules 101 - 02 - If and Expressions (Performance Considerations)
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/124/iRules-101--02--If-and-Expressions.aspx
and
iRules Optimization 101 - 02 - Expressions and Variables (Section One: Braced Expressions)
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/110/iRules-Optimization-101--02--Expressions-and-Variables.aspx
Thanks a lot!
2 Replies
- hoolio
Cirrostratus
Hi Nitass,
It's for performance. See the expr man page's 'performance considerations' section for details:
http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm
Enclose expressions in braces for the best speed and the smallest storage requirements. This allows the Tcl bytecode compiler to generate the best code.
As mentioned above, expressions are substituted twice: once by the Tcl parser and once by the expr command. For example, the commands
set a 3
set b {$a + 2}
expr $b*4
return 11, not a multiple of 4. This is because the Tcl parser will first substitute $a + 2 for the variable b, then the expr command will evaluate the expression $a + 2*4.
Most expressions do not require a second round of substitutions. Either they are enclosed in braces or, if not, their variable and command substitutions yield numbers or strings that don't themselves require substitutions. However, because a few unbraced expressions need two rounds of substitutions, the bytecode compiler must emit additional instructions to handle this situation. The most expensive code is required for unbraced expressions that contain command substitutions. These expressions must be implemented by generating new code each time the expression is executed.
Aaron - nitass
Employee
Hi Aaron,
Thanks. That is the part I do not understand.
Would you mind explaining me conversion steps? How is it different between with and without brace?
By adding in braces around the values being passed to the expr command, we can stop unnecessary conversions, such as numeric -> string -> numeric, from occurring.Thanks!
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects