Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify string concatenation in log #1378

Merged
merged 1 commit into from
Feb 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@ private void decode(Object message) {
try {
((Decodeable) message).decode();
if (log.isDebugEnabled()) {
log.debug(new StringBuilder(32).append("Decode decodeable message ")
.append(message.getClass().getName()).toString());
log.debug("Decode decodeable message " + message.getClass().getName());
}
} catch (Throwable e) {
if (log.isWarnEnabled()) {
log.warn(
new StringBuilder(32)
.append("Call Decodeable.decode failed: ")
.append(e.getMessage()).toString(),
e);
log.warn("Call Decodeable.decode failed: " + e.getMessage(), e);
Copy link
Member

Choose a reason for hiding this comment

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

the capacity of StringBuilder in modified statement is 16, not equals with 32 in the original one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @kimmking , what do you think about this change?

Copy link
Member

Choose a reason for hiding this comment

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

remain original codes

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a good optimization, let's wait for other suggestions before merge.

Copy link
Contributor Author

@liketic liketic Feb 23, 2018

Choose a reason for hiding this comment

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

Thanks @chickenlj . For those cases, the naive summation will be much faster than StringBuilder. I made a simple test:

public static void main(String[] args) {

        for (int t = 0; t < 10; t++) {
            long st = System.currentTimeMillis();

            for (int i = 0; i < 100000; i++) {
                String s = "Decode decodeable message " + "xxx";
            }

            long e = System.currentTimeMillis();
            System.out.print(e - st);
            System.out.print(" ");
            
            st = System.currentTimeMillis();

            for (int i = 0; i < 100000; i++) {
                new StringBuilder(32).append("Decode decodeable message ").append("xxx").toString();
            }
            e = System.currentTimeMillis();
            System.out.println(e - st);
        }
    }

and I got output:

1 13
0 11
0 4
0 3
0 4
0 4
0 4
0 1
0 1
0 1

Copy link
Member

Choose a reason for hiding this comment

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

this is not an equivalent comparison. the compiler may optimize "Decode decodeable message " + "xxx" into "Decode decodeable message xxx" directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @beiwei30 ! That's my mistake. The test is uncorrect. The two strings cannot both be constants. 👍

}
} // ~ end of catch
} // ~ end of if
Expand Down