From 1ce597bb9c13cc9b403da355b48e2188344fed71 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 6 Nov 2014 16:47:23 +0100 Subject: [PATCH] SONAR-5672 Decrease memory comsumption when working on very big DSM with empty cells --- .../org/sonar/batch/design/DsmSerializer.java | 4 ++-- .../src/main/java/org/sonar/graph/Dsm.java | 23 +++++++++++++++---- .../main/java/org/sonar/graph/DsmPrinter.java | 9 ++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/design/DsmSerializer.java b/sonar-batch/src/main/java/org/sonar/batch/design/DsmSerializer.java index 48da21bd0c2..039921a94cf 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/design/DsmSerializer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/design/DsmSerializer.java @@ -74,9 +74,9 @@ public final class DsmSerializer { } private void serializeCell(int y, int x) { - DsmCell cell = dsm.getCell(x, y); + DsmCell cell = dsm.cell(x, y); json.append('{'); - if (cell.getEdge() != null && cell.getWeight() > 0) { + if (cell != null && cell.getEdge() != null && cell.getWeight() > 0) { Dependency dep = (Dependency) cell.getEdge(); json.append("\"i\":"); json.append(dep.getId()); diff --git a/sonar-graph/src/main/java/org/sonar/graph/Dsm.java b/sonar-graph/src/main/java/org/sonar/graph/Dsm.java index 972b44bd1e2..fc9f386fff2 100644 --- a/sonar-graph/src/main/java/org/sonar/graph/Dsm.java +++ b/sonar-graph/src/main/java/org/sonar/graph/Dsm.java @@ -19,6 +19,8 @@ */ package org.sonar.graph; +import javax.annotation.CheckForNull; + import java.util.Collection; import java.util.Collections; import java.util.Set; @@ -53,9 +55,10 @@ public class Dsm { V to = vertices[y]; Edge edge = graph.getEdge(from, to); - boolean isFeedbackEdge = edge != null && feedbackEdges.contains(edge); - DsmCell cell = new DsmCell(edge, isFeedbackEdge); - cells[x][y] = cell; + if (edge != null) { + boolean isFeedbackEdge = feedbackEdges.contains(edge); + cells[x][y] = new DsmCell(edge, isFeedbackEdge); + } } } } @@ -129,7 +132,7 @@ public class Dsm { int incomingEdges = 0; for (int x = from; x <= to; x++) { DsmCell cell = cells[x][y]; - if (cell.getWeight() != 0 && !cell.isFeedbackEdge()) { + if (cell != null && cell.getWeight() != 0 && !cell.isFeedbackEdge()) { incomingEdges++; } } @@ -140,14 +143,24 @@ public class Dsm { int outgoingEdges = 0; for (int y = from; y <= to; y++) { DsmCell cell = cells[x][y]; - if (cell.getWeight() != 0 && !cell.isFeedbackEdge()) { + if (cell != null && cell.getWeight() != 0 && !cell.isFeedbackEdge()) { outgoingEdges++; } } return outgoingEdges; } + /** + * @deprecated since 5.0 use {@link #cell(int, int)} + */ + @Deprecated public DsmCell getCell(int x, int y) { + DsmCell cell = cells[x][y]; + return cell != null ? cell : new DsmCell(null, false); + } + + @CheckForNull + public DsmCell cell(int x, int y) { return cells[x][y]; } diff --git a/sonar-graph/src/main/java/org/sonar/graph/DsmPrinter.java b/sonar-graph/src/main/java/org/sonar/graph/DsmPrinter.java index 0676aed7ecb..5cdbd694829 100644 --- a/sonar-graph/src/main/java/org/sonar/graph/DsmPrinter.java +++ b/sonar-graph/src/main/java/org/sonar/graph/DsmPrinter.java @@ -46,7 +46,7 @@ public final class DsmPrinter { printRow(y); } writer.flush(); - + } catch (IOException e) { throw new RuntimeException("Unable to print the desired DSM.", e); // NOSONAR } @@ -76,12 +76,13 @@ public final class DsmPrinter { } private void printCell(int y, int x) throws IOException { - if (dsm.getCell(x, y).getWeight() == 0) { + DsmCell cell = dsm.cell(x, y); + if (cell == null || cell.getWeight() == 0) { writer.append(" "); } else { - writer.append("").append(String.valueOf(dsm.getCell(x, y).getWeight())); + writer.append("").append(String.valueOf(cell.getWeight())); } - if (dsm.getCell(x, y).isFeedbackEdge()) { + if (cell != null && cell.isFeedbackEdge()) { writer.append(FEEDBACK_EDGE_FLAG); } else { writer.append(' '); -- 2.39.5