Concatenating strings

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Concatenating strings

yvanavermaet
This post was updated on .
Dear

In the documentation it's written that concatenating strings can be done with the "+"-sign. However, we've found that doesn't work as expected. Well, it does work, but it first converts them to BigDecimals, then throws an error per value that's not a number and then concatenates it as strings.

So: "a" + "b" ==> a to BigDecimal, b to BigDecimal. a is not a number ==> error, b is not a number ==> error. concatenate as strings (so the end result works)

We've noticed that this causes most of the Thymeleaf CPU load and IO actions on our servers.

This is not only for the "+" sign, but also for "eq", "ne", ... "boolean eq true", will first convert to bigdecimals, etc etc etc.

For now we'll have to refactor all our code to using the #strings, #bools, ... expression utilities.

Is this something that'll be fixed in Thymeleaf 3?

Edit: How would one perform the following check: "Object ne null". I can't find an equivalent. For strings, I can just use #strings.isEmpty()

Kind regards,
Yannick
Reply | Threaded
Open this post in threaded view
|

Re: Concatenating strings

danielfernandez
Administrator
Would you be so kind to open a GitHub ticket for this? http://github.com/thymeleaf/thymeleaf/issues

I agree that it might be possible to optimize that intent to convert the operands into BigDecimal, but we've never seen this meaning a significant percent of template execution time in our profiling/benchmarking sessions (and we are *really* into profiling and optimizing hot spots in 3.0), so this is a scenario I'd definitely want to have a more careful look at. Maybe your templates are especially heavy on String append operations?

Thanks,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Concatenating strings

danielfernandez
Administrator
In reply to this post by yvanavermaet
Reply | Threaded
Open this post in threaded view
|

Re: Concatenating strings

danielfernandez
Administrator
Also back-ported to the 2.1 branch: https://github.com/thymeleaf/thymeleaf/issues/449
Reply | Threaded
Open this post in threaded view
|

Re: Concatenating strings

yvanavermaet
In reply to this post by danielfernandez
Hi Daniel

My apologies for the late reply, I was on holiday :-)

Correct, our templates are heavy on String append and comparison operations.

The problem is not only with the concatenation. It also occurs with the "eq", "ne", ...as states in my first post. And not just strings, but also with booleans and objects (maybe some other?)
Also, is there an alternative way for th:if="${Object ne null}" ? Is th:if="${Object}" an alternative and how would it be processed? I want to avoid using the EvaluateAsNumber (as #strings.concat does for example)

Kind regards,
Yannick
Reply | Threaded
Open this post in threaded view
|

Re: Concatenating strings

yvanavermaet
This post was updated on .
Also noticed that the pipe-symbols (|) also use the same evaluateAsNumber and therefor throw errors.

Example: th:src="|sometext${myvariable}|

Edit:
Switch-case also uses the evaluateAsNumber.