Forum Discussion
smp_86112
Cirrostratus
Jun 24, 2010Optimizing If statement conditions
I see this examples like this all over the place:
when HTTP_REQUEST {
if { [HTTP::host] equals "www.myhost.com" and [HTTP::uri] equals "/myuri" } {
HTTP::redirect https://[HTTP::host][HTTP::...
Jun 24, 2010
The error is coming up because you are surrounding the combined string with brackets. Try replacing the surrounding brackets with quotes (or curly braces if your embedded string contains quotes itself)
when HTTP_REQUEST {
if { "[HTTP::host][HTTP::uri]" equals "www.myhost.com/myuri" } {
HTTP::redirect https://[HTTP::host][HTTP::uri]
}
}
I'd be interested in the performance gain with this approach. While the first example does do two string comparisons, they are both operating on the builtin variables which are essentially a memory lookup (ie. no additional memory allocations required) while the combined approach takes those two memory lookups and has to allocated a new temporary string and then do the same number of character comparisons but with the overhead of the memory allocation. On first guess, I would think this would actually be slower, but would love to see the results.
As Colin and I say in our optimization presentations, it's a fine line when optimizing your code. You can go hog wild and make your code the absolute fastest it can be but if it's harder to read and limits maintainability, then the trade-off of a few clock cycles, in most cases, will not be worth it. In my opinion the combined approach is more concise and easier to read and thus would be preferable. But that's just my opinion...
-Joe
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