Skip to content

Use CopyOnWriteArrayList instead of recreating manually#3106

Closed
kezhenxu94 wants to merge 3 commits into
apache:masterfrom
kezhenxu94:polish/cow
Closed

Use CopyOnWriteArrayList instead of recreating manually#3106
kezhenxu94 wants to merge 3 commits into
apache:masterfrom
kezhenxu94:polish/cow

Conversation

@kezhenxu94
Copy link
Copy Markdown
Member

Please answer these questions before submitting pull request

  • Why submit this pull request?
  • Bug fix
  • New feature provided
  • Improve performance

public void addNewTarget(Channels channels, IConsumer consumer) {
Group group = new Group(channels, consumer);
// Recreate the new list to avoid change list while the list is used in consuming.
ArrayList<Group> newList = new ArrayList<Group>();
for (Group target : consumeTargets) {
newList.add(target);
}
newList.add(group);
consumeTargets = newList;
size += channels.size();
}

the comment // Recreate the new list to avoid change list while the list is used in consuming. reminds me of the CopyOnWrite data structure that is perfectly suitable to this scenario, although the method addNewTarget is not frequently invoked, I did a simple benchmark out of curiosity.

This patch also fixes that the non-atomic operation += on the volatile variable size

@kezhenxu94 kezhenxu94 added the feature New feature label Jul 17, 2019
@kezhenxu94 kezhenxu94 added this to the 6.3.0 milestone Jul 17, 2019
@kezhenxu94
Copy link
Copy Markdown
Member Author

kezhenxu94 commented Jul 17, 2019

FYI @wu-sheng , I'm recently considering whether we should create a new module/directory to place all the benchmark tests like other projects, the reasons are:

  1. we have some benchmarks that are independent to any existed module, such as LinkedArrayBenchmark, StringFormatBenchmarkTest, IMHO they should be placed in an independent module, otherwise it's hard for new-comers to refer to and find out whether we already have that benchmark or not

  2. some of the existed benchmarks are annotated with @Test, and are executed in maven test phase (I think), which is not necessary and time consuming, gathering them together may be easy to manage and remove @Test

Just a bold thought and may make no sense at all :)

@wu-sheng
Copy link
Copy Markdown
Member

@kezhenxu94 How about we create WIKI pages for that? And link to the PRs?

@wu-sheng
Copy link
Copy Markdown
Member

We have this. https://cwiki.apache.org/confluence/display/SKYWALKING/Home

I could grant committers access right. Then all the acceptable performance improvement PRs, should have a benchmark list there.

@kezhenxu94
Copy link
Copy Markdown
Member Author

@kezhenxu94 How about we create WIKI pages for that? And link to the PRs?

That's a great alternative, it would be easy for new-comers who are interested in performance optimization and can refer to the wiki

@dmsolr
Copy link
Copy Markdown
Member

dmsolr commented Jul 17, 2019

Out of this patch.
ArrayList has worse performance because ArrayList uses add() one by one. It would be better if using addAll() or new ArrayList<>(collections).

Copy link
Copy Markdown
Member

@wu-sheng wu-sheng left a comment

Choose a reason for hiding this comment

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

Is this really performance concern? This never changes in runtime, right?

@wu-sheng
Copy link
Copy Markdown
Member

We accept a lot of performance improvements these days, but we don't need to focus on that too much.

@wu-sheng
Copy link
Copy Markdown
Member

Like I said in some other PRs, we don't require people to change their coding style, due to that is personal interests.

It is only acceptable when causing runtime performance issue.

@wu-sheng wu-sheng closed this Jul 17, 2019
@wu-sheng wu-sheng added wontfix This will not be worked on and removed feature New feature labels Jul 17, 2019
@kezhenxu94 kezhenxu94 deleted the polish/cow branch July 17, 2019 15:52
@kezhenxu94 kezhenxu94 restored the polish/cow branch July 17, 2019 15:54
@kezhenxu94 kezhenxu94 deleted the polish/cow branch July 17, 2019 15:55
@dmsolr
Copy link
Copy Markdown
Member

dmsolr commented Jul 17, 2019

Yes. In this case, it is more importance to fix size. And I say that it is out of this issue. Just for talking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants