{"action":"created","comment":{"url":"https://api.github.com/repos/smarr/SOMns/pulls/comments/145068743","pull_request_review_id":69811735,"id":145068743,"diff_hunk":"@@ -0,0 +1,164 @@\n+package tools.dym.superinstructions;\n+\n+import java.util.Arrays;\n+import java.util.Comparator;\n+import java.util.HashMap;\n+import java.util.HashSet;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.Set;\n+import java.util.stream.Collectors;\n+import java.util.stream.Stream;\n+\n+import som.interpreter.nodes.SequenceNode;\n+\n+\n+/**\n+ * Given a map mapping activation contexts to activation counts,\n+ * create a list of super-instruction candidates.\n+ */\n+public class CandidateDetector {\n+  private static final int             CONSIDER_TOP_CONTEXTS = 100;\n+  private Map<ActivationContext, Long> contexts;\n+\n+  public CandidateDetector(final Map<ActivationContext, Long> contexts) {\n+    this.contexts = contexts;\n+  }\n+\n+  /**\n+   * Return a stream of ActivationContext objects whose traces begin with prefix.\n+   */\n+  private Stream<ActivationContext> contextsWithPrefix(final Object[] prefix) {\n+    return contexts.keySet().stream()\n+                   .filter(ctx -> ctx.traceStartsWith(prefix));\n+  }\n+\n+  /**\n+   * Given a prefix\n+   * [ C_0, s_0, ..., s_{n-1}, C_n ]\n+   * this returns a map mapping each s_n to an activation context with a trace\n+   * [ C_0, s_0, ..., s_{n-1}, C_n, s_n, C_{n+1} ]\n+   * for which the number of activations is maximal.\n+   */\n+  private Map<Integer, ActivationContext> findExtensions(final Object[] prefix) {\n+    Map<Integer, ActivationContext> result = new HashMap<>();\n+    // Find all activation contexts which extend prefix\n+    // with one child slot index and one class name\n+    Set<ActivationContext> extensions =\n+        contextsWithPrefix(prefix).filter(ctx -> ctx.getTrace().length == prefix.length + 2)\n+                                  .collect(Collectors.toSet());\n+    // Extract all possible values of s_n\n+    Set<Integer> childIndices = extensions.stream()\n+                                          .map(ActivationContext::getLeafChildIndex)\n+                                          .collect(Collectors.toSet());\n+    // Handle each s_n separately ...\n+    for (int childIndex : childIndices) {\n+      // Get all extensions for which s_n == childIndex,\n+      // get the extension with the most activations, put it into the map\n+      ActivationContext extension =\n+          extensions.stream().filter(ctx -> ctx.getLeafChildIndex() == childIndex)\n+                    .max(Comparator.comparingLong(ctx -> contexts.get(ctx)))\n+                    .orElseThrow(() -> new RuntimeException(\"No suitable alternatives\"));\n+      result.put(childIndex, extension);\n+    }\n+    return result;\n+  }\n+\n+  /**\n+   * Given an activation context, construct a candidate for a super-instruction\n+   * and return it.\n+   */\n+  private Candidate constructCandidate(final ActivationContext currentContext) {\n+    assert currentContext.getNumberOfClasses() == 3;\n+    // currentContext has a trace:\n+    // [C_0, s_0, C_1, s_1, C_2]\n+    // We now find possible \"piblings\", i.e. siblings of its parent.\n+    // For that, we invoke findExtensions on the prefix\n+    // [C_0]\n+    Map<Integer, ActivationContext> piblings = findExtensions(\n+        new Object[] {\n+            currentContext.getClass(0)\n+        });\n+    // Also, we find possible siblings. For that, we invoke findExtensions\n+    // on the prefix\n+    // [C_0, s_0, C_1]\n+    Map<Integer, ActivationContext> siblings = findExtensions(\n+        new Object[] {\n+            currentContext.getClass(0),\n+            currentContext.getChildIndex(0),\n+            currentContext.getClass(1)\n+        });\n+    // We now construct a super-instruction candidate, i.e.\n+    // a tree of height 2. The root of the tree is C_0 (its Java type is unknown).\n+    Candidate candidate = new Candidate(currentContext.getClass(0), \"?\");\n+    // Now, we add the children of C_0, i.e. the siblings of C_1 and C_1 itself.\n+    for (int piblingSlot : piblings.keySet()) {\n+      if (piblingSlot == currentContext.getChildIndex(0)) {\n+        // This is C_1. We add it to the candidate and proceed\n+        // with adding the siblings of C_2 and C_2 itself.\n+        Candidate.Node child =\n+            candidate.getRoot().setChild(piblingSlot, currentContext.getClass(1), \"?\");\n+        for (int siblingSlot : siblings.keySet()) {\n+          if (siblingSlot == currentContext.getChildIndex(1)) {\n+            // Add C_2\n+            child.setChild(siblingSlot,\n+                currentContext.getClass(2),\n+                currentContext.getJavaType());\n+          } else {\n+            // Add a sibling of C_2\n+            ActivationContext sibling = siblings.get(siblingSlot);\n+            child.setChild(siblingSlot,\n+                sibling.getClass(2),\n+                sibling.getJavaType());\n+          }\n+        }\n+      } else {\n+        ActivationContext pibling = piblings.get(piblingSlot);\n+        // Add a sibling of C_1.\n+        assert pibling.getNumberOfClasses() == 2;\n+        candidate.getRoot().setChild(piblingSlot,\n+            pibling.getClass(1),\n+            pibling.getJavaType());\n+      }\n+    }\n+    // The score of the super-instruction candidate corresponds to the number\n+    // of activations of the current context.\n+    candidate.setScore(contexts.get(currentContext));\n+    return candidate;\n+  }\n+\n+  /**\n+   * Run the candidate detection heuristic and return a report as a String.\n+   */\n+  public String detect() {\n+    // Find all activation contexts with traces of length n = 2,\n+    // exclude traces containing SequenceNodes (they do not yield\n+    // viable candidates for super-instructions),\n+    // sort them by activation counts in descending order,\n+    // fetch the top CONSIDER_TOP_CONTEXTS activation contexts\n+    // and construct a candidate for each of them.\n+    List<ActivationContext> sorted =\n+        contexts.keySet().stream().filter(context -> context.getNumberOfClasses() == 3)","path":"src/tools/dym/superinstructions/CandidateDetector.java","position":141,"original_position":141,"commit_id":"9d818b173d9123ffed91e54188aef2799164ac77","original_commit_id":"9d818b173d9123ffed91e54188aef2799164ac77","user":{"login":"codacy-bot","id":19940114,"avatar_url":"https://avatars1.githubusercontent.com/u/19940114?v=4","gravatar_id":"","url":"https://api.github.com/users/codacy-bot","html_url":"https://github.com/codacy-bot","followers_url":"https://api.github.com/users/codacy-bot/followers","following_url":"https://api.github.com/users/codacy-bot/following{/other_user}","gists_url":"https://api.github.com/users/codacy-bot/gists{/gist_id}","starred_url":"https://api.github.com/users/codacy-bot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codacy-bot/subscriptions","organizations_url":"https://api.github.com/users/codacy-bot/orgs","repos_url":"https://api.github.com/users/codacy-bot/repos","events_url":"https://api.github.com/users/codacy-bot/events{/privacy}","received_events_url":"https://api.github.com/users/codacy-bot/received_events","type":"User","site_admin":false},"body":"![Codacy](https://www.codacy.com/assets/images/favicon.png) Issue found: ['3' is a magic number.](https://www.codacy.com/app/smarr/SOMns/pullRequest?prid=993298)","created_at":"2017-10-17T09:03:39Z","updated_at":"2017-10-17T09:03:39Z","html_url":"https://github.com/smarr/SOMns/pull/192#discussion_r145068743","pull_request_url":"https://api.github.com/repos/smarr/SOMns/pulls/192","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/smarr/SOMns/pulls/comments/145068743"},"html":{"href":"https://github.com/smarr/SOMns/pull/192#discussion_r145068743"},"pull_request":{"href":"https://api.github.com/repos/smarr/SOMns/pulls/192"}}},"pull_request":{"url":"https://api.github.com/repos/smarr/SOMns/pulls/192","id":146729983,"html_url":"https://github.com/smarr/SOMns/pull/192","diff_url":"https://github.com/smarr/SOMns/pull/192.diff","patch_url":"https://github.com/smarr/SOMns/pull/192.patch","issue_url":"https://api.github.com/repos/smarr/SOMns/issues/192","number":192,"state":"open","locked":false,"title":"Add tool to identify candidates for super-instructions","user":{"login":"smarr","id":40815,"avatar_url":"https://avatars0.githubusercontent.com/u/40815?v=4","gravatar_id":"","url":"https://api.github.com/users/smarr","html_url":"https://github.com/smarr","followers_url":"https://api.github.com/users/smarr/followers","following_url":"https://api.github.com/users/smarr/following{/other_user}","gists_url":"https://api.github.com/users/smarr/gists{/gist_id}","starred_url":"https://api.github.com/users/smarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarr/subscriptions","organizations_url":"https://api.github.com/users/smarr/orgs","repos_url":"https://api.github.com/users/smarr/repos","events_url":"https://api.github.com/users/smarr/events{/privacy}","received_events_url":"https://api.github.com/users/smarr/received_events","type":"User","site_admin":false},"body":"This PR adds a new Truffle tool to instrument all nodes, track their execution, and determine node configurations that are frequently used together.\r\nThese node configurations might benefit from being converted into a single node that reduces the interpretation overhead.\r\n\r\nThis is work by @fredreichbier.\r\n\r\n@fredreichbier, could you please review the PR, and check whether everything looks ok for you?\r\n\r\nThis is a first version, I might still do some more cosmetic changes.","created_at":"2017-10-16T09:00:32Z","updated_at":"2017-10-17T09:03:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"efd2db4928df590680ba9572b9069f48a5f5f2b0","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":{"url":"https://api.github.com/repos/smarr/SOMns/milestones/7","html_url":"https://github.com/smarr/SOMns/milestone/7","labels_url":"https://api.github.com/repos/smarr/SOMns/milestones/7/labels","id":2684834,"number":7,"title":"v0.6.0 - Black Diamonds","description":"The main goal of the next milestone is to move common code between TruffleSOM, TruffleMate, and SOMns into a new common project.\r\n\r\nSee https://github.com/SOM-st/black-diamonds","creator":{"login":"smarr","id":40815,"avatar_url":"https://avatars0.githubusercontent.com/u/40815?v=4","gravatar_id":"","url":"https://api.github.com/users/smarr","html_url":"https://github.com/smarr","followers_url":"https://api.github.com/users/smarr/followers","following_url":"https://api.github.com/users/smarr/following{/other_user}","gists_url":"https://api.github.com/users/smarr/gists{/gist_id}","starred_url":"https://api.github.com/users/smarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarr/subscriptions","organizations_url":"https://api.github.com/users/smarr/orgs","repos_url":"https://api.github.com/users/smarr/repos","events_url":"https://api.github.com/users/smarr/events{/privacy}","received_events_url":"https://api.github.com/users/smarr/received_events","type":"User","site_admin":false},"open_issues":4,"closed_issues":4,"state":"open","created_at":"2017-08-07T09:52:50Z","updated_at":"2017-10-16T09:00:32Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/smarr/SOMns/pulls/192/commits","review_comments_url":"https://api.github.com/repos/smarr/SOMns/pulls/192/comments","review_comment_url":"https://api.github.com/repos/smarr/SOMns/pulls/comments{/number}","comments_url":"https://api.github.com/repos/smarr/SOMns/issues/192/comments","statuses_url":"https://api.github.com/repos/smarr/SOMns/statuses/9d818b173d9123ffed91e54188aef2799164ac77","head":{"label":"smarr:super-instruction-detector","ref":"super-instruction-detector","sha":"9d818b173d9123ffed91e54188aef2799164ac77","user":{"login":"smarr","id":40815,"avatar_url":"https://avatars0.githubusercontent.com/u/40815?v=4","gravatar_id":"","url":"https://api.github.com/users/smarr","html_url":"https://github.com/smarr","followers_url":"https://api.github.com/users/smarr/followers","following_url":"https://api.github.com/users/smarr/following{/other_user}","gists_url":"https://api.github.com/users/smarr/gists{/gist_id}","starred_url":"https://api.github.com/users/smarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarr/subscriptions","organizations_url":"https://api.github.com/users/smarr/orgs","repos_url":"https://api.github.com/users/smarr/repos","events_url":"https://api.github.com/users/smarr/events{/privacy}","received_events_url":"https://api.github.com/users/smarr/received_events","type":"User","site_admin":false},"repo":{"id":35489534,"name":"SOMns","full_name":"smarr/SOMns","owner":{"login":"smarr","id":40815,"avatar_url":"https://avatars0.githubusercontent.com/u/40815?v=4","gravatar_id":"","url":"https://api.github.com/users/smarr","html_url":"https://github.com/smarr","followers_url":"https://api.github.com/users/smarr/followers","following_url":"https://api.github.com/users/smarr/following{/other_user}","gists_url":"https://api.github.com/users/smarr/gists{/gist_id}","starred_url":"https://api.github.com/users/smarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarr/subscriptions","organizations_url":"https://api.github.com/users/smarr/orgs","repos_url":"https://api.github.com/users/smarr/repos","events_url":"https://api.github.com/users/smarr/events{/privacy}","received_events_url":"https://api.github.com/users/smarr/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/smarr/SOMns","description":"SOMns: A Newspeak for Concurrency Research","fork":false,"url":"https://api.github.com/repos/smarr/SOMns","forks_url":"https://api.github.com/repos/smarr/SOMns/forks","keys_url":"https://api.github.com/repos/smarr/SOMns/keys{/key_id}","collaborators_url":"https://api.github.com/repos/smarr/SOMns/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/smarr/SOMns/teams","hooks_url":"https://api.github.com/repos/smarr/SOMns/hooks","issue_events_url":"https://api.github.com/repos/smarr/SOMns/issues/events{/number}","events_url":"https://api.github.com/repos/smarr/SOMns/events","assignees_url":"https://api.github.com/repos/smarr/SOMns/assignees{/user}","branches_url":"https://api.github.com/repos/smarr/SOMns/branches{/branch}","tags_url":"https://api.github.com/repos/smarr/SOMns/tags","blobs_url":"https://api.github.com/repos/smarr/SOMns/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/smarr/SOMns/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/smarr/SOMns/git/refs{/sha}","trees_url":"https://api.github.com/repos/smarr/SOMns/git/trees{/sha}","statuses_url":"https://api.github.com/repos/smarr/SOMns/statuses/{sha}","languages_url":"https://api.github.com/repos/smarr/SOMns/languages","stargazers_url":"https://api.github.com/repos/smarr/SOMns/stargazers","contributors_url":"https://api.github.com/repos/smarr/SOMns/contributors","subscribers_url":"https://api.github.com/repos/smarr/SOMns/subscribers","subscription_url":"https://api.github.com/repos/smarr/SOMns/subscription","commits_url":"https://api.github.com/repos/smarr/SOMns/commits{/sha}","git_commits_url":"https://api.github.com/repos/smarr/SOMns/git/commits{/sha}","comments_url":"https://api.github.com/repos/smarr/SOMns/comments{/number}","issue_comment_url":"https://api.github.com/repos/smarr/SOMns/issues/comments{/number}","contents_url":"https://api.github.com/repos/smarr/SOMns/contents/{+path}","compare_url":"https://api.github.com/repos/smarr/SOMns/compare/{base}...{head}","merges_url":"https://api.github.com/repos/smarr/SOMns/merges","archive_url":"https://api.github.com/repos/smarr/SOMns/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/smarr/SOMns/downloads","issues_url":"https://api.github.com/repos/smarr/SOMns/issues{/number}","pulls_url":"https://api.github.com/repos/smarr/SOMns/pulls{/number}","milestones_url":"https://api.github.com/repos/smarr/SOMns/milestones{/number}","notifications_url":"https://api.github.com/repos/smarr/SOMns/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/smarr/SOMns/labels{/name}","releases_url":"https://api.github.com/repos/smarr/SOMns/releases{/id}","deployments_url":"https://api.github.com/repos/smarr/SOMns/deployments","created_at":"2015-05-12T13:27:56Z","updated_at":"2017-08-22T16:28:10Z","pushed_at":"2017-10-17T08:59:57Z","git_url":"git://github.com/smarr/SOMns.git","ssh_url":"git@github.com:smarr/SOMns.git","clone_url":"https://github.com/smarr/SOMns.git","svn_url":"https://github.com/smarr/SOMns","homepage":"","size":10223,"stargazers_count":19,"watchers_count":19,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":9,"mirror_url":null,"open_issues_count":33,"forks":9,"open_issues":33,"watchers":19,"default_branch":"master"}},"base":{"label":"smarr:dev","ref":"dev","sha":"c3e83813f3b7c98fd517cea90d0c7b4aa416b27b","user":{"login":"smarr","id":40815,"avatar_url":"https://avatars0.githubusercontent.com/u/40815?v=4","gravatar_id":"","url":"https://api.github.com/users/smarr","html_url":"https://github.com/smarr","followers_url":"https://api.github.com/users/smarr/followers","following_url":"https://api.github.com/users/smarr/following{/other_user}","gists_url":"https://api.github.com/users/smarr/gists{/gist_id}","starred_url":"https://api.github.com/users/smarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarr/subscriptions","organizations_url":"https://api.github.com/users/smarr/orgs","repos_url":"https://api.github.com/users/smarr/repos","events_url":"https://api.github.com/users/smarr/events{/privacy}","received_events_url":"https://api.github.com/users/smarr/received_events","type":"User","site_admin":false},"repo":{"id":35489534,"name":"SOMns","full_name":"smarr/SOMns","owner":{"login":"smarr","id":40815,"avatar_url":"https://avatars0.githubusercontent.com/u/40815?v=4","gravatar_id":"","url":"https://api.github.com/users/smarr","html_url":"https://github.com/smarr","followers_url":"https://api.github.com/users/smarr/followers","following_url":"https://api.github.com/users/smarr/following{/other_user}","gists_url":"https://api.github.com/users/smarr/gists{/gist_id}","starred_url":"https://api.github.com/users/smarr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarr/subscriptions","organizations_url":"https://api.github.com/users/smarr/orgs","repos_url":"https://api.github.com/users/smarr/repos","events_url":"https://api.github.com/users/smarr/events{/privacy}","received_events_url":"https://api.github.com/users/smarr/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/smarr/SOMns","description":"SOMns: A Newspeak for Concurrency Research","fork":false,"url":"https://api.github.com/repos/smarr/SOMns","forks_url":"https://api.github.com/repos/smarr/SOMns/forks","keys_url":"https://api.github.com/repos/smarr/SOMns/keys{/key_id}","collaborators_url":"https://api.github.com/repos/smarr/SOMns/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/smarr/SOMns/teams","hooks_url":"https://api.github.com/repos/smarr/SOMns/hooks","issue_events_url":"https://api.github.com/repos/smarr/SOMns/issues/events{/number}","events_url":"https://api.github.com/repos/smarr/SOMns/events","assignees_url":"https://api.github.com/repos/smarr/SOMns/assignees{/user}","branches_url":"https://api.github.com/repos/smarr/SOMns/branches{/branch}","tags_url":"https://api.github.com/repos/smarr/SOMns/tags","blobs_url":"https://api.github.com/repos/smarr/SOMns/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/smarr/SOMns/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/smarr/SOMns/git/refs{/sha}","trees_url":"https://api.github.com/repos/smarr/SOMns/git/trees{/sha}","statuses_url":"https://api.github.com/repos/smarr/SOMns/statuses/{sha}","languages_url":"https://api.github.com/repos/smarr/SOMns/languages","stargazers_url":"https://api.github.com/repos/smarr/SOMns/stargazers","contributors_url":"https://api.github.com/repos/smarr/SOMns/contributors","subscribers_url":"https://api.github.com/repos/smarr/SOMns/subscribers","subscription_url":"https://api.github.com/repos/smarr/SOMns/subscription","commits_url":"https://api.github.com/repos/smarr/SOMns/commits{/sha}","git_commits_url":"https://api.github.com/repos/smarr/SOMns/git/commits{/sha}","comments_url":"https://api.github.com/repos/smarr/SOMns/comments{/number}","issue_comment_url":"https://api.github.com/repos/smarr/SOMns/issues/comments{/number}","contents_url":"https://api.github.com/repos/smarr/SOMns/contents/{+path}","compare_url":"https://api.github.com/repos/smarr/SOMns/compare/{base}...{head}","merges_url":"https://api.github.com/repos/smarr/SOMns/merges","archive_url":"https://api.github.com/repos/smarr/SOMns/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/smarr/SOMns/downloads","issues_url":"https://api.github.com/repos/smarr/SOMns/issues{/number}","pulls_url":"https://api.github.com/repos/smarr/SOMns/pulls{/number}","milestones_url":"https://api.github.com/repos/smarr/SOMns/milestones{/number}","notifications_url":"https://api.github.com/repos/smarr/SOMns/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/smarr/SOMns/labels{/name}","releases_url":"https://api.github.com/repos/smarr/SOMns/releases{/id}","deployments_url":"https://api.github.com/repos/smarr/SOMns/deployments","created_at":"2015-05-12T13:27:56Z","updated_at":"2017-08-22T16:28:10Z","pushed_at":"2017-10-17T08:59:57Z","git_url":"git://github.com/smarr/SOMns.git","ssh_url":"git@github.com:smarr/SOMns.git","clone_url":"https://github.com/smarr/SOMns.git","svn_url":"https://github.com/smarr/SOMns","homepage":"","size":10223,"stargazers_count":19,"watchers_count":19,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":9,"mirror_url":null,"open_issues_count":33,"forks":9,"open_issues":33,"watchers":19,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/smarr/SOMns/pulls/192"},"html":{"href":"https://github.com/smarr/SOMns/pull/192"},"issue":{"href":"https://api.github.com/repos/smarr/SOMns/issues/192"},"comments":{"href":"https://api.github.com/repos/smarr/SOMns/issues/192/comments"},"review_comments":{"href":"https://api.github.com/repos/smarr/SOMns/pulls/192/comments"},"review_comment":{"href":"https://api.github.com/repos/smarr/SOMns/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/smarr/SOMns/pulls/192/commits"},"statuses":{"href":"https://api.github.com/repos/smarr/SOMns/statuses/9d818b173d9123ffed91e54188aef2799164ac77"}},"author_association":"OWNER"}}
{"actor":{"display_login":"codacy-bot"}}