Skip to content

Fix for #103 #124

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

Merged
merged 9 commits into from
Dec 16, 2017
Merged

Fix for #103 #124

merged 9 commits into from
Dec 16, 2017

Conversation

yiming-tang-cs
Copy link
Contributor

@yiming-tang-cs yiming-tang-cs commented Nov 30, 2017

Fix for #103.

Copy link
Member

@khatchad khatchad left a comment

Choose a reason for hiding this comment

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

Please add one more test case similar to testNonInternalAPI4() but varying execution modes.


public void testNonInternalAPI4() throws Exception {
helper(new StreamAnalysisExpectedResult("new HashSet<>().stream()",
Collections.singleton(ExecutionMode.SEQUENTIAL), Collections.singleton(Ordering.UNORDERED), false, false,
Copy link
Member

Choose a reason for hiding this comment

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

@saledouble This should be ordered, correct?

Copy link
Member

Choose a reason for hiding this comment

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

@saledouble I'm confused as to why the the ordering is UNORDERED. Do you know why? I am wondering if it is the case that we determine ordering upon a terminal operation and when we think there's none, we are just using some default value. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the test case is

class A {

	Stream<Object> m() {
		Stream<Object> stream = new HashSet<>().stream();
		return stream;
	}

	@EntryPoint
	void n() {
		Stream<Object> s = m();
		s.sorted().distinct().count();
	}
}

the ordering is 'UNORDERED' and the project throws an exception requiring terminal operations.

If the test case is

class A {

	@EntryPoint
	void n() {
		Stream<Object> s = new HashSet<>().stream();;
		s.sorted().distinct().count();
	}
}

the ordering is 'ORDERED' and the project doesn't throw an exception.

Then, I found a sentence from https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html :
"When the terminal operation is initiated, the stream pipeline is executed sequentially or in parallel depending on the orientation of the stream on which it is invoked."

I think the project doesn't find the terminal operations, so the stream pipeline has never been executed.

Copy link
Member

Choose a reason for hiding this comment

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

If you replace HashSet() with ArrayList, is it still UNORDERED? That would answer whether it is just accepting a default value.

Copy link
Member

Choose a reason for hiding this comment

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

Do you have any insight as to why it is not finding the terminal operation? Do you have ideas how to fix it? Let's discuss it prior to executing a particular strategy.

Copy link
Contributor Author

@yiming-tang-cs yiming-tang-cs Dec 1, 2017

Choose a reason for hiding this comment

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

If the test case is:

class A {

	Stream<Object> m() {
		Stream<Object> stream = new ArrayList<>().stream().sorted();
		return stream;
	}

	@EntryPoint
	void n() {
		Stream<Object> s = m();
		s.distinct().count();
	}
}

the ordering is 'Ordered' and the project throws an exception (require terminal operations).

image

If you replace HashSet() with ArrayList, is it still UNORDERED? That would answer whether it is just accepting a default value.

According to the test result above, the answer should be yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have any insight as to why it is not finding the terminal operation? Do you have ideas how to fix it? Let's discuss it prior to executing a particular strategy.

I came up with a question in #103 . I found the instance of stream just processes the operations in the method of enclosingMethodDeclaration.

For the example below:

class A {

	Stream<Object> m() {
		Stream<Object> stream = new HashSet<>().parallelStream();
		return stream;
	}

	@EntryPoint
	void n() {
		Stream<Object> s = m();
		s.count();
	}
}

Do we need to create an instance of stream for n()? Can we reuse the instance of stream for m()? Those are what I am thinking.

@khatchad khatchad self-assigned this Dec 1, 2017
Copy link
Member

@khatchad khatchad left a comment

Choose a reason for hiding this comment

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

No variation of execution modes in a single example.


public void testNonInternalAPI4() throws Exception {
helper(new StreamAnalysisExpectedResult("new HashSet<>().stream()",
Collections.singleton(ExecutionMode.SEQUENTIAL), Collections.singleton(Ordering.UNORDERED), false, false,
Copy link
Member

Choose a reason for hiding this comment

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

If you replace HashSet() with ArrayList, is it still UNORDERED? That would answer whether it is just accepting a default value.


public void testNonInternalAPI4() throws Exception {
helper(new StreamAnalysisExpectedResult("new HashSet<>().stream()",
Collections.singleton(ExecutionMode.SEQUENTIAL), Collections.singleton(Ordering.UNORDERED), false, false,
Copy link
Member

Choose a reason for hiding this comment

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

Do you have any insight as to why it is not finding the terminal operation? Do you have ideas how to fix it? Let's discuss it prior to executing a particular strategy.

class A {

Stream<Object> m() {
Stream<Object> stream = new HashSet<>().parallelStream().sorted();
Copy link
Member

Choose a reason for hiding this comment

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

I fail to see how this is varying the execution order. Particularly, I should see a call to either parallel() or sequential() rather than something like sorted().

Copy link
Contributor Author

@yiming-tang-cs yiming-tang-cs Dec 1, 2017

Choose a reason for hiding this comment

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

Please add one more test case similar to testNonInternalAPI4() but varying execution modes.

Is .parallelStream() not enough?
sorted() influences ordering and .parallelStream() influences execution mode. You want to see different execution mode or ordering?

@khatchad
Copy link
Member

khatchad commented Dec 1, 2017 via email

@khatchad
Copy link
Member

khatchad commented Dec 1, 2017 via email

@yiming-tang-cs
Copy link
Contributor Author

yiming-tang-cs commented Dec 1, 2017

Do you mean an instance of edu.cuny.hunter.streamrefactoring.core.analysis.Stream?

Yes.
For the example below:

class A {

	Stream<Object> m() {
		Stream<Object> stream = new ArrayList<>().stream().sorted();
		return stream;
	}

	@EntryPoint
	void n() {
		Stream<Object> s = m();
		s.distinct().count();
	}
}

I found there is only one instance of Stream. What this instance does is limited to one method m(). I did not find any instance of Stream to operate n().

@khatchad
Copy link
Member

khatchad commented Dec 1, 2017 via email

@yiming-tang-cs
Copy link
Contributor Author

Because there is only one instance of edu.cuny.hunter.streamrefactoring.core.analysis.Stream, the method new StreamStateMachine(this).start(); in edu.cuny.hunter.streamrefactoring.core.analysis.Stream only processes one method in test case, i.e., only processes the method where the stream is created in the test case. For the test case above, it only processes m() ( stream is created in m).

What I am doing:
After initializing the stream, e.g. calling this.inferInitialExecution(); in edu.cuny.hunter.streamrefactoring.core.analysis.Stream, I check whether the current method(where the stream is created) in test case returns a stream (I finished this part). If no, I will not change anything. If yes, I want to change the value the enclosingMethodDeclaration before calling new StreamStateMachine(this).start();. I want the initialized stream with the new value of enclosingMethodDeclaration as a new input for StreamStateMachine(this).

@yiming-tang-cs
Copy link
Contributor Author

Just changing the value of enclosingMethodDeclaration cannot work. Too many low level implementations are needed to change.

@khatchad
Copy link
Member

khatchad commented Dec 2, 2017 via email

@khatchad
Copy link
Member

khatchad commented Dec 2, 2017 via email

@khatchad
Copy link
Member

khatchad commented Dec 2, 2017 via email

@khatchad
Copy link
Member

khatchad commented Dec 2, 2017 via email

@khatchad
Copy link
Member

khatchad commented Dec 2, 2017 via email

@yiming-tang-cs
Copy link
Contributor Author

Changing call sites can work. I did not find a method to examine the call sites for all method, so I examine the call sites for the methods in the context of enclosing method and enclosing method itself (this may be smarter). The new pull request also needs a new pull request for WALA.

@khatchad
Copy link
Member

khatchad commented Dec 5, 2017

Changing call sites can work. I did not find a method to examine the call sites for all method, so I examine the call sites for the methods in the context of enclosing method and enclosing method itself (this may be smarter).

I think that it is better to have the functionality and design correct and in agreement, respectively, prior to "being smarter." Once we are sure of the former two, we can think of ways to improve.

@@ -636,6 +637,11 @@ private void inferInitialOrdering()
*/
protected CGNode getEnclosingMethodNode() throws IOException, CoreException, NoEnclosingMethodNodeFoundException {
MethodReference methodReference = this.getEnclosingMethodReference();
return getEnclosingMethodNode(methodReference);
Copy link
Member

Choose a reason for hiding this comment

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

Per this comment, I advised against altering getEnclosingMethodNode().

return getEnclosingMethodNode(methodReference);
}

protected CGNode getEnclosingMethodNode(MethodReference methodReference)
Copy link
Member

Choose a reason for hiding this comment

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

This parameter makes no sense and has not associated documentation.

@@ -644,6 +650,19 @@ protected CGNode getEnclosingMethodNode() throws IOException, CoreException, NoE
return nodes.iterator().next(); // just return the first.
}

protected HashSet<CGNode> getEnclosingMethodNodes()
Copy link
Member

Choose a reason for hiding this comment

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

Because portions of our call graph are context sensitive, there may be more than one node that represents a single method. But, they are basically the same and are only used methods returning streams. The only difference is the calling context. I don't see any documentation on why all methods representing the enclosing method are needed. Furthermore, there was no internal discussion of this strategy.

+ " that was originally: " + previousReceivers;

++processedInstructions;
// CGNode cgNode = this.getStream().getEnclosingMethodNode();
Copy link
Member

Choose a reason for hiding this comment

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

I will not accept commented code.


++processedInstructions;
// CGNode cgNode = this.getStream().getEnclosingMethodNode();
for (CGNode cgNode : this.getStream().getEnclosingMethodNodes()) {
Copy link
Member

Choose a reason for hiding this comment

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

Why are these diff blocks so large? I cannot see what has changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just move the block into a loop and change its format by shift + ctrl + f。


++processedInstructions;
// CGNode cgNode = this.getStream().getEnclosingMethodNode();
for (CGNode cgNode : this.getStream().getEnclosingMethodNodes()) {
Copy link
Member

Choose a reason for hiding this comment

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

I am unsure of:

  1. What the collection of enclosing method nodes represents in this context, and
  2. why there are needed.

In other words, there is a lot of documentation missing here.


++processedInstructions;
// CGNode cgNode = this.getStream().getEnclosingMethodNode();
for (CGNode cgNode : this.getStream().getEnclosingMethodNodes()) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not, for starters, iterate over all nodes in the graph? If that can cover all cases (i.e., returning streams, passing streams, and storing streams into fields), and we can create proper unit tests for that functionality, we can then think about how to reduce the search space while still maintaining correctness.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the last weekend, I tried to iterate over all nodes, but got a new exception, so I tried the current strategy to see whether changing call sites can work. However, you are right. Although the current strategy may be smarter (it just iterates over the necessary node), it needs much more time to check whether the strategy are absolutely right. Hence, I will follow your advice.

@khatchad
Copy link
Member

khatchad commented Dec 6, 2017 via email

Copy link
Member

@khatchad khatchad left a comment

Choose a reason for hiding this comment

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

Besides the comments I've entered, I am still not seeing test cases corresponding to passing Streams as parameters and methods communicating through a field (i.e., one method writes to a stream field and the other reads it).

* @throws CoreException
* @throws NoEnclosingMethodNodeFoundException
*/
protected HashSet<CGNode> getCGNodesInGraph() throws IOException, CoreException {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just use the iterator?

cgNodes.add(cgNodeIterator.next());
}

return cgNodes;
Copy link
Member

Choose a reason for hiding this comment

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

Call graphs can be massive. We don't want an extra step here of adding them to a set if it is not necessary.

+ " that was originally: " + previousReceivers;

++processedInstructions;
for (CGNode cgNode : this.getStream().getCGNodesInGraph()) {
Copy link
Member

Choose a reason for hiding this comment

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

You would use the iterator directly here.

@khatchad
Copy link
Member

khatchad commented Dec 6, 2017

I am also curious about the additional run time to examine each node in the call graph. I'm especially interested in tests that don't need it. I am wondering:

  1. What is the running time of a single test case that doesn't need interprocedural analysis without the change.
  2. What is the running time of the same test with the change.

Please report on these.

@yiming-tang-cs
Copy link
Contributor Author

Besides the comments I've entered, I am still not seeing test cases corresponding to passing Streams as parameters and methods communicating through a field (i.e., one method writes to a stream field and the other reads it).

Do you mean :

class A {

	Stream<Object> m() {
		Stream<Object> stream = new HashSet<>().stream().parallel();
		return stream;
	}

	@EntryPoint
	void n(Stream<Object> s) {
		s.distinct().count();
	}
	
	void mn() {
		n(m());
	}
}

@yiming-tang-cs
Copy link
Contributor Author

What is the running time of a single test case that doesn't need interprocedural analysis without the change.
What is the running time of the same test with the change.
Please report on these.

image

@yiming-tang-cs
Copy link
Contributor Author

  With changes Without changes
TestStreamOf 9.357 9.963
testGenerate 2.99 3.3
testTypeResolution2 2.549 3.799
testHashSetParallelStream2 107.15 102.758
testArrayAsList 0.732 0.581
testStaticInitializer 0.112 0.131
testIntStreamGenerate 1.467 1.594
testDoubleStreamOf 1.344 1.192
testArrayStream 0.625 0.603
testTypeResolution 0.548 0.467
testMotivatingExample 404.412 408.822
testLongSteamOf 1.147 1.226
testMultipleCallsToEnclosingMethod 1.027 1.262
testBitSet 0.936 0.856
testHashSetParallelStream 0.493 0.429
testIntermediateOperations 105.978 107.039
testTerminalOp1 1.459 1.55
testTerminalOp2 1.564 1.565
testTerminalOp3 0.577 0.471
testCollectionFromParameter2 90.784 88.962
testCollectionFromParameter3 86.142 88.893
testCollectionFromParameter4 0.425 0.339
testCollectionFromParameter 86.487 91.936
testIntStreamOf 1.206 1.228

@khatchad khatchad changed the title test cases for #103 Fix for #103 Dec 8, 2017
Copy link
Member

@khatchad khatchad left a comment

Choose a reason for hiding this comment

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

There is some strangeness in the test code. Please investigate.


helper(new StreamAnalysisExpectedResult("new HashSet<>().stream()",
Collections.singleton(ExecutionMode.SEQUENTIAL), orderings, false, true, false, null, null, null,
RefactoringStatus.ERROR, EnumSet.of(PreconditionFailure.INCONSISTENT_POSSIBLE_ORDERINGS)));
Copy link
Member

Choose a reason for hiding this comment

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

Why inconsistent orderings?

Copy link
Member

Choose a reason for hiding this comment

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

Is that supposed to be the case? Please investigate.

Copy link
Member

Choose a reason for hiding this comment

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

@saledouble Any progress on this?

public void testNonInternalAPI5() throws Exception {
HashSet<ExecutionMode> executionModes = new HashSet<>();
executionModes .add(ExecutionMode.PARALLEL);
executionModes .add(ExecutionMode.SEQUENTIAL);
Copy link
Member

Choose a reason for hiding this comment

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

@saledouble There's also a similar problem here. Do we have a bug?

@yiming-tang-cs
Copy link
Contributor Author

@yiming-tang-cs
Copy link
Contributor Author

@khatchad
Copy link
Member

khatchad commented Dec 8, 2017 via email

@yiming-tang-cs
Copy link
Contributor Author

Interesting, thanks for the info. I think we have had a bug related to this recently but I thought it was fixed. Can you find it in the bug list? It might be closed.

It may be relevant to to #62

@khatchad
Copy link
Member

khatchad commented Dec 8, 2017 via email


++processedInstructions;
// CGNode cgNode = this.getStream().getEnclosingMethodNode();
for (CGNode cgNode : this.getStream().getEnclosingMethodNodes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the last weekend, I tried to iterate over all nodes, but got a new exception, so I tried the current strategy to see whether changing call sites can work. However, you are right. Although the current strategy may be smarter (it just iterates over the necessary node), it needs much more time to check whether the strategy are absolutely right. Hence, I will follow your advice.

@@ -281,114 +280,115 @@ public void start() throws IOException, CoreException, CallGraphBuilderCancelExc
// the node of where the stream was declared: TODO: Can this be
// somehow rewritten to get blocks corresponding to terminal
// operations?
CGNode cgNode = this.getStream().getEnclosingMethodNode();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just add a FOR statement. It changes the format of the block in the loop, so red block is large.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The loop is from line 283 to line 396.

throws IOException, CoreException, NoEnclosingMethodNodeFoundException {
HashSet<CGNode> cgNodes = new HashSet<>();

Iterator<CGNode> cgNodeIterator = this.getAnalysisEngine().getCallGraph().iterator();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Finally, I found a easy way to get CGNodes in the call graph. I tried a complex way in the weekend and got an exception.

@khatchad
Copy link
Member

khatchad commented Dec 9, 2017 via email

@khatchad khatchad merged commit 6ea3f22 into ponder-lab:master Dec 16, 2017
@yiming-tang-cs yiming-tang-cs deleted the issue_103_new branch December 20, 2017 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants