kezhenxu94 opened a new pull request #7201: URL: https://github.com/apache/skywalking/pull/7201
There are a lot of calls to `Metrics.id()` and `ISource.getEntityId`, which calculates the id by manipulating strings in every single call, producing many garbage objects. In this patch, I cache the id and only calculate the id if it's requested for the first time. ### Improve the performance of <class or module or ...> - [x] Add a benchmark for the improvement, refer to [the existing ones](https://github.com/apache/skywalking/blob/master/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/LinkedArrayBenchmark.java) ```java AvgFunction function0; AvgFunction function1; Random random = new Random(); long timebucket; Object name; @Setup public void setup() { timebucket = random.nextLong(); name = RandomStringUtils.random(5); function0 = new AvgFunction() { @Override public AcceptableValue<Long> createNew() { throw new UnexpectedException("createNew should not be called"); } @Override public String id0() { return timebucket + ID_CONNECTOR + name; } }; function1 = new AvgFunction() { @Override public AcceptableValue<Long> createNew() { throw new UnexpectedException("createNew should not be called"); } @Override public String id() { return timebucket + ID_CONNECTOR + name; } }; } @Benchmark public void id(Blackhole b) { for (int i = 0; i < 10; i++) { b.consume(function1.id()); } } @Benchmark public void id0(Blackhole b) { for (int i = 0; i < 10; i++) { b.consume(function0.id()); } } public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(MyBenchmark.class.getSimpleName()) .addProfiler(GCProfiler.class) .build(); new Runner(opt).run(); } ``` - [x] The benchmark result. ```text Benchmark Mode Cnt Score Error Units MyBenchmark.id thrpt 25 1201376.291 ± 152856.703 ops/s MyBenchmark.id:·gc.alloc.rate thrpt 25 3683.137 ± 375.101 MB/sec MyBenchmark.id:·gc.alloc.rate.norm thrpt 25 3392.168 ± 73.398 B/op MyBenchmark.id:·gc.churn.G1_Eden_Space thrpt 25 3686.722 ± 376.161 MB/sec MyBenchmark.id:·gc.churn.G1_Eden_Space.norm thrpt 25 3395.417 ± 74.860 B/op MyBenchmark.id:·gc.churn.G1_Survivor_Space thrpt 25 0.005 ± 0.001 MB/sec MyBenchmark.id:·gc.churn.G1_Survivor_Space.norm thrpt 25 0.005 ± 0.001 B/op MyBenchmark.id:·gc.count thrpt 25 2181.000 counts MyBenchmark.id:·gc.time thrpt 25 2336.000 ms MyBenchmark.id0 thrpt 25 39069507.415 ± 1553343.663 ops/s MyBenchmark.id0:·gc.alloc.rate thrpt 25 ≈ 10⁻⁴ MB/sec MyBenchmark.id0:·gc.alloc.rate.norm thrpt 25 ≈ 10⁻⁶ B/op MyBenchmark.id0:·gc.count thrpt 25 ≈ 0 counts ``` - [x] Links/URLs to the theory proof or discussion articles/blogs. <links/URLs here> NO - [x] If this pull request closes/resolves/fixes an existing issue, replace the issue number. Closes #<issue number>. NO - [x] Update the [`CHANGES` log](https://github.com/apache/skywalking/blob/master/CHANGES.md). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
