Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql/opt/invertedexpr: avoid per-span allocations in GeoUnionKeySpansT…
…oSpanExpr This commit optimizes a few heap allocations in `GeoUnionKeySpansToSpanExpr` and in `GeoRPKeyExprToSpanExpr` to avoid per-span/per-expression heap allocations. Before this change, these heap allocations accounted for 27.52% of all heap allocations (by object, `alloc_objects`) in a geospatial query of interest: ``` Type: alloc_objects Time: Aug 21, 2020 at 4:59pm (UTC) Active filters: focus=geoKeyToEncInvertedVal Showing nodes accounting for 61277094, 27.52% of 222698492 total ----------------------------------------------------------+------------- flat flat% sum% cum cum% calls calls% + context ----------------------------------------------------------+------------- 15270121 99.79% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoToSpan /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:46 32768 0.21% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoToSpan /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:45 15302889 6.87% 6.87% 15302889 6.87% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoKeyToEncInvertedVal /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:31 ----------------------------------------------------------+------------- 16187639 53.52% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoToSpan /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:46 14057686 46.48% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoToSpan /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:45 0 0% 6.87% 30245325 13.58% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoKeyToEncInvertedVal /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:32 30245325 100% | github.com/cockroachdb/cockroach/pkg/sql/sqlbase.EncodeTableKey /go/src/github.com/cockroachdb/cockroach/pkg/sql/sqlbase/column_type_encoding.go:75 ----------------------------------------------------------+------------- 15728880 100% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoToSpan /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:46 0 0% 6.87% 15728880 7.06% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoKeyToEncInvertedVal /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:38 15728880 100% | github.com/cockroachdb/cockroach/pkg/roachpb.Key.PrefixEnd /go/src/github.com/cockroachdb/cockroach/pkg/roachpb/data.go:188 ----------------------------------------------------------+------------- ``` These allocations were largely avoidable. This commit removes some of them entirely and reworks the code structure in order to amortize the unavoidable allocations over the set of keys being encoded. The result is a large reduction in allocations. On the same workload, allocations under `geoKeyToEncInvertedVal` dropped from **27.52%** of the workload to **2.23%** of the workload: ``` Type: alloc_objects Time: Aug 21, 2020 at 5:04pm (UTC) Active filters: focus=geoKeyToEncInvertedVal Showing nodes accounting for 3632555, 2.23% of 162651628 total ----------------------------------------------------------+------------- flat flat% sum% cum cum% calls calls% + context ----------------------------------------------------------+------------- 2014518 55.46% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoToSpan /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:53 1618037 44.54% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoToSpan /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:54 0 0% 0% 3632555 2.23% | github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr.geoKeyToEncInvertedVal /go/src/github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr/geo_expression.go:44 3632555 100% | github.com/cockroachdb/cockroach/pkg/util/encoding.EncodeUvarintAscending /go/src/github.com/cockroachdb/cockroach/pkg/util/encoding/encoding.go:375 ----------------------------------------------------------+------------- ``` When applied on top of cockroachdb#53181, this results in a **1.71%** speedup on the following geospatial query: ```sql SELECT Count(census.blkid), Count(subways.name) FROM nyc_census_blocks AS census JOIN nyc_subway_stations AS subways ON ST_Intersects(subways.geom, census.geom); ``` ``` name old ms new ms delta Test/postgis_geometry_tutorial/nyc 136 ±12% 134 ±11% -1.71% (p=0.000 n=976+977) ``` In making this change, we also fix a bug where `geoindex.Key` (a uint64) was being cast to a `tree.DInt` (an int64) during encoding. This could result in overflow and could cause inverted key spans to be generated.
- Loading branch information