Skip to content

Commit 47142f9

Browse files
authored
Merge pull request #206 from mbasmanova/estimate-memory-size-for-accelerators
Add accelerator size to OGCGeometry#estimateMemorySize
2 parents bc4769a + 17fdf4e commit 47142f9

10 files changed

Lines changed: 201 additions & 26 deletions

src/main/java/com/esri/core/geometry/GeometryAccelerators.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
package com.esri.core.geometry;
2525

26-
import java.util.ArrayList;
27-
2826
class GeometryAccelerators {
2927

3028
private RasterizedGeometry2D m_rasterizedGeometry;
@@ -84,4 +82,11 @@ static boolean canUseQuadTreeForPaths(Geometry geom) {
8482

8583
return true;
8684
}
85+
86+
public long estimateMemorySize()
87+
{
88+
return (m_rasterizedGeometry != null ? m_rasterizedGeometry.estimateMemorySize() : 0) +
89+
(m_quad_tree != null ? m_quad_tree.estimateMemorySize() : 0) +
90+
(m_quad_tree_for_paths != null ? m_quad_tree_for_paths.estimateMemorySize() : 0);
91+
}
8792
}

src/main/java/com/esri/core/geometry/MultiPathImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public long estimateMemorySize()
7777
size += m_vertexAttributes[i].estimateMemorySize();
7878
}
7979
}
80+
81+
if (m_accelerators != null) {
82+
size += m_accelerators.estimateMemorySize();
83+
}
84+
8085
return size;
8186
}
8287

src/main/java/com/esri/core/geometry/QuadTreeImpl.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import java.io.Serializable;
3030
import java.util.ArrayList;
3131

32+
import static com.esri.core.geometry.SizeOf.SIZE_OF_DATA;
33+
import static com.esri.core.geometry.SizeOf.SIZE_OF_QUAD_TREE_IMPL;
34+
import static com.esri.core.geometry.SizeOf.sizeOfObjectArray;
35+
3236
class QuadTreeImpl implements Serializable {
3337
private static final long serialVersionUID = 1L;
3438

@@ -777,6 +781,22 @@ QuadTreeSortedIteratorImpl getSortedIterator() {
777781
return new QuadTreeSortedIteratorImpl(getIterator());
778782
}
779783

784+
public long estimateMemorySize()
785+
{
786+
long size = SIZE_OF_QUAD_TREE_IMPL +
787+
(m_extent != null ? m_extent.estimateMemorySize() : 0) +
788+
(m_data_extent != null ? m_data_extent.estimateMemorySize() : 0) +
789+
(m_quad_tree_nodes != null ? m_quad_tree_nodes.estimateMemorySize() : 0) +
790+
(m_element_nodes != null ? m_element_nodes.estimateMemorySize() : 0) +
791+
(m_free_data != null ? m_free_data.estimateMemorySize() : 0);
792+
793+
if (m_data != null) {
794+
size += sizeOfObjectArray(m_data.size()) + m_data.size() * SIZE_OF_DATA;
795+
}
796+
797+
return size;
798+
}
799+
780800
private void reset_(Envelope2D extent, int height) {
781801
if (height < 0 || height > 127)
782802
throw new IllegalArgumentException("invalid height");
@@ -1268,9 +1288,6 @@ static final class Data {
12681288
int element;
12691289
Envelope2D box;
12701290

1271-
Data() {
1272-
}
1273-
12741291
Data(int element_, double x1, double y1, double x2, double y2) {
12751292
element = element_;
12761293
box = new Envelope2D();

src/main/java/com/esri/core/geometry/RasterizedGeometry2D.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,10 @@ static boolean canUseAccelerator(Geometry geom) {
136136
*/
137137
public abstract boolean dbgSaveToBitmap(String fileName);
138138

139+
/**
140+
* Returns an estimate of this object size in bytes.
141+
*
142+
* @return Returns an estimate of this object size in bytes.
143+
*/
144+
public abstract long estimateMemorySize();
139145
}

src/main/java/com/esri/core/geometry/RasterizedGeometry2DImpl.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@
2424

2525
package com.esri.core.geometry;
2626

27-
import java.io.*;
27+
import java.io.FileOutputStream;
28+
import java.io.IOException;
2829
import java.nio.ByteBuffer;
2930
import java.nio.ByteOrder;
3031

31-
import com.esri.core.geometry.Envelope2D;
32-
import com.esri.core.geometry.Geometry;
33-
import com.esri.core.geometry.GeometryException;
34-
import com.esri.core.geometry.NumberUtils;
35-
import com.esri.core.geometry.Point2D;
36-
import com.esri.core.geometry.Segment;
37-
import com.esri.core.geometry.SegmentIteratorImpl;
38-
import com.esri.core.geometry.SimpleRasterizer;
32+
import static com.esri.core.geometry.SizeOf.SIZE_OF_RASTERIZED_GEOMETRY_2D_IMPL;
33+
import static com.esri.core.geometry.SizeOf.SIZE_OF_SCAN_CALLBACK_IMPL;
34+
import static com.esri.core.geometry.SizeOf.sizeOfIntArray;
3935

4036
final class RasterizedGeometry2DImpl extends RasterizedGeometry2D {
4137
int[] m_bitmap;
@@ -89,6 +85,13 @@ public void drawScan(int[] scans, int scanCount3) {
8985
}
9086
}
9187
}
88+
89+
@Override
90+
public long estimateMemorySize()
91+
{
92+
return SIZE_OF_SCAN_CALLBACK_IMPL +
93+
(m_bitmap != null ? sizeOfIntArray(m_bitmap.length) : 0);
94+
}
9295
}
9396

9497
void fillMultiPath(SimpleRasterizer rasterizer, Transformation2D trans, MultiPathImpl polygon, boolean isWinding) {
@@ -559,4 +562,14 @@ public boolean dbgSaveToBitmap(String fileName) {
559562

560563
}
561564
}
565+
566+
@Override
567+
public long estimateMemorySize()
568+
{
569+
return SIZE_OF_RASTERIZED_GEOMETRY_2D_IMPL +
570+
(m_geomEnv != null ? m_geomEnv.estimateMemorySize() : 0) +
571+
(m_transform != null ? m_transform.estimateMemorySize(): 0) +
572+
(m_rasterizer != null ? m_rasterizer.estimateMemorySize(): 0) +
573+
(m_callback != null ? m_callback.estimateMemorySize(): 0);
574+
}
562575
}

src/main/java/com/esri/core/geometry/SimpleRasterizer.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424

2525
package com.esri.core.geometry;
2626

27-
import java.util.ArrayList;
2827
import java.util.Arrays;
29-
import java.util.Collections;
3028
import java.util.Comparator;
3129

30+
import static com.esri.core.geometry.SizeOf.SIZE_OF_EDGE;
31+
import static com.esri.core.geometry.SizeOf.SIZE_OF_SIMPLE_RASTERIZER;
32+
import static com.esri.core.geometry.SizeOf.sizeOfIntArray;
33+
import static com.esri.core.geometry.SizeOf.sizeOfObjectArray;
34+
3235
/**
3336
* Simple scanline rasterizer. Caller provides a callback to draw pixels to actual surface.
3437
*
@@ -44,15 +47,22 @@ public class SimpleRasterizer {
4447
* Winding fill rule
4548
*/
4649
public final static int WINDING = 1;
47-
48-
public static interface ScanCallback {
50+
51+
public interface ScanCallback {
4952
/**
5053
* Rasterizer calls this method for each scan it produced
5154
* @param scans array of scans. Scans are triplets of numbers. The start X coordinate for the scan (inclusive),
5255
* the end X coordinate of the scan (exclusive), the Y coordinate for the scan.
5356
* @param scanCount3 The number of initialized elements in the scans array. The scan count is scanCount3 / 3.
5457
*/
55-
public abstract void drawScan(int[] scans, int scanCount3);
58+
void drawScan(int[] scans, int scanCount3);
59+
60+
/**
61+
* Returns an estimate of this object size in bytes.
62+
*
63+
* @return Returns an estimate of this object size in bytes.
64+
*/
65+
long estimateMemorySize();
5666
}
5767

5868
public SimpleRasterizer() {
@@ -340,17 +350,50 @@ final boolean addSegmentStroke(double x1, double y1, double x2, double y2, doubl
340350
}
341351

342352
public final ScanCallback getScanCallback() { return callback_; }
343-
344-
353+
354+
public long estimateMemorySize()
355+
{
356+
// callback_ is only a pointer, the actual size is accounted for in the caller of setup()
357+
long size = SIZE_OF_SIMPLE_RASTERIZER +
358+
(activeEdgesTable_ != null ? activeEdgesTable_.estimateMemorySize() : 0) +
359+
(scanBuffer_ != null ? sizeOfIntArray(scanBuffer_.length) : 0);
360+
361+
if (ySortedEdges_ != null) {
362+
size += sizeOfObjectArray(ySortedEdges_.length);
363+
for (int i = 0; i < ySortedEdges_.length; i++) {
364+
if (ySortedEdges_[i] != null) {
365+
size += ySortedEdges_[i].estimateMemorySize();
366+
}
367+
}
368+
}
369+
370+
if (sortBuffer_ != null) {
371+
size += sizeOfObjectArray(sortBuffer_.length);
372+
for (int i = 0; i < sortBuffer_.length; i++) {
373+
if (sortBuffer_[i] != null) {
374+
size += sortBuffer_[i].estimateMemorySize();
375+
}
376+
}
377+
}
378+
379+
return size;
380+
}
381+
345382
//PRIVATE
346383

347-
private static class Edge {
384+
static class Edge {
348385
long x;
349386
long dxdy;
350387
int y;
351388
int ymax;
352389
int dir;
353390
Edge next;
391+
392+
long estimateMemorySize()
393+
{
394+
// next is only a pointer, the actual size is accounted for in SimpleRasterizer#estimateMemorySize
395+
return SIZE_OF_EDGE;
396+
}
354397
}
355398

356399
private final void advanceAET_() {

src/main/java/com/esri/core/geometry/SizeOf.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import static sun.misc.Unsafe.ARRAY_INT_INDEX_SCALE;
3737
import static sun.misc.Unsafe.ARRAY_LONG_BASE_OFFSET;
3838
import static sun.misc.Unsafe.ARRAY_LONG_INDEX_SCALE;
39+
import static sun.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET;
40+
import static sun.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE;
3941
import static sun.misc.Unsafe.ARRAY_SHORT_BASE_OFFSET;
4042
import static sun.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE;
4143

@@ -88,6 +90,22 @@ public final class SizeOf {
8890

8991
public static final int SIZE_OF_MAPGEOMETRY = 24;
9092

93+
public static final int SIZE_OF_RASTERIZED_GEOMETRY_2D_IMPL = 112;
94+
95+
public static final int SIZE_OF_SCAN_CALLBACK_IMPL = 32;
96+
97+
public static final int SIZE_OF_TRANSFORMATION_2D = 64;
98+
99+
public static final int SIZE_OF_SIMPLE_RASTERIZER = 64;
100+
101+
public static final int SIZE_OF_EDGE = 48;
102+
103+
public static final int SIZE_OF_QUAD_TREE_IMPL = 48;
104+
105+
public static final int SIZE_OF_DATA = 24;
106+
107+
public static final int SIZE_OF_STRIDED_INDEX_TYPE_COLLECTION = 48;
108+
91109
public static long sizeOfByteArray(int length) {
92110
return ARRAY_BYTE_BASE_OFFSET + (((long) ARRAY_BYTE_INDEX_SCALE) * length);
93111
}
@@ -116,6 +134,11 @@ public static long sizeOfDoubleArray(int length) {
116134
return ARRAY_DOUBLE_BASE_OFFSET + (((long) ARRAY_DOUBLE_INDEX_SCALE) * length);
117135
}
118136

137+
public static long sizeOfObjectArray(int length)
138+
{
139+
return ARRAY_OBJECT_BASE_OFFSET + (((long) ARRAY_OBJECT_INDEX_SCALE) * length);
140+
}
141+
119142
private SizeOf() {
120143
}
121144
}

src/main/java/com/esri/core/geometry/StridedIndexTypeCollection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
import java.io.Serializable;
2727

28+
import static com.esri.core.geometry.SizeOf.SIZE_OF_STRIDED_INDEX_TYPE_COLLECTION;
29+
import static com.esri.core.geometry.SizeOf.sizeOfIntArray;
30+
import static com.esri.core.geometry.SizeOf.sizeOfObjectArray;
31+
2832
/**
2933
* A collection of strides of Index_type elements. To be used when one needs a
3034
* collection of homogeneous elements that contain only integer fields (i.e.
@@ -277,4 +281,18 @@ private void grow_(long newsize) {
277281
}
278282
}
279283
}
284+
285+
public long estimateMemorySize()
286+
{
287+
long size = SIZE_OF_STRIDED_INDEX_TYPE_COLLECTION;
288+
if (m_buffer != null) {
289+
size += sizeOfObjectArray(m_buffer.length);
290+
for (int i = 0; i< m_buffer.length; i++) {
291+
if (m_buffer[i] != null) {
292+
size += sizeOfIntArray(m_buffer[i].length);
293+
}
294+
}
295+
}
296+
return size;
297+
}
280298
}

src/main/java/com/esri/core/geometry/Transformation2D.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
package com.esri.core.geometry;
2626

27+
import static com.esri.core.geometry.SizeOf.SIZE_OF_TRANSFORMATION_2D;
28+
2729
/**
2830
* The affine transformation class for 2D.
2931
*
@@ -921,4 +923,8 @@ public void extractScaleTransform(Transformation2D scale,
921923
rotateNshearNshift.multiply(this);
922924
}
923925

926+
public long estimateMemorySize()
927+
{
928+
return SIZE_OF_TRANSFORMATION_2D;
929+
}
924930
}

0 commit comments

Comments
 (0)