Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

LoggentriesAppender: memory efficient concatenation for layout.ignoresThrowable() #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

LoggentriesAppender: memory efficient concatenation for layout.ignoresThrowable() #53

wants to merge 2 commits into from

Conversation

jcodagnone
Copy link

@jcodagnone jcodagnone commented Jul 7, 2016

Allocate just the right amount of characters once.

Before this change the stack gets concatenated using the string concatenation operator.
In such scenery, the optimization proposed on JLS-15.18.1 may not apply to the code valid
(it is not a one liner).

Oracle 1.8.0_77 i7-4510U CPU @ 2.00GHz, for 60 deep stacktrace exception ("a typical exception")

Legacy code (extracted as an static method):

  n:           98,999
  min:        210,397 nanoseconds
  max:      5,640,727 nanoseconds
  mean:       256,441 nanoseconds
  std dev:     68,782 nanoseconds
  median:     242,984 nanoseconds

After:

  n:           98,999
  min:          2,555 nanoseconds
  max:      2,930,444 nanoseconds
  mean:         5,038 nanoseconds
  std dev:     22,034 nanoseconds
  median:       3,803 nanoseconds

Methodology:

  final int n = 100000;
  final DescriptiveStatistics statistics = new DescriptiveStatistics(n);
  final String []stack = {
     // ....
  };
  for (int i = 0 ; i < n ; i++) {
    final long t0 = System.nanoTime();
    LogentriesAppender.appendFormatedEvent("fooo", stack);
    final long t1 = System.nanoTime();
    if(i > 1000) {
      statistics.addValue(t1-t0);
    }
  }
  System.out.println(statistics);

DescriptiveStatistics is provided by org.apache.commons:commons-math:2.2

buffSize += 1;
}
}

Copy link

@danilosterrapid7 danilosterrapid7 Jul 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some things that might saved more cpu cycles here.
Replacing:

int buffSize = formattedEvent.length();
buffSize += EXCEPTION_SEPARATOR.length();

By:

int buffSize = formattedEvent.length() + EXCEPTION_SEPARATOR.length();

And removing from the loop:

if (i < len - 1) {
    buffSize += 1;
}

And adding after for loop:

buffSize += len - 1;

It would save... ;)

…sThrowable()

Allocate just the right amount of characters once.

Before this change the stack gets concatenated with string concatenation operator.
In such scenary, the optimization proposed on JLS-15.18.1 may not apply to the code valid
(it is not a one liner).

Oracle 1.8.0_77i7-4510U CPU @ 2.00GHz, for 60 deep stacktrace exception ("a tipical exception")

Legacy code (extracted as an static method):
  n:           98,999
  min:        210,397 nanoseconds
  max:      5,640,727 nanoseconds
  mean:       256,441 nanoseconds
  std dev:     68,782 nanoseconds
  median:     242,984 nanoseconds

After:
  n:           98,999
  min:          2,555 nanoseconds
  max:      2,930,444 nanoseconds
  mean:         5,038 nanoseconds
  std dev:     2,2034 nanoseconds
  median:       3,803 nanoseconds

Methodology:
  final int n = 100000;
  final DescriptiveStatistics statistics = new DescriptiveStatistics(n);
  final String []stack = {
     // ....
  };
  for (int i = 0 ; i < n ; i++) {
    final long t0 = System.nanoTime();
    LogentriesAppender.appendFormatedEvent("fooo", stack);
    final long t1 = System.nanoTime();
    if(i > 1000) {
      statistics.addValue(t1-t0);
    }
  }
  System.out.println(statistics);

DescriptiveStatistics is provided by org.apache.commons:commons-math:2.2
@jcodagnone
Copy link
Author

@danilosterrapid7 you are right! commit updated!

@m0wfo
Copy link
Contributor

m0wfo commented Aug 25, 2016

@jcodagnone before doing any more hating, are you running this on Android?

@jcodagnone
Copy link
Author

@m0wfo no, I'm not running it on Android (but I know that StringBuilder is available there), why you ask?

I was reviewing the library before adding it to a project because I wanted to understand if it may block program if the network was slow; and I saw these possibles enhances.

@danilosterrapid7
Copy link

@scawley-r7 a bit ashamed, I really have forgot it. Besides, no one checked it out either. Is it ok to get it merged if you do not have any comment on that?

@danilosterrapid7
Copy link

@stopal-r7 ^^^

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants