Skip to content

Commit

Permalink
replace linkme with inventory
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Mar 12, 2024
1 parent 5a8a6a2 commit 4e4ddfc
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 48 deletions.
31 changes: 8 additions & 23 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion src/expr/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ either = "1"
enum-as-inner = "0.6"
futures-async-stream = { workspace = true }
futures-util = "0.3"
inventory = "0.3"
itertools = "0.12"
linkme = { version = "0.3", features = ["used_linker"] }
md5 = "0.7"
moka = { version = "0.12", features = ["sync"] }
num-traits = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion src/expr/core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
pub use async_trait::async_trait;
pub use futures_async_stream::try_stream;
pub use futures_util::stream::BoxStream;
pub use inventory;
pub use itertools::multizip;
pub use linkme;
20 changes: 13 additions & 7 deletions src/expr/core/src/sig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ use crate::ExprError;
/// The global registry of all function signatures.
pub static FUNCTION_REGISTRY: LazyLock<FunctionRegistry> = LazyLock::new(|| {
let mut map = FunctionRegistry::default();
tracing::info!("found {} functions", FUNCTIONS.len());
for f in FUNCTIONS {
map.insert(f());
for f in inventory::iter::<FuncSignBuilder> {
map.insert((f.0)());
}
tracing::info!("found {} functions", map.len());
map
});

Expand Down Expand Up @@ -144,6 +144,11 @@ impl FunctionRegistry {
(sig.type_infer)(args)
}

/// Returns the number of function signatures in the registry.
pub fn len(&self) -> usize {
self.0.len()
}

/// Returns an iterator of all function signatures.
pub fn iter(&self) -> impl Iterator<Item = &FuncSign> {
self.0.values().flatten()
Expand Down Expand Up @@ -186,6 +191,11 @@ pub struct FuncSign {
pub deprecated: bool,
}

/// A new type around a function that returns a [`FuncSign`]. Used for registering
/// function signatures from `#[function]` attributes in the entire codebase.
pub struct FuncSignBuilder(pub fn() -> FuncSign);
inventory::collect!(FuncSignBuilder);

impl fmt::Debug for FuncSign {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
Expand Down Expand Up @@ -457,7 +467,3 @@ pub enum FuncBuilder {
},
Udf,
}

/// A static distributed slice of functions defined by `#[function]`.
#[linkme::distributed_slice]
pub static FUNCTIONS: [fn() -> FuncSign];
1 change: 0 additions & 1 deletion src/expr/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ hex = "0.4"
icelake = { workspace = true }
itertools = "0.12"
jsonbb = "0.1.2"
linkme = { version = "0.3", features = ["used_linker"] }
md5 = "0.7"
num-traits = "0.2"
openssl = { version = "0.10", features = ["vendored"] }
Expand Down
12 changes: 9 additions & 3 deletions src/expr/macro/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ impl FunctionAttr {
let deprecated = self.deprecated;

Ok(quote! {
#[risingwave_expr::codegen::linkme::distributed_slice(risingwave_expr::sig::FUNCTIONS)]
fn #ctor_name() -> risingwave_expr::sig::FuncSign {
use risingwave_common::types::{DataType, DataTypeName};
use risingwave_expr::sig::{FuncSign, SigDataType, FuncBuilder};
Expand All @@ -177,6 +176,9 @@ impl FunctionAttr {
deprecated: #deprecated,
}
}
risingwave_expr::codegen::inventory::submit! {
risingwave_expr::sig::FuncSignBuilder(#ctor_name)
}
})
}

Expand Down Expand Up @@ -652,7 +654,6 @@ impl FunctionAttr {
let deprecated = self.deprecated;

Ok(quote! {
#[risingwave_expr::codegen::linkme::distributed_slice(risingwave_expr::sig::FUNCTIONS)]
fn #ctor_name() -> risingwave_expr::sig::FuncSign {
use risingwave_common::types::{DataType, DataTypeName};
use risingwave_expr::sig::{FuncSign, SigDataType, FuncBuilder};
Expand All @@ -672,6 +673,9 @@ impl FunctionAttr {
deprecated: #deprecated,
}
}
risingwave_expr::codegen::inventory::submit! {
risingwave_expr::sig::FuncSignBuilder(#ctor_name)
}
})
}

Expand Down Expand Up @@ -984,7 +988,6 @@ impl FunctionAttr {
let deprecated = self.deprecated;

Ok(quote! {
#[risingwave_expr::codegen::linkme::distributed_slice(risingwave_expr::sig::FUNCTIONS)]
fn #ctor_name() -> risingwave_expr::sig::FuncSign {
use risingwave_common::types::{DataType, DataTypeName};
use risingwave_expr::sig::{FuncSign, SigDataType, FuncBuilder};
Expand All @@ -999,6 +1002,9 @@ impl FunctionAttr {
deprecated: #deprecated,
}
}
risingwave_expr::codegen::inventory::submit! {
risingwave_expr::sig::FuncSignBuilder(#ctor_name)
}
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ futures = { version = "0.3", default-features = false, features = ["alloc"] }
futures-async-stream = { workspace = true }
iana-time-zone = "0.1"
icelake = { workspace = true }
inventory = "0.3"
itertools = "0.12"
linkme = { version = "0.3", features = ["used_linker"] }
maplit = "1"
md5 = "0.7.0"
memcomparable = "0.2"
Expand Down
8 changes: 6 additions & 2 deletions src/frontend/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ fn gen_sys_table(attr: Attr, item_fn: ItemFn) -> Result<TokenStream2> {
let handle_error = return_result.then(|| quote!(?));

Ok(quote! {
#[linkme::distributed_slice(crate::catalog::system_catalog::SYS_CATALOGS_SLICE)]
#[no_mangle] // to prevent duplicate schema.table name
fn #gen_fn_name() -> crate::catalog::system_catalog::BuiltinCatalog {
const _: () = {
Expand All @@ -136,6 +135,9 @@ fn gen_sys_table(attr: Attr, item_fn: ItemFn) -> Result<TokenStream2> {
}),
})
}
::inventory::submit! {
crate::catalog::system_catalog::BuiltinCatalogBuilder(#gen_fn_name)
}
})
}

Expand All @@ -155,7 +157,6 @@ fn gen_sys_view(attr: Attr, item_struct: ItemStruct) -> Result<TokenStream2> {
};

Ok(quote! {
#[linkme::distributed_slice(crate::catalog::system_catalog::SYS_CATALOGS_SLICE)]
#[no_mangle] // to prevent duplicate schema.table name
fn #gen_fn_name() -> crate::catalog::system_catalog::BuiltinCatalog {
let fields = #struct_type::fields();
Expand All @@ -166,5 +167,8 @@ fn gen_sys_view(attr: Attr, item_struct: ItemStruct) -> Result<TokenStream2> {
columns: fields,
})
}
::inventory::submit! {
crate::catalog::system_catalog::BuiltinCatalogBuilder(#gen_fn_name)
}
})
}
20 changes: 11 additions & 9 deletions src/frontend/src/catalog/system_catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,21 +325,23 @@ pub fn get_sys_views_in_schema(schema_name: &str) -> Vec<Arc<ViewCatalog>> {
.collect()
}

/// A new type around a function that returns a [`BuiltinCatalog`]. Used for registering
/// builtin catalogs from `#[system_catalog]` attributes in the `frontend` crate.
pub(crate) struct BuiltinCatalogBuilder(pub fn() -> BuiltinCatalog);
inventory::collect!(BuiltinCatalogBuilder);

/// The global registry of all builtin catalogs.
pub static SYS_CATALOGS: LazyLock<SystemCatalog> = LazyLock::new(|| {
tracing::info!("found {} catalogs", SYS_CATALOGS_SLICE.len());
assert!(SYS_CATALOGS_SLICE.len() <= MAX_SYS_CATALOG_NUM as usize);
let catalogs = SYS_CATALOGS_SLICE
.iter()
.map(|f| f())
let catalogs = inventory::iter::<BuiltinCatalogBuilder>
.into_iter()
.map(|b| (b.0)())
.sorted_by_key(|c| c.full_name())
.collect();
.collect_vec();
assert!(catalogs.len() <= MAX_SYS_CATALOG_NUM as usize);
tracing::info!("found {} catalogs", catalogs.len());
SystemCatalog { catalogs }
});

#[linkme::distributed_slice]
pub static SYS_CATALOGS_SLICE: [fn() -> BuiltinCatalog];

#[async_trait]
impl SysCatalogReader for SysCatalogReaderImpl {
async fn read_table(&self, table_id: &TableId) -> Result<DataChunk, BoxedError> {
Expand Down

0 comments on commit 4e4ddfc

Please sign in to comment.