Skip to content

Commit c3b7cd8

Browse files
committed
Merge pull request #6 from JuliaGeo/alloc
Minimize allocation of single element arrays
2 parents b9c3494 + 7b3fe3c commit c3b7cd8

File tree

2 files changed

+126
-97
lines changed

2 files changed

+126
-97
lines changed

src/geos_functions.jl

Lines changed: 110 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,28 @@ function getZ!(ptr::GEOSCoordSeq, index::Int, coord::Vector{Float64})
105105
result
106106
end
107107

108-
function getSize(ptr::GEOSCoordSeq)
109-
ncoords = Array(UInt32, 1)
110-
# Get size info from a Coordinate Sequence (Return 0 on exception)
111-
result = GEOSCoordSeq_getSize(ptr, pointer(ncoords))
112-
if result == 0
113-
error("LibGEOS: Error in GEOSCoordSeq_getSize")
108+
let out = Array(UInt32, 1)
109+
global getSize
110+
function getSize(ptr::GEOSCoordSeq)
111+
# Get size info from a Coordinate Sequence (Return 0 on exception)
112+
result = GEOSCoordSeq_getSize(ptr, pointer(out))
113+
if result == 0
114+
error("LibGEOS: Error in GEOSCoordSeq_getSize")
115+
end
116+
@compat Int(out[1])
114117
end
115-
@compat Int(ncoords[1])
116118
end
117119

118-
function getDimensions(ptr::GEOSCoordSeq)
119-
ndim = Array(UInt32, 1)
120-
# Get dimensions info from a Coordinate Sequence (Return 0 on exception)
121-
result = GEOSCoordSeq_getDimensions(ptr, pointer(ndim))
122-
if result == 0
123-
error("LibGEOS: Error in GEOSCoordSeq_getDimensions")
120+
let out = Array(UInt32, 1)
121+
global getDimensions
122+
function getDimensions(ptr::GEOSCoordSeq)
123+
# Get dimensions info from a Coordinate Sequence (Return 0 on exception)
124+
result = GEOSCoordSeq_getDimensions(ptr, pointer(out))
125+
if result == 0
126+
error("LibGEOS: Error in GEOSCoordSeq_getDimensions")
127+
end
128+
@compat Int(out[1])
124129
end
125-
@compat Int(ndim[1])
126130
end
127131

128132
# convenience functions
@@ -168,10 +172,12 @@ function createCoordSeq(coords::Vector{Vector{Float64}})
168172
coordinates
169173
end
170174

171-
function getX(ptr::GEOSCoordSeq, i::Int)
172-
coord = Array(Float64, 1)
173-
getX!(ptr, i, coord)
174-
coord[1]
175+
let out = Array(Float64, 1)
176+
global getX
177+
function getX(ptr::GEOSCoordSeq, i::Int)
178+
getX!(ptr, i, out)
179+
out[1]
180+
end
175181
end
176182

177183
function getX(ptr::GEOSCoordSeq)
@@ -185,10 +191,13 @@ function getX(ptr::GEOSCoordSeq)
185191
xcoords
186192
end
187193

188-
function getY(ptr::GEOSCoordSeq, i::Int)
189-
coord = Array(Float64, 1)
190-
getY!(ptr, i, coord)
191-
coord[1]
194+
let out = Array(Float64, 1)
195+
global getY
196+
function getY(ptr::GEOSCoordSeq, i::Int)
197+
out = Array(Float64, 1)
198+
getY!(ptr, i, out)
199+
out[1]
200+
end
192201
end
193202

194203
function getY(ptr::GEOSCoordSeq)
@@ -202,10 +211,12 @@ function getY(ptr::GEOSCoordSeq)
202211
ycoords
203212
end
204213

205-
function getZ(ptr::GEOSCoordSeq, i::Int)
206-
coord = Array(Float64, 1)
207-
getZ!(ptr, i, coord)
208-
coord[1]
214+
let out = Array(Float64, 1)
215+
global getZ
216+
function getZ(ptr::GEOSCoordSeq, i::Int)
217+
getZ!(ptr, i, out)
218+
out[1]
219+
end
209220
end
210221

211222
function getZ(ptr::GEOSCoordSeq)
@@ -913,22 +924,26 @@ function numPoints(ptr::GEOSGeom)
913924
end
914925

915926
# Return -1 on exception, Geometry must be a Point.
916-
function getGeomX(ptr::GEOSGeom)
917-
x = Array(Float64, 1)
918-
result = GEOSGeomGetX(ptr, pointer(x))
919-
if result == -1
920-
error("LibGEOS: Error in GEOSGeomGetX")
927+
let out = Array(Float64, 1)
928+
global getGeomX
929+
function getGeomX(ptr::GEOSGeom)
930+
result = GEOSGeomGetX(ptr, pointer(out))
931+
if result == -1
932+
error("LibGEOS: Error in GEOSGeomGetX")
933+
end
934+
out[1]
921935
end
922-
x[1]
923936
end
924937

925-
function getGeomY(ptr::GEOSGeom)
926-
y = Array(Float64, 1)
927-
result = GEOSGeomGetY(ptr, pointer(y))
928-
if result == -1
929-
error("LibGEOS: Error in GEOSGeomGetY")
938+
let out = Array(Float64, 1)
939+
global getGeomY
940+
function getGeomY(ptr::GEOSGeom)
941+
result = GEOSGeomGetY(ptr, pointer(out))
942+
if result == -1
943+
error("LibGEOS: Error in GEOSGeomGetY")
944+
end
945+
out[1]
930946
end
931-
y[1]
932947
end
933948

934949
# Return NULL on exception, Geometry must be a Polygon.
@@ -1015,54 +1030,64 @@ end
10151030
# -----
10161031
# Misc functions
10171032
# -----
1018-
function geomArea(ptr::GEOSGeom)
1019-
area = Array(Float64, 1)
1020-
# Return 0 on exception, 1 otherwise
1021-
result = GEOSArea(ptr, pointer(area))
1022-
if result == 0
1023-
error("LibGEOS: Error in GEOSArea")
1024-
end
1025-
area[1]
1026-
end
1027-
1028-
function geomLength(ptr::GEOSGeom)
1029-
len = Array(Float64, 1)
1030-
# Return 0 on exception, 1 otherwise
1031-
result = GEOSLength(ptr, pointer(len))
1032-
if result == 0
1033-
error("LibGEOS: Error in GEOSLength")
1034-
end
1035-
len[1]
1036-
end
1037-
1038-
function geomDistance(g1::GEOSGeom, g2::GEOSGeom)
1039-
dist = Array(Float64, 1)
1040-
# Return 0 on exception, 1 otherwise
1041-
result = GEOSDistance(g1, g2, pointer(dist))
1042-
if result == 0
1043-
error("LibGEOS: Error in GEOSDistance")
1044-
end
1045-
dist[1]
1046-
end
1047-
1048-
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom)
1049-
dist = Array(Float64, 1)
1050-
# Return 0 on exception, 1 otherwise
1051-
result = GEOSHausdorffDistance(g1, g2, pointer(dist))
1052-
if result == 0
1053-
error("LibGEOS: Error in GEOSHausdorffDistance")
1054-
end
1055-
dist[1]
1056-
end
1057-
1058-
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, densifyFrac::Float64)
1059-
dist = Array(Float64, 1)
1060-
# Return 0 on exception, 1 otherwise
1061-
result = GEOSHausdorffDistanceDensify(g1, g2, densifyFrac, pointer(dist))
1062-
if result == 0
1063-
error("LibGEOS: Error in GEOSHausdorffDistanceDensify")
1033+
let out = Array(Float64, 1)
1034+
global geomArea
1035+
function geomArea(ptr::GEOSGeom)
1036+
# Return 0 on exception, 1 otherwise
1037+
result = GEOSArea(ptr, pointer(out))
1038+
if result == 0
1039+
error("LibGEOS: Error in GEOSArea")
1040+
end
1041+
out[1]
1042+
end
1043+
end
1044+
1045+
let out = Array(Float64, 1)
1046+
global geomLength
1047+
function geomLength(ptr::GEOSGeom)
1048+
# Return 0 on exception, 1 otherwise
1049+
result = GEOSLength(ptr, pointer(out))
1050+
if result == 0
1051+
error("LibGEOS: Error in GEOSLength")
1052+
end
1053+
out[1]
1054+
end
1055+
end
1056+
1057+
let out = Array(Float64, 1)
1058+
global geomDistance
1059+
function geomDistance(g1::GEOSGeom, g2::GEOSGeom)
1060+
# Return 0 on exception, 1 otherwise
1061+
result = GEOSDistance(g1, g2, pointer(out))
1062+
if result == 0
1063+
error("LibGEOS: Error in GEOSDistance")
1064+
end
1065+
out[1]
1066+
end
1067+
end
1068+
1069+
let out = Array(Float64, 1)
1070+
global hausdorffdistance
1071+
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom)
1072+
# Return 0 on exception, 1 otherwise
1073+
result = GEOSHausdorffDistance(g1, g2, pointer(out))
1074+
if result == 0
1075+
error("LibGEOS: Error in GEOSHausdorffDistance")
1076+
end
1077+
out[1]
1078+
end
1079+
end
1080+
1081+
let out = Array(Float64, 1)
1082+
global hausdorffdistance
1083+
function hausdorffdistance(g1::GEOSGeom, g2::GEOSGeom, densifyFrac::Float64)
1084+
# Return 0 on exception, 1 otherwise
1085+
result = GEOSHausdorffDistanceDensify(g1, g2, densifyFrac, pointer(out))
1086+
if result == 0
1087+
error("LibGEOS: Error in GEOSHausdorffDistanceDensify")
1088+
end
1089+
out[1]
10641090
end
1065-
dist[1]
10661091
end
10671092

10681093
# Return 0 on exception, the closest points of the two geometries otherwise.

src/geos_operations.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,26 @@ for geom in (:Point, :MultiPoint, :LineString, :MultiLineString, :LinearRing, :P
292292
end
293293

294294
# # Return -1 on exception, Geometry must be a Point.
295-
# function getGeomX(ptr::GEOSGeom)
296-
# x = Array(Float64, 1)
297-
# result = GEOSGeomGetX(ptr, pointer(x))
298-
# if result == -1
299-
# error("LibGEOS: Error in GEOSGeomGetX")
295+
# let out = Array(Float64, 1)
296+
# global getGeomX
297+
# function getGeomX(ptr::GEOSGeom)
298+
# result = GEOSGeomGetX(ptr, pointer(out))
299+
# if result == -1
300+
# error("LibGEOS: Error in GEOSGeomGetX")
301+
# end
302+
# out[1]
300303
# end
301-
# x[1]
302304
# end
303305

304-
# function getGeomY(ptr::GEOSGeom)
305-
# y = Array(Float64, 1)
306-
# result = GEOSGeomGetY(ptr, pointer(y))
307-
# if result == -1
308-
# error("LibGEOS: Error in GEOSGeomGetY")
306+
# let out = Array(Float64, 1)
307+
# global getGeomY
308+
# function getGeomY(ptr::GEOSGeom)
309+
# result = GEOSGeomGetY(ptr, pointer(out))
310+
# if result == -1
311+
# error("LibGEOS: Error in GEOSGeomGetY")
312+
# end
313+
# out[1]
309314
# end
310-
# y[1]
311315
# end
312316

313317
interiorRings(obj::Polygon) = map(LinearRing, interiorRings(obj.ptr))

0 commit comments

Comments
 (0)