Skip to content

Commit b726995

Browse files
goffrieConvex, Inc.
authored andcommitted
Take document by reference in PackedDocument::pack (#35570)
This eliminates a few document clones. GitOrigin-RevId: 7625f4e332690b955ab27c9054320d8f66fb6b87
1 parent 37e50a7 commit b726995

File tree

12 files changed

+61
-47
lines changed

12 files changed

+61
-47
lines changed

crates/common/src/document.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,10 @@ impl DeveloperDocument {
775775
pub struct PackedDocument(PackedValue<ByteBuffer>, ResolvedDocumentId);
776776

777777
impl PackedDocument {
778-
pub fn pack(document: ResolvedDocument) -> Self {
778+
pub fn pack(document: &ResolvedDocument) -> Self {
779779
let document_id = document.id();
780-
let value = document.document.into_value().0.into();
781-
Self(PackedValue::pack(&value), document_id)
780+
let value = document.document.value();
781+
Self(PackedValue::pack_object(value), document_id)
782782
}
783783

784784
pub fn unpack(&self) -> ResolvedDocument {
@@ -1133,7 +1133,7 @@ mod tests {
11331133
let index_key_bytes = doc.index_key(&field_paths, ver).into_bytes();
11341134
assert_eq!(
11351135
index_key_bytes,
1136-
*PackedDocument::pack(doc).index_key(
1136+
*PackedDocument::pack(&doc).index_key(
11371137
&field_paths, ver, &mut IndexKeyBuffer::new()
11381138
),
11391139
);

crates/database/benches/subscriptions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn load_datasets(
136136
let value = assert_obj!("body" => d.text);
137137
let creation_time = CreationTime::try_from(1.)?;
138138
let document = ResolvedDocument::new(id, creation_time, value)?;
139-
documents.push(PackedDocument::pack(document));
139+
documents.push(PackedDocument::pack(&document));
140140
}
141141

142142
let terms_by_frequency: Vec<String> = frequency_map

crates/database/src/committer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -637,15 +637,15 @@ impl<RT: Runtime> Committer<RT> {
637637
}
638638
timer.finish();
639639

640-
let updates: Vec<_> = transaction.writes.into_coalesced_writes().collect();
640+
let updates: Vec<_> = transaction.writes.coalesced_writes().collect();
641641
// The updates are ordered using table_dependency_sort_key,
642642
// which is the same order they should be applied to database metadata
643643
// and index data structures
644644
let mut ordered_updates = updates;
645645
ordered_updates.sort_by_key(|(id, update)| {
646646
table_dependency_sort_key(
647647
BootstrapTableIds::new(&transaction.table_mapping),
648-
InternalDocumentId::from(*id),
648+
InternalDocumentId::from(**id),
649649
update.new_document.as_ref(),
650650
)
651651
});
@@ -663,7 +663,7 @@ impl<RT: Runtime> Committer<RT> {
663663
commit_ts,
664664
ordered_updates
665665
.into_iter()
666-
.map(|(id, update)| (id, PackedDocumentUpdate::pack(update.into())))
666+
.map(|(&id, update)| (id, PackedDocumentUpdate::pack(update)))
667667
.collect(),
668668
write_source,
669669
);
@@ -679,7 +679,7 @@ impl<RT: Runtime> Committer<RT> {
679679
fn compute_writes(
680680
&self,
681681
commit_ts: Timestamp,
682-
ordered_updates: &Vec<(ResolvedDocumentId, DocumentUpdateWithPrevTs)>,
682+
ordered_updates: &Vec<(&ResolvedDocumentId, &DocumentUpdateWithPrevTs)>,
683683
) -> anyhow::Result<(
684684
Vec<ValidatedDocumentWrite>,
685685
BTreeSet<(Timestamp, DatabaseIndexUpdate)>,
@@ -691,7 +691,7 @@ impl<RT: Runtime> Committer<RT> {
691691
// same tables and indexes as the base snapshot and the final publishing
692692
// snapshot. Therefore index writes can be computed from the current snapshot.
693693
let mut current_snapshot = self.snapshot_manager.read().latest_snapshot();
694-
for (id, document_update) in ordered_updates.iter() {
694+
for &(id, document_update) in ordered_updates.iter() {
695695
let (updates, doc_in_vector_index) =
696696
current_snapshot.update(document_update, commit_ts)?;
697697
index_writes.extend(updates);

crates/database/src/reads.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ mod tests {
887887
let doc_without_word = create_document_with_one_field(id, field_name, val!(document_text))?;
888888
Ok(read_set
889889
.overlaps(
890-
&PackedDocument::pack(doc_without_word),
890+
&PackedDocument::pack(&doc_without_word),
891891
PersistenceVersion::default(),
892892
)
893893
.is_some())
@@ -924,7 +924,7 @@ mod tests {
924924
assert_eq!(
925925
read_set
926926
.overlaps(
927-
&PackedDocument::pack(doc_with_word),
927+
&PackedDocument::pack(&doc_with_word),
928928
PersistenceVersion::default()
929929
)
930930
.unwrap()
@@ -940,7 +940,7 @@ mod tests {
940940
)?;
941941
assert_eq!(
942942
read_set.overlaps(
943-
&PackedDocument::pack(doc_without_word),
943+
&PackedDocument::pack(&doc_without_word),
944944
PersistenceVersion::default()
945945
),
946946
None
@@ -980,7 +980,7 @@ mod tests {
980980
assert_eq!(
981981
read_set
982982
.overlaps(
983-
&PackedDocument::pack(doc_with_explicit_null),
983+
&PackedDocument::pack(&doc_with_explicit_null),
984984
PersistenceVersion::default()
985985
)
986986
.unwrap()
@@ -996,7 +996,7 @@ mod tests {
996996
)?;
997997
assert_eq!(
998998
read_set.overlaps(
999-
&PackedDocument::pack(doc_with_missing_field),
999+
&PackedDocument::pack(&doc_with_missing_field),
10001000
PersistenceVersion::default()
10011001
),
10021002
None
@@ -1010,7 +1010,7 @@ mod tests {
10101010
)?;
10111011
assert_eq!(
10121012
read_set.overlaps(
1013-
&PackedDocument::pack(doc_with_implicit_null),
1013+
&PackedDocument::pack(&doc_with_implicit_null),
10141014
PersistenceVersion::default()
10151015
),
10161016
None
@@ -1057,7 +1057,7 @@ mod tests {
10571057
assert_eq!(
10581058
read_set
10591059
.overlaps(
1060-
&PackedDocument::pack(doc_with_explicit_null),
1060+
&PackedDocument::pack(&doc_with_explicit_null),
10611061
PersistenceVersion::default()
10621062
)
10631063
.unwrap()
@@ -1073,7 +1073,7 @@ mod tests {
10731073
)?;
10741074
assert_eq!(
10751075
read_set.overlaps(
1076-
&PackedDocument::pack(doc_with_missing_field),
1076+
&PackedDocument::pack(&doc_with_missing_field),
10771077
PersistenceVersion::default()
10781078
),
10791079
None
@@ -1087,7 +1087,7 @@ mod tests {
10871087
)?;
10881088
assert_eq!(
10891089
read_set.overlaps(
1090-
&PackedDocument::pack(doc_with_implicit_null),
1090+
&PackedDocument::pack(&doc_with_implicit_null),
10911091
PersistenceVersion::default()
10921092
),
10931093
None

crates/database/src/subscription.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ mod tests {
710710
);
711711
assert_eq!(*index_name.table(), id.tablet_id);
712712

713-
let document = pack(create_document(
713+
let document = pack(&create_document(
714714
query.field_path.clone(),
715715
match &query.term {
716716
TextQueryTerm::Exact(term) => term.clone(),
@@ -725,7 +725,7 @@ mod tests {
725725
result
726726
}
727727

728-
fn pack(doc: ResolvedDocument) -> PackedDocument {
728+
fn pack(doc: &ResolvedDocument) -> PackedDocument {
729729
PackedDocument::pack(doc)
730730
}
731731

crates/database/src/tests/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ async fn test_query_filter_readset(rt: TestRuntime) -> anyhow::Result<()> {
22802280
assert!(token
22812281
.reads()
22822282
.overlaps(
2283-
&PackedDocument::pack(out_of_range_doc),
2283+
&PackedDocument::pack(&out_of_range_doc),
22842284
PersistenceVersion::default()
22852285
)
22862286
.is_none());
@@ -2289,7 +2289,7 @@ async fn test_query_filter_readset(rt: TestRuntime) -> anyhow::Result<()> {
22892289
assert!(token
22902290
.reads()
22912291
.overlaps(
2292-
&PackedDocument::pack(in_range_doc),
2292+
&PackedDocument::pack(&in_range_doc),
22932293
PersistenceVersion::default()
22942294
)
22952295
.is_some());
@@ -2355,7 +2355,7 @@ async fn test_query_readset_empty_query(rt: TestRuntime) -> anyhow::Result<()> {
23552355
assert!(token
23562356
.reads()
23572357
.overlaps(
2358-
&PackedDocument::pack(in_range_doc),
2358+
&PackedDocument::pack(&in_range_doc),
23592359
PersistenceVersion::default()
23602360
)
23612361
.is_some());

crates/database/src/transaction_index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ impl TransactionIndex {
513513
match new_document {
514514
Some(ref doc) => {
515515
assert_eq!(doc.id(), *doc_id);
516-
Some(doc.clone())
516+
Some(doc)
517517
},
518518
None => panic!("Unexpected index update: {:?}", update.value),
519519
}
@@ -647,7 +647,7 @@ impl TransactionIndexMap {
647647
.map(|(k, v)| (IndexKeyBytes(k.clone()), v.as_ref().map(|v| v.unpack())))
648648
}
649649

650-
pub fn insert(&mut self, k: IndexKey, v: Option<ResolvedDocument>) {
650+
pub fn insert(&mut self, k: IndexKey, v: Option<&ResolvedDocument>) {
651651
self.inner
652652
.insert(k.into_bytes().0, v.map(PackedDocument::pack));
653653
}

crates/database/src/write_log.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{
1111
use common::{
1212
document::{
1313
DocumentUpdate,
14+
DocumentUpdateRef,
1415
PackedDocument,
1516
},
1617
knobs::{
@@ -61,11 +62,11 @@ impl HeapSize for PackedDocumentUpdate {
6162
type OrderedWrites = WithHeapSize<Vec<(ResolvedDocumentId, PackedDocumentUpdate)>>;
6263

6364
impl PackedDocumentUpdate {
64-
pub fn pack(update: DocumentUpdate) -> Self {
65+
pub fn pack(update: &impl DocumentUpdateRef) -> Self {
6566
Self {
66-
id: update.id,
67-
old_document: update.old_document.map(PackedDocument::pack),
68-
new_document: update.new_document.map(PackedDocument::pack),
67+
id: update.id(),
68+
old_document: update.old_document().map(PackedDocument::pack),
69+
new_document: update.new_document().map(PackedDocument::pack),
6970
}
7071
}
7172

@@ -635,7 +636,7 @@ mod tests {
635636
Timestamp::must(1003),
636637
vec![(
637638
id,
638-
PackedDocumentUpdate::pack(DocumentUpdate {
639+
PackedDocumentUpdate::pack(&DocumentUpdate {
639640
id,
640641
old_document: None,
641642
new_document: Some(doc.clone()),
@@ -749,7 +750,7 @@ mod tests {
749750
Timestamp::must(1003),
750751
vec![(
751752
id,
752-
PackedDocumentUpdate::pack(DocumentUpdate {
753+
PackedDocumentUpdate::pack(&DocumentUpdate {
753754
id,
754755
old_document: Some(doc),
755756
new_document: None,

crates/database/src/writes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ mod tests {
484484
user_table1.tablet_id,
485485
)?;
486486

487-
let user_table1_table_metadata_change = PackedDocument::pack(ResolvedDocument::new(
487+
let user_table1_table_metadata_change = PackedDocument::pack(&ResolvedDocument::new(
488488
bootstrap_tables.table_resolved_doc_id(user_table1.tablet_id),
489489
CreationTime::ONE,
490490
TableMetadata::new(
@@ -502,7 +502,7 @@ mod tests {
502502
)
503503
.is_some());
504504

505-
let user_table1_index_change = PackedDocument::pack(ResolvedDocument::new(
505+
let user_table1_index_change = PackedDocument::pack(&ResolvedDocument::new(
506506
id_generator.system_generate(&INDEX_TABLE),
507507
CreationTime::ONE,
508508
IndexMetadata::new_backfilling(
@@ -519,7 +519,7 @@ mod tests {
519519

520520
// Writes to a table should *not* OCC with modification of the table metadata
521521
// or an index of unrelated same table.
522-
let user_table2_table_metadata_change = PackedDocument::pack(ResolvedDocument::new(
522+
let user_table2_table_metadata_change = PackedDocument::pack(&ResolvedDocument::new(
523523
bootstrap_tables.table_resolved_doc_id(user_table2.tablet_id),
524524
CreationTime::ONE,
525525
TableMetadata::new(
@@ -537,7 +537,7 @@ mod tests {
537537
)
538538
.is_none());
539539

540-
let user_table2_index_change = PackedDocument::pack(ResolvedDocument::new(
540+
let user_table2_index_change = PackedDocument::pack(&ResolvedDocument::new(
541541
id_generator.system_generate(&INDEX_TABLE),
542542
CreationTime::ONE,
543543
IndexMetadata::new_backfilling(

crates/function_runner/src/in_memory_indexes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ async fn load_index(
173173
Order::Asc,
174174
usize::MAX,
175175
)
176-
.map_ok(|(key, rev)| (key.0, (rev.ts, PackedDocument::pack(rev.value))))
176+
.map_ok(|(key, rev)| (key.0, (rev.ts, PackedDocument::pack(&rev.value))))
177177
.try_collect()
178178
.await?;
179179
log_funrun_index_load_rows(index_map.len() as u64, &table_name, &instance_name);

0 commit comments

Comments
 (0)