Skip to content

Commit b25c4a7

Browse files
committed
[pinpoint-apm#12241] Apply Primitive Containers to optimize memory usage
1 parent c993651 commit b25c4a7

File tree

18 files changed

+317
-140
lines changed

18 files changed

+317
-140
lines changed

commons-server/src/main/java/com/navercorp/pinpoint/common/server/scatter/OneByteFuzzyRowKeyFactory.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.navercorp.pinpoint.common.server.scatter;
22

33

4+
import com.google.common.primitives.Longs;
45
import com.navercorp.pinpoint.common.server.util.pair.LongPair;
56
import org.springframework.util.backoff.BackOffExecution;
67
import org.springframework.util.backoff.ExponentialBackOff;
@@ -28,17 +29,9 @@ private long[] allocateSlot() {
2829
break;
2930
}
3031
}
31-
return toLongArray(backOffTimeList);
32+
return Longs.toArray(backOffTimeList);
3233
}
3334

34-
private long[] toLongArray(List<Long> list) {
35-
long[] buffer = new long[list.size()];
36-
for (int i = 0; i < list.size(); i++) {
37-
long time = list.get(i);
38-
buffer[i] = time;
39-
}
40-
return buffer;
41-
}
4235

4336
@Override
4437
public Byte getKey(long timeStamp) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.navercorp.pinpoint.common.server.util.array;
2+
3+
import com.google.common.primitives.Doubles;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.RandomAccess;
8+
import java.util.function.ToDoubleFunction;
9+
10+
public final class DoubleArray {
11+
12+
private DoubleArray() {
13+
}
14+
15+
public static double[] newArray(int size, double defaultValue) {
16+
double[] array = new double[size];
17+
Arrays.fill(array, defaultValue);
18+
return array;
19+
}
20+
21+
public static <T> List<Double> asList(List<T> list, ToDoubleFunction<T> function) {
22+
final int size = list.size();
23+
final double[] values = new double[size];
24+
if (list instanceof RandomAccess) {
25+
for (int i = 0; i < size; i++) {
26+
values[i] = function.applyAsDouble(list.get(i));
27+
}
28+
} else {
29+
int i = 0;
30+
for (T element : list) {
31+
values[i++] = function.applyAsDouble(element);
32+
}
33+
}
34+
return Doubles.asList(values);
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.navercorp.pinpoint.common.server.util.array;
2+
3+
import com.google.common.primitives.Ints;
4+
5+
import java.util.List;
6+
import java.util.RandomAccess;
7+
import java.util.function.ToIntFunction;
8+
9+
public final class IntArray {
10+
11+
private IntArray() {
12+
}
13+
14+
public static <T> List<Integer> asList(List<T> list, ToIntFunction<T> function) {
15+
final int size = list.size();
16+
final int[] values = new int[size];
17+
if (list instanceof RandomAccess) {
18+
for (int i = 0; i < size; i++) {
19+
values[i] = function.applyAsInt(list.get(i));
20+
}
21+
} else {
22+
int i = 0;
23+
for (T element : list) {
24+
values[i++] = function.applyAsInt(element);
25+
}
26+
}
27+
return Ints.asList(values);
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.navercorp.pinpoint.common.server.util.array;
2+
3+
import com.google.common.primitives.Longs;
4+
5+
import java.util.List;
6+
import java.util.RandomAccess;
7+
import java.util.function.ToLongFunction;
8+
9+
public final class LongArray {
10+
11+
private LongArray() {
12+
}
13+
14+
public static <T> List<Long> asList(List<T> list, ToLongFunction<T> function) {
15+
final int size = list.size();
16+
final long[] values = new long[size];
17+
if (list instanceof RandomAccess) {
18+
for (int i = 0; i < size; i++) {
19+
values[i] = function.applyAsLong(list.get(i));
20+
}
21+
} else {
22+
int i = 0;
23+
for (T element : list) {
24+
values[i++] = function.applyAsLong(element);
25+
}
26+
}
27+
return Longs.asList(values);
28+
}
29+
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.navercorp.pinpoint.common.server.util.array;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class DoubleArrayTest {
10+
@Test
11+
void asDoubleList() {
12+
DoubleRecord doubleRecord1 = new DoubleRecord(1.0);
13+
DoubleRecord doubleRecord2 = new DoubleRecord(2.0);
14+
DoubleRecord doubleRecord3 = new DoubleRecord(3.0);
15+
16+
List<DoubleRecord> records = List.of(doubleRecord1, doubleRecord2, doubleRecord3);
17+
List<Double> doubleList = DoubleArray.asList(records, DoubleRecord::value);
18+
assertThat(doubleList).containsExactly(1.0, 2.0, 3.0);
19+
}
20+
21+
record DoubleRecord(double value) {
22+
}
23+
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.navercorp.pinpoint.common.server.util.array;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class LongArrayTest {
11+
12+
@Test
13+
void asLongList() {
14+
LongRecord longRecord1 = new LongRecord(1L);
15+
LongRecord longRecord2 = new LongRecord(2L);
16+
LongRecord longRecord3 = new LongRecord(3L);
17+
18+
List<LongRecord> records = List.of(longRecord1, longRecord2, longRecord3);
19+
List<Long> longList = LongArray.asList(records, LongRecord::value);
20+
assertThat(longList).containsExactly(1L, 2L, 3L);
21+
}
22+
23+
record LongRecord(long value) {
24+
}
25+
26+
@Test
27+
void asLongList_linkedList() {
28+
LongRecord longRecord2 = new LongRecord(2L);
29+
LongRecord longRecord1 = new LongRecord(1L);
30+
LongRecord longRecord3 = new LongRecord(3L);
31+
32+
List<LongRecord> records = new LinkedList<>();
33+
records.add(longRecord1);
34+
records.add(longRecord2);
35+
records.add(longRecord3);
36+
List<Long> longList = LongArray.asList(records, LongRecord::value);
37+
assertThat(longList).containsExactly(1L, 2L, 3L);
38+
}
39+
}

exceptiontrace/exceptiontrace-collector/src/main/java/com/navercorp/pinpoint/exceptiontrace/collector/mapper/StackTraceMapper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.navercorp.pinpoint.exceptiontrace.collector.mapper;
1717

1818
import com.navercorp.pinpoint.common.server.mapper.MapStructUtils;
19+
import com.navercorp.pinpoint.common.server.util.array.IntArray;
1920
import com.navercorp.pinpoint.exceptiontrace.common.model.StackTraceElementWrapper;
2021
import org.mapstruct.Qualifier;
2122
import org.springframework.stereotype.Component;
@@ -80,9 +81,7 @@ public List<String> stackTraceToFileNames(List<StackTraceElementWrapper> fileNam
8081

8182
@StackTraceToLineNumbers
8283
public List<Integer> stackTraceToLineNumber(List<StackTraceElementWrapper> lineNumbers) {
83-
return lineNumbers.stream()
84-
.map(StackTraceElementWrapper::getLineNumber)
85-
.collect(Collectors.toList());
84+
return IntArray.asList(lineNumbers, StackTraceElementWrapper::getLineNumber);
8685
}
8786

8887
@StackTraceToMethodNames

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/definition/metric/AvgUsingIntervalPostProcessor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.navercorp.pinpoint.inspector.web.definition.metric;
1818

19+
import com.google.common.primitives.Doubles;
1920
import com.navercorp.pinpoint.inspector.web.model.InspectorMetricValue;
2021
import org.apache.commons.math3.util.Precision;
2122
import org.springframework.stereotype.Component;
@@ -54,17 +55,17 @@ public List<InspectorMetricValue> postProcess(List<InspectorMetricValue> metricV
5455

5556
private void addTotalMetricValue(List<InspectorMetricValue> processedMetricValueList, int metricValueSize) {
5657

57-
List<Double> totalValueList = new ArrayList<>(metricValueSize);
58+
double[] totalValueList = new double[metricValueSize];
5859
for (int i = 0; i < metricValueSize; i++) {
5960
double total = 0;
6061
for (InspectorMetricValue metricValue : processedMetricValueList) {
6162
total += metricValue.getValueList().get(i);
6263
}
6364
total = Precision.round(total, NUM_DECIMAL_PLACES);
64-
totalValueList.add(total);
65+
totalValueList[i] = total;
6566
}
6667

67-
processedMetricValueList.add(new InspectorMetricValue("totalCount", Collections.emptyList(), "tooltip", "count", totalValueList));
68+
processedMetricValueList.add(new InspectorMetricValue("totalCount", Collections.emptyList(), "tooltip", "count", Doubles.asList(totalValueList)));
6869
}
6970

7071
private void calculateAvg(InspectorMetricValue collectInterval, List<InspectorMetricValue> metricCountList, List<InspectorMetricValue> processedMetricValueList) {
@@ -77,16 +78,16 @@ private void calculateAvg(InspectorMetricValue collectInterval, List<InspectorMe
7778
protected List<Double> calculateAvg(InspectorMetricValue collectInterval, InspectorMetricValue metricCount) {
7879
List<Double> valueList = metricCount.getValueList();
7980
List<Double> commitIntervalList = collectInterval.getValueList();
80-
List<Double> avgList = new ArrayList<>(valueList.size());
81+
double[] avgList = new double[valueList.size()];
8182
for (int i = 0; i < valueList.size(); i++) {
8283
if (commitIntervalList.get(i) < 0) {
83-
avgList.add(i, -1.0);
84+
avgList[i] = -1.0;
8485
continue;
8586
}
86-
avgList.add(calculateTps(valueList.get(i), commitIntervalList.get(i)));
87+
avgList[i] = calculateTps(valueList.get(i), commitIntervalList.get(i));
8788
}
8889

89-
return avgList;
90+
return Doubles.asList(avgList);
9091
}
9192

9293
private double calculateTps(double count, double intervalMs) {

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/definition/metric/PercentageScalePostProcessor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.navercorp.pinpoint.inspector.web.definition.metric;
1818

19+
import com.navercorp.pinpoint.common.server.util.array.DoubleArray;
1920
import com.navercorp.pinpoint.inspector.web.model.InspectorMetricValue;
2021
import com.navercorp.pinpoint.metric.common.util.DoubleUncollectedDataCreator;
2122
import org.springframework.stereotype.Component;
2223

23-
import java.util.ArrayList;
2424
import java.util.List;
2525
import java.util.stream.Collectors;
2626

@@ -45,9 +45,8 @@ public List<InspectorMetricValue> postProcess(List<InspectorMetricValue> metricV
4545
}
4646

4747
private InspectorMetricValue processInspectorMetric(InspectorMetricValue inspectorMetric) {
48-
List<Double> scaledValues = inspectorMetric.getValueList().stream()
49-
.map(this::scaleToPercentage)
50-
.collect(Collectors.toList());
48+
List<Double> valueList = inspectorMetric.getValueList();
49+
List<Double> scaledValues = DoubleArray.asList(valueList, this::scaleToPercentage);
5150

5251
return new InspectorMetricValue(
5352
inspectorMetric.getFieldName(),

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/service/DefaultAgentStatService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.navercorp.pinpoint.inspector.web.service;
1818

19+
import com.navercorp.pinpoint.common.server.util.array.DoubleArray;
1920
import com.navercorp.pinpoint.common.server.util.timewindow.TimeWindow;
2021
import com.navercorp.pinpoint.inspector.web.dao.AgentStatDao;
2122
import com.navercorp.pinpoint.inspector.web.definition.AggregationFunction;
@@ -168,9 +169,7 @@ private InspectorMetricValue createInspectorMetricValue(TimeWindow timeWindow, F
168169
TimeSeriesBuilder<Double> builder = new TimeSeriesBuilder<>(timeWindow, uncollectedDataCreator);
169170
List<SystemMetricPoint<Double>> filledSystemMetricDataList = builder.build(postProcessedDataList);
170171

171-
List<Double> valueList = filledSystemMetricDataList.stream()
172-
.map(SystemMetricPoint::getYVal)
173-
.collect(Collectors.toList());
172+
List<Double> valueList = DoubleArray.asList(filledSystemMetricDataList, SystemMetricPoint::getYVal);
174173

175174
return new InspectorMetricValue(field.getFieldAlias(), field.getTags(), field.getChartType(), field.getUnit(), valueList);
176175
}

0 commit comments

Comments
 (0)