Tencent / TubeMQ

TubeMQ has been donated to the Apache Software Foundation and renamed to InLong, please visit the new Apache repository: https://github.com/apache/incubator-inlong
https://inlong.apache.org/
2.02k stars 388 forks source link

[question] String concatenation style #40

Closed tisonkun closed 5 years ago

tisonkun commented 5 years ago

Notice there are lots of string concatenations following style

throw new RuntimeException(new StringBuilder(512)
                    .append("[Data Repair] store path is not existed, path is ")
                    .append(storePath).toString());

which can be transferred to

throw new RuntimeException("[Data Repair] store path is not existed, path is " + storePath);

I don't know the reason that we adopt the first style but it seems counter-intuitive.

CC @gosonzhang

gosonzhang commented 5 years ago

To reduce the allocation and release of memory, some fragments are not done in the same way as below. It isn't a problem if the code location is not the main process.

        public void reCreateIndexFiles() {
            final StringBuilder sBuilder = new StringBuilder(512);
            try {
                loadDataSegments(sBuilder);
                deleteIndexFiles(sBuilder);
                createIndexFiles();
            } catch (Throwable ee) {
                sBuilder.delete(0, sBuilder.length());
                logger.error(sBuilder.append("ReCreate Index File of ")
                        .append(this.topicKey).append(" error ").toString(), ee);
            }
        }

减少内存的分配与释放, 部分片段如果没有按照如上同一的方式去做, 只要代码位置不是主干流程,问题不大

tisonkun commented 5 years ago

@gosonzhang

Thanks for your explanation.

FYI https://www.jianshu.com/p/4518359da254 & https://dzone.com/articles/string-concatenation-performacne-improvement-in-ja

Maybe it is valid to replace StringBuilder usage above with string concatenation for readability in non-performance-sensitive scenario.