Skip to content

Commit

Permalink
Replace usages of lazy_static with LazyLock (#1214)
Browse files Browse the repository at this point in the history
fix #1139
robert3005 authored Nov 5, 2024
1 parent 6bcc279 commit 6904ea0
Showing 18 changed files with 334 additions and 197 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -96,8 +96,6 @@ humansize = "2.1.3"
indicatif = "0.17.8"
itertools = "0.13.0"
jiff = "0.1.8"
lazy_static = "1.4.0"
leb128 = "0.2.5"
libfuzzer-sys = "0.4"
log = "0.4.21"
mimalloc = "0.1.42"
@@ -156,7 +154,7 @@ vortex-runend-bool = { version = "0.14.0", path = "./encodings/runend-bool" }
vortex-scalar = { version = "0.14.0", path = "./vortex-scalar", default-features = false }
vortex-schema = { version = "0.14.0", path = "./vortex-schema" }
vortex-serde = { version = "0.14.0", path = "./vortex-serde", default-features = false }
vortex-all = { version = "0.14.0", path = "./vortex-all" }
vortex-all = { version = "0.14.0", path = "./vortex-all" }
vortex-sampling-compressor = { version = "0.14.0", path = "./vortex-sampling-compressor" }
vortex-zigzag = { version = "0.14.0", path = "./encodings/zigzag" }
# END crates published by this project
1 change: 0 additions & 1 deletion bench-vortex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@ homedir = { workspace = true }
humansize = { workspace = true }
indicatif = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
mimalloc = { workspace = true }
object_store = { workspace = true, features = ["aws"] }
23 changes: 11 additions & 12 deletions bench-vortex/benches/datafusion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use arrow_array::builder::{StringBuilder, UInt32Builder};
use arrow_array::RecordBatch;
@@ -11,7 +11,6 @@ use datafusion::execution::memory_pool::human_readable_size;
use datafusion::functions_aggregate::count::count_distinct;
use datafusion::logical_expr::lit;
use datafusion::prelude::{col, DataFrame, SessionContext};
use lazy_static::lazy_static;
use vortex::aliases::hash_set::HashSet;
use vortex::compress::CompressionStrategy;
use vortex::dict::DictEncoding;
@@ -26,24 +25,24 @@ use vortex::sampling_compressor::SamplingCompressor;
use vortex::{Array, Context};
use vortex_datafusion::memory::{VortexMemTable, VortexMemTableOptions};

lazy_static! {
pub static ref CTX: Context = Context::default().with_encodings([
pub static CTX: LazyLock<Context> = LazyLock::new(|| {
Context::default().with_encodings([
&BitPackedEncoding as EncodingRef,
&DictEncoding,
&FoREncoding,
&DeltaEncoding
]);
}
&DeltaEncoding,
])
});

lazy_static! {
pub static ref COMPRESSORS: HashSet<CompressorRef<'static>> = [
pub static COMPRESSORS: LazyLock<HashSet<CompressorRef<'static>>> = LazyLock::new(|| {
[
&BITPACK_WITH_PATCHES as CompressorRef<'static>,
&DictCompressor,
&FoRCompressor,
&DeltaCompressor
&DeltaCompressor,
]
.into();
}
.into()
});

fn toy_dataset_arrow() -> RecordBatch {
// 64,000 rows of string and numeric data.
13 changes: 7 additions & 6 deletions bench-vortex/benches/tokio_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use lazy_static::lazy_static;
use tokio::runtime::Runtime;
use std::sync::LazyLock;

use tokio::runtime::{Builder, Runtime};
use vortex::error::{VortexError, VortexExpect};

lazy_static! {
pub static ref TOKIO_RUNTIME: Runtime = tokio::runtime::Builder::new_current_thread()
pub static TOKIO_RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
Builder::new_current_thread()
.enable_all()
.build()
.map_err(VortexError::IOError)
.vortex_expect("tokio runtime must not fail to start");
}
.vortex_expect("tokio runtime must not fail to start")
});
22 changes: 12 additions & 10 deletions bench-vortex/src/lib.rs
Original file line number Diff line number Diff line change
@@ -4,11 +4,10 @@ use std::env::temp_dir;
use std::fs::{create_dir_all, File};
use std::future::Future;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use arrow_array::RecordBatchReader;
use itertools::Itertools;
use lazy_static::lazy_static;
use log::LevelFilter;
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use simplelog::{ColorChoice, Config, TermLogger, TerminalMode};
@@ -44,13 +43,16 @@ pub mod taxi_data;
pub mod tpch;
pub mod vortex_utils;

lazy_static! {
pub static ref CTX: Arc<Context> = Arc::new(
pub static CTX: LazyLock<Arc<Context>> = LazyLock::new(|| {
Arc::new(
Context::default()
.with_encodings(SamplingCompressor::default().used_encodings())
.with_encoding(&DeltaEncoding)
);
pub static ref COMPRESSORS: HashSet<CompressorRef<'static>> = [
.with_encoding(&DeltaEncoding),
)
});

pub static COMPRESSORS: LazyLock<HashSet<CompressorRef<'static>>> = LazyLock::new(|| {
[
&ALPCompressor as CompressorRef<'static>,
&ALPRDCompressor,
&DictCompressor,
@@ -60,10 +62,10 @@ lazy_static! {
&DateTimePartsCompressor,
&DEFAULT_RUN_END_COMPRESSOR,
&RoaringBoolCompressor,
&SparseCompressor
&SparseCompressor,
]
.into();
}
.into()
});

/// Creates a file if it doesn't already exist.
/// NB: Does NOT modify the given path to ensure that it resides in the data directory.
330 changes: 230 additions & 100 deletions bench-vortex/src/public_bi_data.rs

Large diffs are not rendered by default.

60 changes: 41 additions & 19 deletions bench-vortex/src/tpch/schema.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
use std::sync::LazyLock;

/// Arrow schemas for TPC-H tables.
///
/// Adapted from the SQL definitions in https://github.com/dimitri/tpch-citus/blob/master/schema/tpch-schema.sql
use arrow_schema::{DataType, Field, Schema};
use lazy_static::lazy_static;

lazy_static! {
pub static ref NATION: Schema = Schema::new(vec![
pub static NATION: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("n_nationkey", DataType::Int64, false),
Field::new("n_name", DataType::Utf8View, false),
Field::new("n_regionkey", DataType::Int64, false),
Field::new("n_comment", DataType::Utf8View, true),
]);
pub static ref REGION: Schema = Schema::new(vec![
])
});

pub static REGION: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("r_regionkey", DataType::Int64, false),
Field::new("r_name", DataType::Utf8View, false),
Field::new("r_comment", DataType::Utf8View, true),
]);
pub static ref PART: Schema = Schema::new(vec![
])
});

pub static PART: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("p_partkey", DataType::Int64, false),
Field::new("p_name", DataType::Utf8View, false),
Field::new("p_mfgr", DataType::Utf8View, false),
@@ -26,24 +33,33 @@ lazy_static! {
Field::new("p_container", DataType::Utf8View, false),
Field::new("p_retailprice", DataType::Float64, false),
Field::new("p_comment", DataType::Utf8View, false),
]);
pub static ref SUPPLIER: Schema = Schema::new(vec![
])
});

pub static SUPPLIER: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("s_suppkey", DataType::Int64, false),
Field::new("s_name", DataType::Utf8View, false),
Field::new("s_address", DataType::Utf8View, false),
Field::new("s_nationkey", DataType::Int32, false),
Field::new("s_phone", DataType::Utf8View, false),
Field::new("s_acctbal", DataType::Float64, false),
Field::new("s_comment", DataType::Utf8View, false),
]);
pub static ref PARTSUPP: Schema = Schema::new(vec![
])
});

pub static PARTSUPP: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("ps_partkey", DataType::Int64, false),
Field::new("ps_suppkey", DataType::Int64, false),
Field::new("ps_availqty", DataType::Int64, false),
Field::new("ps_supplycost", DataType::Float64, false),
Field::new("ps_comment", DataType::Utf8View, false),
]);
pub static ref CUSTOMER: Schema = Schema::new(vec![
])
});

pub static CUSTOMER: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("c_custkey", DataType::Int64, false),
Field::new("c_name", DataType::Utf8View, false),
Field::new("c_address", DataType::Utf8View, false),
@@ -52,8 +68,11 @@ lazy_static! {
Field::new("c_acctbal", DataType::Float64, false),
Field::new("c_mktsegment", DataType::Utf8View, false),
Field::new("c_comment", DataType::Utf8View, false),
]);
pub static ref ORDERS: Schema = Schema::new(vec![
])
});

pub static ORDERS: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("o_orderkey", DataType::Int64, false),
Field::new("o_custkey", DataType::Int64, false),
Field::new("o_orderstatus", DataType::Utf8View, false),
@@ -63,8 +82,11 @@ lazy_static! {
Field::new("o_clerk", DataType::Utf8View, false),
Field::new("o_shippriority", DataType::Int32, false),
Field::new("o_comment", DataType::Utf8View, false),
]);
pub static ref LINEITEM: Schema = Schema::new(vec![
])
});

pub static LINEITEM: LazyLock<Schema> = LazyLock::new(|| {
Schema::new(vec![
Field::new("l_orderkey", DataType::Int64, false),
Field::new("l_partkey", DataType::Int64, false),
Field::new("l_suppkey", DataType::Int64, false),
@@ -81,5 +103,5 @@ lazy_static! {
Field::new("l_shipinstruct", DataType::Utf8View, false),
Field::new("l_shipmode", DataType::Utf8View, false),
Field::new("l_comment", DataType::Utf8View, false),
]);
}
])
});
1 change: 0 additions & 1 deletion pyvortex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@ arrow = { workspace = true, features = ["pyarrow"] }
flexbuffers = { workspace = true }
futures = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
paste = { workspace = true }
pyo3 = { workspace = true }
11 changes: 6 additions & 5 deletions pyvortex/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(unsafe_op_in_unsafe_fn)]

use std::sync::LazyLock;

use array::PyArray;
use expr::PyExpr;
use pyo3::exceptions::PyRuntimeError;
@@ -16,17 +18,16 @@ mod io;
mod object_store_urls;
mod python_repr;
mod scalar;
use lazy_static::lazy_static;
use log::LevelFilter;
use pyo3_log::{Caching, Logger};
use tokio::runtime::Runtime;
use vortex::error::{VortexError, VortexExpect as _};

lazy_static! {
static ref TOKIO_RUNTIME: Runtime = Runtime::new()
pub static TOKIO_RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
Runtime::new()
.map_err(VortexError::IOError)
.vortex_expect("tokio runtime must not fail to start");
}
.vortex_expect("tokio runtime must not fail to start")
});

/// Vortex is an Apache Arrow-compatible toolkit for working with compressed array data.
#[pymodule]
1 change: 0 additions & 1 deletion vortex-array/Cargo.toml
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@ futures-util = { workspace = true }
hashbrown = { workspace = true }
humansize = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
num-traits = { workspace = true }
num_enum = { workspace = true }
1 change: 0 additions & 1 deletion vortex-datafusion/Cargo.toml
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@ datafusion-physical-expr = { workspace = true }
datafusion-physical-plan = { workspace = true }
futures = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
object_store = { workspace = true }
pin-project = { workspace = true }
18 changes: 8 additions & 10 deletions vortex-datafusion/src/plans.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
use std::any::Any;
use std::fmt::{Debug, Formatter};
use std::pin::Pin;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use std::task::{Context, Poll};

use arrow_array::cast::AsArray;
@@ -17,7 +17,6 @@ use datafusion_physical_plan::{
DisplayAs, DisplayFormatType, ExecutionMode, ExecutionPlan, PlanProperties,
};
use futures::{ready, Stream};
use lazy_static::lazy_static;
use pin_project::pin_project;
use vortex_array::array::ChunkedArray;
use vortex_array::arrow::FromArrowArray;
@@ -38,14 +37,13 @@ pub(crate) struct RowSelectorExec {
chunked_array: ChunkedArray,
}

lazy_static! {
static ref ROW_SELECTOR_SCHEMA_REF: SchemaRef =
Arc::new(Schema::new(vec![arrow_schema::Field::new(
"row_idx",
DataType::UInt64,
false
)]));
}
static ROW_SELECTOR_SCHEMA_REF: LazyLock<SchemaRef> = LazyLock::new(|| {
Arc::new(Schema::new(vec![arrow_schema::Field::new(
"row_idx",
DataType::UInt64,
false,
)]))
});

impl RowSelectorExec {
pub(crate) fn try_new(
1 change: 0 additions & 1 deletion vortex-datetime-dtype/Cargo.toml
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ readme = { workspace = true }
[dependencies]
arrow-schema = { workspace = true, optional = true }
jiff = { workspace = true }
lazy_static = { workspace = true }
num_enum = { workspace = true }
serde = { workspace = true, features = ["derive"] }
vortex-dtype = { workspace = true, features = ["serde"] }
11 changes: 4 additions & 7 deletions vortex-datetime-dtype/src/temporal.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use std::fmt::Display;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use jiff::civil::{Date, Time};
use jiff::{Timestamp, Zoned};
use lazy_static::lazy_static;
use vortex_dtype::ExtID;

use crate::unit::TimeUnit;

lazy_static! {
pub static ref TIME_ID: ExtID = ExtID::from("vortex.time");
pub static ref DATE_ID: ExtID = ExtID::from("vortex.date");
pub static ref TIMESTAMP_ID: ExtID = ExtID::from("vortex.timestamp");
}
pub static TIME_ID: LazyLock<ExtID> = LazyLock::new(|| ExtID::from("vortex.time"));
pub static DATE_ID: LazyLock<ExtID> = LazyLock::new(|| ExtID::from("vortex.date"));
pub static TIMESTAMP_ID: LazyLock<ExtID> = LazyLock::new(|| ExtID::from("vortex.timestamp"));

pub fn is_temporal_ext_type(id: &ExtID) -> bool {
[&DATE_ID as &ExtID, &TIME_ID, &TIMESTAMP_ID].contains(&id)
1 change: 0 additions & 1 deletion vortex-sampling-compressor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@ readme = { workspace = true }
arbitrary = { workspace = true, optional = true }
fsst-rs = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
rand = { workspace = true }
vortex-alp = { workspace = true }
25 changes: 14 additions & 11 deletions vortex-sampling-compressor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use compressors::bitpacked::BITPACK_WITH_PATCHES;
use compressors::chunked::DEFAULT_CHUNKED_COMPRESSOR;
use compressors::fsst::FSSTCompressor;
use compressors::struct_::StructCompressor;
use lazy_static::lazy_static;
use log::{debug, warn};
use rand::rngs::StdRng;
use rand::SeedableRng;
@@ -47,8 +46,8 @@ pub mod compressors;
mod constants;
mod sampling;

lazy_static! {
pub static ref DEFAULT_COMPRESSORS: [CompressorRef<'static>; 9] = [
pub static DEFAULT_COMPRESSORS: LazyLock<[CompressorRef<'static>; 9]> = LazyLock::new(|| {
[
&ALPCompressor as CompressorRef,
&BITPACK_WITH_PATCHES,
&DateTimePartsCompressor,
@@ -61,19 +60,23 @@ lazy_static! {
// &RoaringIntCompressor,
&SparseCompressor,
&ZigZagCompressor,
];
]
});

pub static ref FASTEST_COMPRESSORS: [CompressorRef<'static>; 7] = [
pub static FASTEST_COMPRESSORS: LazyLock<[CompressorRef<'static>; 7]> = LazyLock::new(|| {
[
&BITPACK_WITH_PATCHES,
&DateTimePartsCompressor,
&DEFAULT_RUN_END_COMPRESSOR, // replace with FastLanes RLE
&DictCompressor, // replace with FastLanes Dictionary
&DictCompressor, // replace with FastLanes Dictionary
&FoRCompressor,
&SparseCompressor,
&ZigZagCompressor,
];
]
});

pub static ref ALL_COMPRESSORS_CONTEXT: Arc<Context> = Arc::new(Context::default().with_encodings([
pub static ALL_COMPRESSORS_CONTEXT: LazyLock<Arc<Context>> = LazyLock::new(|| {
Arc::new(Context::default().with_encodings([
&ALPEncoding as EncodingRef,
&ByteBoolEncoding,
&DateTimePartsEncoding,
@@ -88,8 +91,8 @@ lazy_static! {
&RunEndBoolEncoding,
&ZigZagEncoding,
&ALPRDEncoding,
]));
}
]))
});

#[derive(Debug, Clone)]
pub enum Objective {
1 change: 0 additions & 1 deletion vortex-serde/Cargo.toml
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@ futures = { workspace = true }
futures-executor = { workspace = true }
futures-util = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
monoio = { workspace = true, optional = true, features = ["bytes"] }
object_store = { workspace = true, optional = true }
once_cell = { workspace = true }

0 comments on commit 6904ea0

Please sign in to comment.