Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer dimensions in capacity #670

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/algorithm/geo/minimum_rotated_rect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::array::polygon::PolygonCapacity;
use crate::array::*;
use crate::chunked_array::{ChunkedGeometryArray, ChunkedGeometryArrayTrait, ChunkedPolygonArray};
Expand Down Expand Up @@ -52,7 +54,12 @@ impl<O: OffsetSizeTrait> MinimumRotatedRect<O> for PointArray<2> {
// Each output polygon has exactly 5 coordinates
let coord_capacity = ring_capacity * 5;

let capacity = PolygonCapacity::new(coord_capacity, ring_capacity, geom_capacity);
let capacity = PolygonCapacity::new(
coord_capacity,
ring_capacity,
geom_capacity,
HashSet::from_iter([2]),
);

let mut output_array = PolygonBuilder::with_capacity(capacity);

Expand Down Expand Up @@ -84,7 +91,12 @@ macro_rules! iter_geo_impl {
// Each output polygon has exactly 5 coordinates
let coord_capacity = ring_capacity * 5;

let capacity = PolygonCapacity::new(coord_capacity, ring_capacity, geom_capacity);
let capacity = PolygonCapacity::new(
coord_capacity,
ring_capacity,
geom_capacity,
HashSet::from_iter([2]),
);

let mut output_array = PolygonBuilder::with_capacity(capacity);

Expand Down
19 changes: 15 additions & 4 deletions src/algorithm/native/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! todo: have a set of "fast cast" functions, where you first try to fast cast and fall back to
//! slower copies if necessary. Can check that the coord type of the input and output is the same.

use std::collections::HashSet;
use std::sync::Arc;

use arrow_array::OffsetSizeTrait;
Expand Down Expand Up @@ -77,8 +78,11 @@ impl Cast for PointArray<2> {
Ok(Arc::new(builder.finish()))
}
MultiPoint(ct, Dimension::XY) => {
let capacity =
MultiPointCapacity::new(self.buffer_lengths(), self.buffer_lengths());
let capacity = MultiPointCapacity::new(
self.buffer_lengths(),
self.buffer_lengths(),
HashSet::from_iter([2]),
);
let mut builder = MultiPointBuilder::<i32, 2>::with_capacity_and_options(
capacity,
*ct,
Expand All @@ -89,8 +93,11 @@ impl Cast for PointArray<2> {
Ok(Arc::new(builder.finish()))
}
LargeMultiPoint(ct, Dimension::XY) => {
let capacity =
MultiPointCapacity::new(self.buffer_lengths(), self.buffer_lengths());
let capacity = MultiPointCapacity::new(
self.buffer_lengths(),
self.buffer_lengths(),
HashSet::from_iter([2]),
);
let mut builder = MultiPointBuilder::<i64, 2>::with_capacity_and_options(
capacity,
*ct,
Expand Down Expand Up @@ -510,6 +517,7 @@ impl<O: OffsetSizeTrait> Cast for MultiLineStringArray<O, 2> {
let capacity = LineStringCapacity {
coord_capacity: existing_capacity.coord_capacity,
geom_capacity: existing_capacity.ring_capacity,
dimensions: HashSet::from_iter([2]),
};
let mut builder = LineStringBuilder::<i32, 2>::with_capacity_and_options(
capacity,
Expand All @@ -530,6 +538,7 @@ impl<O: OffsetSizeTrait> Cast for MultiLineStringArray<O, 2> {
let capacity = LineStringCapacity {
coord_capacity: existing_capacity.coord_capacity,
geom_capacity: existing_capacity.ring_capacity,
dimensions: HashSet::from_iter([2]),
};
let mut builder = LineStringBuilder::<i64, 2>::with_capacity_and_options(
capacity,
Expand Down Expand Up @@ -621,6 +630,7 @@ impl<O: OffsetSizeTrait> Cast for MultiPolygonArray<O, 2> {
coord_capacity: existing_capacity.coord_capacity,
ring_capacity: existing_capacity.ring_capacity,
geom_capacity: existing_capacity.polygon_capacity,
dimensions: HashSet::from_iter([2]),
};
let mut builder = PolygonBuilder::<i32, 2>::with_capacity_and_options(
capacity,
Expand All @@ -642,6 +652,7 @@ impl<O: OffsetSizeTrait> Cast for MultiPolygonArray<O, 2> {
coord_capacity: existing_capacity.coord_capacity,
ring_capacity: existing_capacity.ring_capacity,
geom_capacity: existing_capacity.polygon_capacity,
dimensions: HashSet::from_iter([2]),
};
let mut builder = PolygonBuilder::<i64, 2>::with_capacity_and_options(
capacity,
Expand Down
2 changes: 1 addition & 1 deletion src/array/geometrycollection/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<O: OffsetSizeTrait, const D: usize> GeometryCollectionArray<O, D> {
/// The number of bytes occupied by this array.
pub fn num_bytes(&self) -> usize {
let validity_len = self.validity().map(|v| v.buffer().len()).unwrap_or(0);
validity_len + self.buffer_lengths().num_bytes::<O>()
validity_len + self.buffer_lengths().num_bytes::<O>(D)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/array/geometrycollection/capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::geo_traits::{
/// [`GeometryCollectionArray`][crate::array::GeometryCollectionArray].
///
/// This can be used to reduce allocations by allocating once for exactly the array size you need.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct GeometryCollectionCapacity {
pub(crate) mixed_capacity: MixedCapacity,
pub(crate) geom_capacity: usize,
Expand Down Expand Up @@ -141,10 +141,10 @@ impl GeometryCollectionCapacity {
}

/// The number of bytes an array with this capacity would occupy.
pub fn num_bytes<O: OffsetSizeTrait>(&self) -> usize {
pub fn num_bytes<O: OffsetSizeTrait>(&self, dim: usize) -> usize {
let offsets_byte_width = if O::IS_LARGE { 8 } else { 4 };
let num_offsets = self.geom_capacity;
(offsets_byte_width * num_offsets) + self.mixed_capacity.num_bytes::<O>()
(offsets_byte_width * num_offsets) + self.mixed_capacity.num_bytes::<O>(dim)
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/array/linestring/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::algorithm::native::eq::offset_buffer_eq;
Expand Down Expand Up @@ -133,13 +133,17 @@ impl<O: OffsetSizeTrait, const D: usize> LineStringArray<O, D> {

/// The lengths of each buffer contained in this array.
pub fn buffer_lengths(&self) -> LineStringCapacity {
LineStringCapacity::new(self.geom_offsets.last().to_usize().unwrap(), self.len())
LineStringCapacity::new(
self.geom_offsets.last().to_usize().unwrap(),
self.len(),
HashSet::from_iter([D]),
)
}

/// The number of bytes occupied by this array.
pub fn num_bytes(&self) -> usize {
let validity_len = self.validity().map(|v| v.buffer().len()).unwrap_or(0);
validity_len + self.buffer_lengths().num_bytes::<O>()
validity_len + self.buffer_lengths().num_bytes::<O>(D)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/array/linestring/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl<O: OffsetSizeTrait, const D: usize> GeometryArrayBuilder for LineStringBuil
coord_type: CoordType,
metadata: Arc<ArrayMetadata>,
) -> Self {
let capacity = LineStringCapacity::new(0, geom_capacity);
let capacity = LineStringCapacity::new(0, geom_capacity, Default::default());
Self::with_capacity_and_options(capacity, coord_type, metadata)
}

Expand Down
30 changes: 21 additions & 9 deletions src/array/linestring/capacity.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ops::Add;
use std::collections::HashSet;
use std::ops::{Add, AddAssign};

use arrow_array::OffsetSizeTrait;

Expand All @@ -8,24 +9,26 @@ use crate::geo_traits::{GeometryTrait, GeometryType, LineStringTrait};
/// A counter for the buffer sizes of a [`LineStringArray`][crate::array::LineStringArray].
///
/// This can be used to reduce allocations by allocating once for exactly the array size you need.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct LineStringCapacity {
pub(crate) coord_capacity: usize,
pub(crate) geom_capacity: usize,
pub(crate) dimensions: HashSet<usize>,
}

impl LineStringCapacity {
/// Create a new capacity with known sizes.
pub fn new(coord_capacity: usize, geom_capacity: usize) -> Self {
pub fn new(coord_capacity: usize, geom_capacity: usize, dimensions: HashSet<usize>) -> Self {
Self {
coord_capacity,
geom_capacity,
dimensions,
}
}

/// Create a new empty capacity.
pub fn new_empty() -> Self {
Self::new(0, 0)
Self::new(0, 0, HashSet::new())
}

/// Return `true` if the capacity is empty.
Expand All @@ -45,6 +48,7 @@ impl LineStringCapacity {
#[inline]
fn add_valid_line_string(&mut self, line_string: &impl LineStringTrait) {
self.coord_capacity += line_string.num_coords();
self.dimensions.insert(line_string.dim());
}

#[inline]
Expand Down Expand Up @@ -81,10 +85,10 @@ impl LineStringCapacity {
}

/// The number of bytes an array with this capacity would occupy.
pub fn num_bytes<O: OffsetSizeTrait>(&self) -> usize {
pub fn num_bytes<O: OffsetSizeTrait>(&self, dim: usize) -> usize {
let offsets_byte_width = if O::IS_LARGE { 8 } else { 4 };
let num_offsets = self.geom_capacity;
(offsets_byte_width * num_offsets) + (self.coord_capacity * 2 * 8)
(offsets_byte_width * num_offsets) + (self.coord_capacity * dim * 8)
}
}

Expand All @@ -98,8 +102,16 @@ impl Add for LineStringCapacity {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
let coord_capacity = self.coord_capacity + rhs.coord_capacity;
let geom_capacity = self.geom_capacity + rhs.geom_capacity;
Self::new(coord_capacity, geom_capacity)
let mut new = self.clone();
new += rhs;
new
}
}

impl AddAssign for LineStringCapacity {
fn add_assign(&mut self, rhs: Self) {
self.coord_capacity += rhs.coord_capacity();
self.geom_capacity += rhs.geom_capacity();
self.dimensions.extend(rhs.dimensions);
}
}
2 changes: 1 addition & 1 deletion src/array/mixed/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl<O: OffsetSizeTrait, const D: usize> MixedGeometryArray<O, D> {

/// The number of bytes occupied by this array.
pub fn num_bytes(&self) -> usize {
self.buffer_lengths().num_bytes::<O>()
self.buffer_lengths().num_bytes::<O>(D)
}
}

Expand Down
40 changes: 20 additions & 20 deletions src/array/mixed/capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::geo_traits::*;
/// A counter for the buffer sizes of a [`MixedGeometryArray`][crate::array::MixedGeometryArray].
///
/// This can be used to reduce allocations by allocating once for exactly the array size you need.
#[derive(Default, Debug, Clone, Copy)]
#[derive(Default, Debug, Clone)]
pub struct MixedCapacity {
/// Simple: just the total number of points, nulls included
pub(crate) point: usize,
Expand Down Expand Up @@ -77,28 +77,28 @@ impl MixedCapacity {
total
}

pub fn point_capacity(&self) -> usize {
self.point
pub fn point_capacity(&self) -> &usize {
&self.point
}

pub fn line_string_capacity(&self) -> LineStringCapacity {
self.line_string
pub fn line_string_capacity(&self) -> &LineStringCapacity {
&self.line_string
}

pub fn polygon_capacity(&self) -> PolygonCapacity {
self.polygon
pub fn polygon_capacity(&self) -> &PolygonCapacity {
&self.polygon
}

pub fn multi_point_capacity(&self) -> MultiPointCapacity {
self.multi_point
pub fn multi_point_capacity(&self) -> &MultiPointCapacity {
&self.multi_point
}

pub fn multi_line_string_capacity(&self) -> MultiLineStringCapacity {
self.multi_line_string
pub fn multi_line_string_capacity(&self) -> &MultiLineStringCapacity {
&self.multi_line_string
}

pub fn multi_polygon_capacity(&self) -> MultiPolygonCapacity {
self.multi_polygon
pub fn multi_polygon_capacity(&self) -> &MultiPolygonCapacity {
&self.multi_polygon
}

pub fn point_compatible(&self) -> bool {
Expand Down Expand Up @@ -221,13 +221,13 @@ impl MixedCapacity {
}

/// The number of bytes an array with this capacity would occupy.
pub fn num_bytes<O: OffsetSizeTrait>(&self) -> usize {
let mut count = self.point * 2 * 8;
count += self.line_string.num_bytes::<O>();
count += self.polygon.num_bytes::<O>();
count += self.multi_point.num_bytes::<O>();
count += self.multi_line_string.num_bytes::<O>();
count += self.multi_polygon.num_bytes::<O>();
pub fn num_bytes<O: OffsetSizeTrait>(&self, dim: usize) -> usize {
let mut count = self.point * dim * 8;
count += self.line_string.num_bytes::<O>(dim);
count += self.polygon.num_bytes::<O>(dim);
count += self.multi_point.num_bytes::<O>(dim);
count += self.multi_line_string.num_bytes::<O>(dim);
count += self.multi_polygon.num_bytes::<O>(dim);
count
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use mixed::{MixedCapacity, MixedGeometryArray, MixedGeometryBuilder};
pub use multilinestring::{MultiLineStringArray, MultiLineStringBuilder, MultiLineStringCapacity};
pub use multipoint::{MultiPointArray, MultiPointBuilder, MultiPointCapacity};
pub use multipolygon::{MultiPolygonArray, MultiPolygonBuilder, MultiPolygonCapacity};
pub use point::{PointArray, PointBuilder};
pub use point::{PointArray, PointBuilder, PointCapacity};
pub use polygon::{PolygonArray, PolygonBuilder, PolygonCapacity};
pub use rect::{RectArray, RectBuilder};

Expand Down
5 changes: 3 additions & 2 deletions src/array/multilinestring/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::algorithm::native::eq::offset_buffer_eq;
Expand Down Expand Up @@ -170,13 +170,14 @@ impl<O: OffsetSizeTrait, const D: usize> MultiLineStringArray<O, D> {
self.ring_offsets.last().to_usize().unwrap(),
self.geom_offsets.last().to_usize().unwrap(),
self.len(),
HashSet::from_iter([D]),
)
}

/// The number of bytes occupied by this array.
pub fn num_bytes(&self) -> usize {
let validity_len = self.validity().map(|v| v.buffer().len()).unwrap_or(0);
validity_len + self.buffer_lengths().num_bytes::<O>()
validity_len + self.buffer_lengths().num_bytes::<O>(D)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/array/multilinestring/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ impl<O: OffsetSizeTrait, const D: usize> GeometryArrayBuilder for MultiLineStrin
coord_type: CoordType,
metadata: Arc<ArrayMetadata>,
) -> Self {
let capacity = MultiLineStringCapacity::new(0, 0, geom_capacity);
let capacity = MultiLineStringCapacity::new(0, 0, geom_capacity, Default::default());
Self::with_capacity_and_options(capacity, coord_type, metadata)
}

Expand Down
Loading
Loading