<p>Manik, I believe the constants are interned the moment the class is loaded, so there's no extra work to pass int to the trace method.<br>
</p>
<div class="gmail_quote">Īn data de 07.12.2011 12:43, "Manik Surtani" <<a href="mailto:manik@jboss.org">manik@jboss.org</a>> a scris:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Bringing this thread up again, is it really true to say that<br>
<br>
if (trace) log.trace("blah")<br>
<br>
is as cheap as<br>
<br>
log.trace("blah")<br>
<br>
?<br>
<br>
In the former, with tracing disabled, nothing happens after the condition check. In the latter, you still construct a String "blah", and even if there is no complex interpolation or message construction, the basic String message is still constructed (and starts to take up PermGen space too, as literals are interned [1]).<br>
<br>
So the case for the if (trace) pattern still stands IMO.<br>
<br>
Cheers<br>
Manik<br>
<br>
[1] <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#intern()" target="_blank">http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#intern()</a><br>
"All literal strings and string-valued constant expressions are interned."<br>
<br>
On 28 Sep 2011, at 17:41, David M. Lloyd wrote:<br>
<br>
> On 09/28/2011 11:35 AM, Mircea Markus wrote:<br>
>> Hi,<br>
>><br>
>> I'm not aware of any convention on using trace vs log.isTraceEnabled() to guard the trace statements.<br>
>><br>
>> if (trace) log.trace("some request related stuff")<br>
>> vs<br>
>> if (log.isTraceEnabled()) log.trace("some request related stuff");<br>
>><br>
>> The former is more efficient, but cannot be managed at runtime. It seems to be the preferred form, so shall we stick with it?<br>
><br>
> If you're using the jboss-logging API, and your log statement does not<br>
> do any interpolation, then it is just as fast to do any of the following<br>
> (with no if):<br>
><br>
> log.trace("blah");<br>
> log.tracef("the %s happened to %s", foo, bar);<br>
> log.tracev("the {0} happened to {1}", foo, bar);<br>
><br>
> In the case where trace logging is disabled, these are exactly as<br>
> efficient as the if (log.isTraceEnabled()) variants. In the case where<br>
> it is enabled, it is marginally more efficient (though the trace log<br>
> itself is substantially more expensive of course).<br>
><br>
> Overall I'd avoid the "if" forms unless you're doing complex interpolation:<br>
><br>
> log.trace("Foo " + bar + " baz " + zap);<br>
> log.tracef("the %s happened to %s", fooMethod().barMethod(), bar);<br>
><br>
> ...both of which incur the expense of the expression resolution even if<br>
> the log message is ultimately discarded.<br>
> --<br>
> - DML<br>
> _______________________________________________<br>
> infinispan-dev mailing list<br>
> <a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
> <a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
<br>
--<br>
Manik Surtani<br>
<a href="mailto:manik@jboss.org">manik@jboss.org</a><br>
<a href="http://twitter.com/maniksurtani" target="_blank">twitter.com/maniksurtani</a><br>
<br>
Lead, Infinispan<br>
<a href="http://www.infinispan.org" target="_blank">http://www.infinispan.org</a><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
infinispan-dev mailing list<br>
<a href="mailto:infinispan-dev@lists.jboss.org">infinispan-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/infinispan-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/infinispan-dev</a><br>
</blockquote></div>