[
https://issues.apache.org/jira/browse/LOG4J2-1415?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15317345#comment-15317345
]
Gary Gregory edited comment on LOG4J2-1415 at 6/6/16 9:58 PM:
--------------------------------------------------------------
An cascading if-else will work but seems ineffective.
A one step lookup is usually done with a switch statement or a Map lookup.
In Java we cannot switch on a Class object but we could switch on the class
_name_
Why not do that?
Forget what I said on the ML about Java 8 and lambdas since we could try:
{code:java}
...
final String cname = o.getClass().getName();
switch (cname) {
case "java.lang.Long":
str.append(((Long) o).longValue());
break;
case "java.lang.Integer":
str.append(((Integer) o).intValue());
break;
case "java.lang.Double":
str.append(((Double) o).doubleValue());
break;
...
default:
...
break;
}
{code}
With this, you also do not call {{instanceof}} a bunch of times. We can put the
class names in statics too.
was (Author: garydgregory):
An cascading if-else will work but seems ineffective.
A one step lookup is usually done with a switch statement or a Map lookup.
In Java we cannot switch on a Class object but we could switch on the class
_name_
Why not do that?
Forget what I said on the ML about Java 8 and lambdas since we could try:
{code:java}
...
final String cname = o.getClass().getName();
switch (cname) {
case "java.lang.Long":
str.append(((Long) o).longValue());
break;
case "java.lang.Integer":
str.append(((Integer) o).intValue());
break;
case "java.lang.Double":
str.append(((Double) o).doubleValue());
break;
...
default:
...
break;
}
{code}
> When running in garbage-free mode, boxed primitive types should be formatted
> without allocating
> -----------------------------------------------------------------------------------------------
>
> Key: LOG4J2-1415
> URL: https://issues.apache.org/jira/browse/LOG4J2-1415
> Project: Log4j 2
> Issue Type: Improvement
> Affects Versions: 2.6
> Reporter: Anthony Maire
> Priority: Minor
>
> When using Log4j 2 through the SLF4J binding, the Unboxer mecanism is not
> available and a parameterized message with a primitive type parameter will be
> auto-boxed.
> Then this boxed value will be formatted by
> ParameterFormatter.recursiveDeepToString() and further allocations will happen
> To lower allocation rate for SLF4J users,
> ParameterFormatter.appendSpecialTypes() should handle boxed primitive types
> too.
> {code}
> private static boolean appendSpecialTypes(final Object o, final StringBuilder
> str) {
> ...
> } else if (o instanceof Long) {
> str.append(((Long) o).longValue());
> return true;
> } else if (o instanceof Integer) {
> str.append(((Integer) o).intValue());
> return true;
> } else if (o instanceof Double) {
> str.append(((Double) o).doubleValue());
> return true;
> } // similarly for float, short, boolean and char.
> ...
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]