Ah, now I finally get what you mean by optimize, you want to avoid making
copies. That is certainly a good idea.
One thing I considered at some point was to let the PatternConverters write
to a ByteBuffer instead of to a StringBuilder.
That may be an interesting thing to experiment with. I would recommend
using a ByteBuffer for this. You can wrap a byte[] array in a ByteBuffer
and when the PatternConverters are done writing to the ByteBuffer, use the
underlying byte[] array to feed data to the legacy appenders.

On Wed, Feb 24, 2016 at 9:16 PM, Mikael Ståldal <[email protected]>
wrote:

> I want to optimize toByteArray() in an appender, and have all existing
> appenders take advantage of it (without having to use encode).
>
> I don't want to use StringBuilder.toString(), I want to avoid the
> intermediate String object.
>
> This is not only about avoiding GC, it is also about avoiding unnecessary
> copying of data.
>
> On Wed, Feb 24, 2016 at 12:06 PM, Remko Popma <[email protected]>
> wrote:
>
>> I don't understand though:  appenders should only call encode() if they
>> can offer the Layout a ByteBuffer to write into...
>>
>> Sent from my iPhone
>>
>> On 2016/02/24, at 19:36, Mikael Ståldal <[email protected]>
>> wrote:
>>
>> But it would be useful to have a way to go from StringBuilder to byte[]
>> (not ByteBuffer) as well, to be able to optimize a layout and still have it
>> working together with all existing appenders.
>>
>> On Wed, Feb 24, 2016 at 11:19 AM, Remko Popma <[email protected]>
>> wrote:
>>
>>> StringBuilder implements CharSequence; the copy method takes a CharSequence
>>> as its source of characters.
>>>
>>>
>>> On Wednesday, 24 February 2016, Remko Popma <[email protected]>
>>> wrote:
>>>
>>>> See TextEncoderHelper's methods,
>>>> copy: from StringBuilder to CharBuffer
>>>> encodeText: from CharBuffer to ByteBufferDestination
>>>>
>>>> We don't want to create the byte[] array object so we write directly
>>>> into the ByteBuffer.
>>>>
>>>> On Wednesday, 24 February 2016, Mikael Ståldal <
>>>> [email protected]> wrote:
>>>>
>>>>> I still cannot find a way to convert a StringBuilder to a byte[].
>>>>>
>>>>> On Tue, Feb 23, 2016 at 11:03 PM, Gary Gregory <[email protected]
>>>>> > wrote:
>>>>>
>>>>>> How about /dropping/ Serializer1 altogether? Having both is mighty
>>>>>> complicated.
>>>>>>
>>>>>> Gary
>>>>>>
>>>>>> On Tue, Feb 23, 2016 at 8:52 AM, Remko Popma <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> I wouldn't mind that (no strong opinion either way) but I won't have
>>>>>>> time to do that work.
>>>>>>>
>>>>>>> On Wed, Feb 24, 2016 at 1:23 AM, Gary Gregory <
>>>>>>> [email protected]> wrote:
>>>>>>>
>>>>>>>> I see we now have:
>>>>>>>>
>>>>>>>>
>>>>>>>> org.apache.logging.log4j.core.layout.AbstractStringLayout.Serializer2
>>>>>>>>
>>>>>>>> Should we
>>>>>>>> deprecate 
>>>>>>>> org.apache.logging.log4j.core.layout.AbstractStringLayout.Serializer
>>>>>>>> and reimplement everything in terms of Serializer2, even if objects are
>>>>>>>> allocated?
>>>>>>>>
>>>>>>>> Gary
>>>>>>>>
>>>>>>>> On Tue, Feb 23, 2016 at 7:59 AM, Remko Popma <[email protected]
>>>>>>>> > wrote:
>>>>>>>>
>>>>>>>>> Layout now extends Encoder so now it has this method:
>>>>>>>>>
>>>>>>>>> encode(LogEvent source, ByteBufferDestination destination)
>>>>>>>>>
>>>>>>>>> PatternLayout implements this method. It delegates the work of
>>>>>>>>> converting the StringBuilder to bytes and writing these bytes into the
>>>>>>>>> ByteBufferDestination to the TextEncoderHelper class. No objects are
>>>>>>>>> created during this conversion.
>>>>>>>>>
>>>>>>>>> The final piece is (Rolling)RandomAccessFileAppender, whose
>>>>>>>>> manager implements ByteBufferDestination. This appender calls the 
>>>>>>>>> #encode()
>>>>>>>>> methos on the layout (other managers still call Layout#toByteArray).
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Wed, Feb 24, 2016 at 12:47 AM, Mikael Ståldal <
>>>>>>>>> [email protected]> wrote:
>>>>>>>>>
>>>>>>>>>> I cannot really find it.
>>>>>>>>>>
>>>>>>>>>> On Tue, Feb 23, 2016 at 3:59 PM, Remko Popma <
>>>>>>>>>> [email protected]> wrote:
>>>>>>>>>>
>>>>>>>>>>> Yes, that was implemented in
>>>>>>>>>>> https://issues.apache.org/jira/browse/LOG4J2-1274.
>>>>>>>>>>>
>>>>>>>>>>> Please also see the description and discussion in the epic
>>>>>>>>>>> https://issues.apache.org/jira/browse/LOG4J2-1270.
>>>>>>>>>>>
>>>>>>>>>>> Sent from my iPhone
>>>>>>>>>>>
>>>>>>>>>>> On 2016/02/23, at 23:56, Mikael Ståldal <
>>>>>>>>>>> [email protected]> wrote:
>>>>>>>>>>>
>>>>>>>>>>> Do we have any way to convert a StringBuilder to a byte[],
>>>>>>>>>>> without any intermediate String?
>>>>>>>>>>>
>>>>>>>>>>> It seems like org.apache.logging.log4j.core.util.StringEncoder 
>>>>>>>>>>> cannot
>>>>>>>>>>> do it.
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> [image: MagineTV]
>>>>>>>>>>>
>>>>>>>>>>> *Mikael Ståldal*
>>>>>>>>>>> Senior software developer
>>>>>>>>>>>
>>>>>>>>>>> *Magine TV*
>>>>>>>>>>> [email protected]
>>>>>>>>>>> Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com
>>>>>>>>>>>
>>>>>>>>>>> Privileged and/or Confidential Information may be contained in
>>>>>>>>>>> this message. If you are not the addressee indicated in this message
>>>>>>>>>>> (or responsible for delivery of the message to such a person),
>>>>>>>>>>> you may not copy or deliver this message to anyone. In such case,
>>>>>>>>>>> you should destroy this message and kindly notify the sender by
>>>>>>>>>>> reply email.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> [image: MagineTV]
>>>>>>>>>>
>>>>>>>>>> *Mikael Ståldal*
>>>>>>>>>> Senior software developer
>>>>>>>>>>
>>>>>>>>>> *Magine TV*
>>>>>>>>>> [email protected]
>>>>>>>>>> Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com
>>>>>>>>>>
>>>>>>>>>> Privileged and/or Confidential Information may be contained in
>>>>>>>>>> this message. If you are not the addressee indicated in this message
>>>>>>>>>> (or responsible for delivery of the message to such a person),
>>>>>>>>>> you may not copy or deliver this message to anyone. In such case,
>>>>>>>>>> you should destroy this message and kindly notify the sender by
>>>>>>>>>> reply email.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> E-Mail: [email protected] | [email protected]
>>>>>>>> Java Persistence with Hibernate, Second Edition
>>>>>>>> <http://www.manning.com/bauer3/>
>>>>>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>>>>>>> Spring Batch in Action <http://www.manning.com/templier/>
>>>>>>>> Blog: http://garygregory.wordpress.com
>>>>>>>> Home: http://garygregory.com/
>>>>>>>> Tweet! http://twitter.com/GaryGregory
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> E-Mail: [email protected] | [email protected]
>>>>>> Java Persistence with Hibernate, Second Edition
>>>>>> <http://www.manning.com/bauer3/>
>>>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>>>>> Spring Batch in Action <http://www.manning.com/templier/>
>>>>>> Blog: http://garygregory.wordpress.com
>>>>>> Home: http://garygregory.com/
>>>>>> Tweet! http://twitter.com/GaryGregory
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> [image: MagineTV]
>>>>>
>>>>> *Mikael Ståldal*
>>>>> Senior software developer
>>>>>
>>>>> *Magine TV*
>>>>> [email protected]
>>>>> Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com
>>>>>
>>>>> Privileged and/or Confidential Information may be contained in this
>>>>> message. If you are not the addressee indicated in this message
>>>>> (or responsible for delivery of the message to such a person), you may
>>>>> not copy or deliver this message to anyone. In such case,
>>>>> you should destroy this message and kindly notify the sender by reply
>>>>> email.
>>>>>
>>>>
>>
>>
>> --
>> [image: MagineTV]
>>
>> *Mikael Ståldal*
>> Senior software developer
>>
>> *Magine TV*
>> [email protected]
>> Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com
>>
>> Privileged and/or Confidential Information may be contained in this
>> message. If you are not the addressee indicated in this message
>> (or responsible for delivery of the message to such a person), you may
>> not copy or deliver this message to anyone. In such case,
>> you should destroy this message and kindly notify the sender by reply
>> email.
>>
>>
>
>
> --
> [image: MagineTV]
>
> *Mikael Ståldal*
> Senior software developer
>
> *Magine TV*
> [email protected]
> Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com
>
> Privileged and/or Confidential Information may be contained in this
> message. If you are not the addressee indicated in this message
> (or responsible for delivery of the message to such a person), you may not
> copy or deliver this message to anyone. In such case,
> you should destroy this message and kindly notify the sender by reply
> email.
>

Reply via email to