You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Remko Popma (JIRA)" <ji...@apache.org> on 2016/02/23 18:57:18 UTC

[jira] [Resolved] (LOG4J2-1271) ParameterizedMessage optimization

     [ https://issues.apache.org/jira/browse/LOG4J2-1271?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Remko Popma resolved LOG4J2-1271.
---------------------------------
       Resolution: Fixed
    Fix Version/s: 2.6

Fixed in master in commit dca586c.

* ParameterizedMessage implements StringBuilderFormattable and can now write itself into an externally provided StringBuilder
* Parameters are not converted to Strings until they are written to the StringBuilder
* the String[] array copy of the arguments array has been removed
* toFormattedString() and format(String, Object[]) now delegate to the formatTo(StringBuilder) method
* recursiveDeepToString() only creates a HashSet when necessary
* user-specified parameters that implement StringBuilderFormattable can avoid creating temporary objects (instead of calling their toString() method, they are asked to write themself into the StringBuilder)

I also added a new benchmark:
("classic" is the ParameterizedMessage class before the refactoring)
{noformat}
Benchmark                                                                Mode  Samples    Score    Error  Units
o.a.l.l.p.j.ParameterizedMessageBenchmark.classicGetFormattedMsg       sample   114695  412.772 ± 16.107  ns/op
o.a.l.l.p.j.ParameterizedMessageBenchmark.refactoredFormatTo           sample   146206  324.426 ±  3.063  ns/op
o.a.l.l.p.j.ParameterizedMessageBenchmark.refactoredGetFormattedMsg    sample   128818  342.420 ± 12.935  ns/op
{noformat}


> ParameterizedMessage optimization
> ---------------------------------
>
>                 Key: LOG4J2-1271
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1271
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: API
>    Affects Versions: 2.5
>            Reporter: Remko Popma
>            Assignee: Remko Popma
>             Fix For: 2.6
>
>
> ParameterizedMessage creates unnecessary objects and does other unnecessary work in its constructor.
> On the other hand, it does not create the full formatted string it will eventually need to return from {{getFormattedMessage()}}.
> I propose the following changes:
> * Don't call {{argumentsToStrings}} in the constructor. Converting the parameter Object[] array to a String[] array should only be done when necessary: when this ParameterizedMessage is serialized.
> * Don't copy the parameter Object[] array to a new array. Instead clone the array when the {{getParameters()}} method is called.
> * In the constructor, build the fully formatted message. This combines the work done by {{formatStringArgs(String, String[])}} and {{argumentsToStrings(Object[])}} and avoids the issue mentioned in LOG4J2-763.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org