Skip to content

Commit

Permalink
Cache the result of model::virtual_system_mapping() (#33618)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 95714946c9e40b836237028d076e5963c82a1f1c
  • Loading branch information
goffrie authored and Convex, Inc. committed Feb 7, 2025
1 parent 5a59f76 commit 3414ccd
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion crates/application/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<RT: Runtime> ApplicationTestExt<RT> for Application<RT> {
rt.clone(),
searcher.clone(),
ShutdownSignal::panic(),
virtual_system_mapping(),
virtual_system_mapping().clone(),
args.event_logger.unwrap_or(Arc::new(NoOpUsageEventLogger)),
)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/function_runner/src/in_memory_indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl<RT: Runtime> InMemoryIndexCache<RT> {
database_index_snapshot,
text_index_snapshot,
retention_validator,
virtual_system_mapping(),
virtual_system_mapping().clone(),
usage_tracker,
)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/isolate/src/environment/udf/async_syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl<RT: Runtime> AsyncSyscallProvider<RT> for DatabaseUdfEnvironment<RT> {
let tx = self.phase.tx()?;
let table_mapping = tx.table_mapping().namespace(called_component_id.into());
if let Some(e) =
returns_validator.check_output(&result, &table_mapping, &virtual_system_mapping())
returns_validator.check_output(&result, &table_mapping, virtual_system_mapping())
{
anyhow::bail!(ErrorMetadata::bad_request("InvalidReturnValue", e.message));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/isolate/src/ops/validate_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn op_validate_args<'b, P: OpProvider<'b>>(
.map_err(|err| anyhow::anyhow!(format!("{}", err)))?;

let table_mapping = provider.get_all_table_mappings()?;
match args_validator.check_args(&args_array, &table_mapping, &virtual_system_mapping())? {
match args_validator.check_args(&args_array, &table_mapping, virtual_system_mapping())? {
Some(js_error) => Ok(json!({
"valid": false,
"message": format!("{}", js_error)
Expand Down
7 changes: 2 additions & 5 deletions crates/isolate/src/ops/validate_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ pub fn op_validate_returns<'b, P: OpProvider<'b>>(
let function_result = ConvexValue::try_from(function_result)?;

let table_mapping = provider.get_all_table_mappings()?;
match returns_validator.check_output(
&function_result,
&table_mapping,
&virtual_system_mapping(),
) {
match returns_validator.check_output(&function_result, &table_mapping, virtual_system_mapping())
{
Some(js_error) => Ok(json!({
"valid": false,
"message": format!("{}", js_error)
Expand Down
2 changes: 1 addition & 1 deletion crates/isolate/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<RT: Runtime, P: Persistence> UdfTest<RT, P> {
DbFixturesArgs {
tp: Some(persistence.clone()),
searcher: Some(Arc::new(InProcessSearcher::new(rt.clone()).await?)),
virtual_system_mapping: virtual_system_mapping(),
virtual_system_mapping: virtual_system_mapping().clone(),
..Default::default()
},
)
Expand Down
2 changes: 1 addition & 1 deletion crates/local_backend/src/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub async fn shapes2(
// Table summaries are still bootstrapping, use `Unknown` in the meantime
None => ReducedShape::Unknown,
};
let json = dashboard_shape_json(&shape, &mapping, &virtual_system_mapping())?;
let json = dashboard_shape_json(&shape, &mapping, virtual_system_mapping())?;
out.insert(String::from(table_name.clone()), json);
}
Ok(Json(out))
Expand Down
2 changes: 1 addition & 1 deletion crates/local_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub async fn make_app(
runtime.clone(),
searcher.clone(),
preempt_tx,
virtual_system_mapping(),
virtual_system_mapping().clone(),
Arc::new(NoOpUsageEventLogger),
)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/model/src/components/type_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub fn validate_component_args(
TableMapping::new().namespace(TableNamespace::by_component_TODO());
let virtual_system_mapping = virtual_system_mapping();
validator
.check_value(value, &table_mapping, &virtual_system_mapping)
.check_value(value, &table_mapping, virtual_system_mapping)
.map_err(|validator_error| {
ErrorMetadata::bad_request(
"TypecheckError",
Expand Down
29 changes: 16 additions & 13 deletions crates/model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,22 @@ pub fn component_system_tables() -> Vec<&'static dyn SystemTable> {
]
}

pub fn virtual_system_mapping() -> VirtualSystemMapping {
let mut mapping = VirtualSystemMapping::default();
for table in app_system_tables() {
if let Some((virtual_table_name, virtual_indexes, mapper)) = table.virtual_table() {
mapping.add_table(
virtual_table_name,
table.table_name(),
virtual_indexes,
mapper,
)
pub fn virtual_system_mapping() -> &'static VirtualSystemMapping {
static MAPPING: LazyLock<VirtualSystemMapping> = LazyLock::new(|| {
let mut mapping = VirtualSystemMapping::default();
for table in app_system_tables() {
if let Some((virtual_table_name, virtual_indexes, mapper)) = table.virtual_table() {
mapping.add_table(
virtual_table_name,
table.table_name(),
virtual_indexes,
mapper,
)
}
}
}
mapping
mapping
});
&MAPPING
}

#[cfg(test)]
Expand Down Expand Up @@ -493,7 +496,7 @@ mod test_default_table_numbers {
async fn test_initialize_model(rt: TestRuntime) -> anyhow::Result<()> {
let args = DbFixturesArgs {
tp: Some(Arc::new(TestPersistence::new())),
virtual_system_mapping: virtual_system_mapping(),
virtual_system_mapping: virtual_system_mapping().clone(),
..Default::default()
};
// Initialize
Expand Down
2 changes: 1 addition & 1 deletion crates/model/src/test_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<RT: Runtime> DbFixturesWithModel<RT> for DbFixtures<RT> {
Self::new_with_model_and_args(
rt,
DbFixturesArgs {
virtual_system_mapping: virtual_system_mapping(),
virtual_system_mapping: virtual_system_mapping().clone(),
..Default::default()
},
)
Expand Down
13 changes: 6 additions & 7 deletions crates/udf/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,10 @@ impl ValidatedPathAndArgs {
let table_mapping = &tx.table_mapping().namespace(path.component.into());

// If the UDF has an args validator, check that these args match.
let args_validation_error = analyzed_function.args()?.check_args(
&args,
table_mapping,
&virtual_system_mapping(),
)?;
let args_validation_error =
analyzed_function
.args()?
.check_args(&args, table_mapping, virtual_system_mapping())?;

if let Some(error) = args_validation_error {
return Ok(Err(JsError::from_message(format!(
Expand Down Expand Up @@ -813,7 +812,7 @@ impl ValidatedUdfOutcome {
};

if let Some(js_err) =
returns_validator.check_output(&returns, table_mapping, &virtual_system_mapping())
returns_validator.check_output(&returns, table_mapping, virtual_system_mapping())
{
validated.result = Err(js_err);
};
Expand Down Expand Up @@ -855,7 +854,7 @@ impl ValidatedActionOutcome {
if let Ok(ref json_packed_value) = &validated.result {
let output = json_packed_value.unpack();
if let Some(js_err) =
returns_validator.check_output(&output, table_mapping, &virtual_system_mapping())
returns_validator.check_output(&output, table_mapping, virtual_system_mapping())
{
validated.result = Err(js_err);
}
Expand Down

0 comments on commit 3414ccd

Please sign in to comment.