Skip to content

Commit d48d85e

Browse files
goffrieConvex, Inc.
authored andcommitted
Skip intermediate serde_json::Value in persistence writes (#36365)
`.json_serialize()` converts a ConvexValue to JSON text in one step, as opposed to `to_internal_json()` which constructs a serde_json::Value which is then serialized and thrown away. GitOrigin-RevId: c03761dbe0cf86d7c0682d155938825bcae06001
1 parent ae91a70 commit d48d85e

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

crates/mysql/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl<RT: Runtime> Persistence for MySqlPersistence<RT> {
316316
update.id,
317317
update.value.clone(),
318318
update.prev_ts,
319-
);
319+
)?;
320320
}
321321
let future = async {
322322
let timer =
@@ -1265,19 +1265,19 @@ fn document_params(
12651265
id: InternalDocumentId,
12661266
maybe_doc: Option<ResolvedDocument>,
12671267
prev_ts: Option<Timestamp>,
1268-
) -> Vec<mysql_async::Value> {
1269-
let (json_value, deleted) = match maybe_doc {
1270-
Some(document) => (document.to_internal_json(), false),
1271-
None => (serde_json::Value::Null, true),
1268+
) -> anyhow::Result<Vec<mysql_async::Value>> {
1269+
let (json_str, deleted) = match maybe_doc {
1270+
Some(document) => (document.value().json_serialize()?, false),
1271+
None => (serde_json::Value::Null.to_string(), true),
12721272
};
12731273

12741274
query.push(internal_doc_id_param(id).into());
12751275
query.push(i64::from(ts).into());
12761276
query.push(internal_id_param(id.table().0).into());
1277-
query.push(json_value.into());
1277+
query.push(mysql_async::Value::Bytes(json_str.into_bytes()));
12781278
query.push(deleted.into());
12791279
query.push(prev_ts.map(i64::from).into());
1280-
query
1280+
Ok(query)
12811281
}
12821282

12831283
fn internal_id_param(id: InternalId) -> Vec<u8> {

crates/postgres/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl Persistence for PostgresPersistence {
306306
update.id,
307307
&update.value,
308308
update.prev_ts,
309-
));
309+
)?);
310310
}
311311
let future = async {
312312
let timer = metrics::insert_document_chunk_timer();
@@ -324,7 +324,7 @@ impl Persistence for PostgresPersistence {
324324
update.id,
325325
&update.value,
326326
update.prev_ts,
327-
);
327+
)?;
328328
let future = async {
329329
let timer = metrics::insert_one_document_timer();
330330
tx.execute_raw(&insert_document, params).await?;
@@ -400,7 +400,10 @@ impl Persistence for PostgresPersistence {
400400
.transact(move |tx| {
401401
async move {
402402
let stmt = tx.prepare_cached(WRITE_PERSISTENCE_GLOBAL).await?;
403-
let params = [Param::PersistenceGlobalKey(key), Param::JsonValue(value)];
403+
let params = [
404+
Param::PersistenceGlobalKey(key),
405+
Param::JsonValue(value.to_string()),
406+
];
404407
tx.execute_raw(&stmt, params).await?;
405408
Ok(())
406409
}
@@ -1206,13 +1209,13 @@ fn document_params(
12061209
id: InternalDocumentId,
12071210
maybe_document: &Option<ResolvedDocument>,
12081211
prev_ts: Option<Timestamp>,
1209-
) -> [Param; NUM_DOCUMENT_PARAMS] {
1212+
) -> anyhow::Result<[Param; NUM_DOCUMENT_PARAMS]> {
12101213
let (json_value, deleted) = match maybe_document {
1211-
Some(doc) => (doc.to_internal_json(), false),
1212-
None => (JsonValue::Null, true),
1214+
Some(doc) => (doc.value().json_serialize()?, false),
1215+
None => (JsonValue::Null.to_string(), true),
12131216
};
12141217

1215-
[
1218+
Ok([
12161219
internal_doc_id_param(id),
12171220
Param::Ts(i64::from(ts)),
12181221
Param::TableId(id.table()),
@@ -1222,7 +1225,7 @@ fn document_params(
12221225
Some(prev_ts) => Param::Ts(i64::from(prev_ts)),
12231226
None => Param::None,
12241227
},
1225-
]
1228+
])
12261229
}
12271230

12281231
fn internal_id_param(id: InternalId) -> Param {
@@ -1270,7 +1273,7 @@ enum Param {
12701273
Ts(i64),
12711274
Limit(i64),
12721275
TableId(TabletId),
1273-
JsonValue(JsonValue),
1276+
JsonValue(String),
12741277
Deleted(bool),
12751278
Bytes(Vec<u8>),
12761279
PersistenceGlobalKey(PersistenceGlobalKey),
@@ -1288,7 +1291,7 @@ impl ToSql for Param {
12881291
Param::None => Ok(IsNull::Yes),
12891292
Param::Ts(ts) => ts.to_sql(ty, out),
12901293
Param::TableId(s) => s.0 .0.to_vec().to_sql(ty, out),
1291-
Param::JsonValue(v) => serde_json::to_vec(v)?.to_sql(ty, out),
1294+
Param::JsonValue(v) => v.as_bytes().to_sql(ty, out),
12921295
Param::Deleted(d) => d.to_sql(ty, out),
12931296
Param::Bytes(v) => v.to_sql(ty, out),
12941297
Param::Limit(v) => v.to_sql(ty, out),

0 commit comments

Comments
 (0)