Skip to content

Commit cd8d5c4

Browse files
authored
refactor(c++/rust): refine c++ serialize_to API (#3045)
## Why? ## What does this PR do? ## Related issues ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent 5fb33ce commit cd8d5c4

File tree

13 files changed

+151
-151
lines changed

13 files changed

+151
-151
lines changed

benchmarks/cpp_benchmark/benchmark.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ static void BM_Fory_Struct_Serialize(benchmark::State &state) {
441441

442442
for (auto _ : state) {
443443
buffer.WriterIndex(0);
444-
fory.serialize_to(obj, buffer);
444+
fory.serialize_to(buffer, obj);
445445
benchmark::DoNotOptimize(buffer.data());
446446
}
447447
}
@@ -528,7 +528,7 @@ static void BM_Fory_Sample_Serialize(benchmark::State &state) {
528528

529529
for (auto _ : state) {
530530
buffer.WriterIndex(0);
531-
auto result = fory.serialize_to(obj, buffer);
531+
auto result = fory.serialize_to(buffer, obj);
532532
benchmark::DoNotOptimize(result);
533533
benchmark::DoNotOptimize(buffer.data());
534534
}
@@ -608,7 +608,7 @@ static void BM_Fory_MediaContent_Serialize(benchmark::State &state) {
608608

609609
for (auto _ : state) {
610610
buffer.WriterIndex(0);
611-
auto result = fory.serialize_to(obj, buffer);
611+
auto result = fory.serialize_to(buffer, obj);
612612
benchmark::DoNotOptimize(result);
613613
benchmark::DoNotOptimize(buffer.data());
614614
}

cpp/fory/serialization/fory.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,12 @@ class Fory : public BaseFory {
422422
/// Serialize an object to an existing Buffer (fastest path).
423423
///
424424
/// @tparam T The type of object to serialize.
425-
/// @param obj The object to serialize.
426425
/// @param buffer The buffer to write to.
426+
/// @param obj The object to serialize.
427427
/// @return Number of bytes written, or error.
428428
template <typename T>
429-
FORY_ALWAYS_INLINE Result<size_t, Error> serialize_to(const T &obj,
430-
Buffer &buffer) {
429+
FORY_ALWAYS_INLINE Result<size_t, Error> serialize_to(Buffer &buffer,
430+
const T &obj) {
431431
if (FORY_PREDICT_FALSE(!finalized_)) {
432432
ensure_finalized();
433433
}
@@ -441,18 +441,18 @@ class Fory : public BaseFory {
441441
/// fit the serialized data.
442442
///
443443
/// @tparam T The type of object to serialize.
444-
/// @param obj The object to serialize.
445444
/// @param output The vector to append to.
445+
/// @param obj The object to serialize.
446446
/// @return Number of bytes written, or error.
447447
template <typename T>
448-
Result<size_t, Error> serialize_to(const T &obj,
449-
std::vector<uint8_t> &output) {
448+
Result<size_t, Error> serialize_to(std::vector<uint8_t> &output,
449+
const T &obj) {
450450
// Wrap the output vector in a Buffer for zero-copy serialization
451451
// writer_index starts at output.size() for appending
452452
Buffer buffer(output);
453453

454454
// Forward to Buffer version
455-
auto result = serialize_to(obj, buffer);
455+
auto result = serialize_to(buffer, obj);
456456

457457
// Resize vector to actual written size
458458
output.resize(buffer.writer_index());
@@ -713,16 +713,16 @@ class ThreadSafeFory : public BaseFory {
713713
}
714714

715715
template <typename T>
716-
Result<size_t, Error> serialize_to(const T &obj, Buffer &buffer) {
716+
Result<size_t, Error> serialize_to(Buffer &buffer, const T &obj) {
717717
auto fory_handle = fory_pool_.acquire();
718-
return fory_handle->serialize_to(obj, buffer);
718+
return fory_handle->serialize_to(buffer, obj);
719719
}
720720

721721
template <typename T>
722-
Result<size_t, Error> serialize_to(const T &obj,
723-
std::vector<uint8_t> &output) {
722+
Result<size_t, Error> serialize_to(std::vector<uint8_t> &output,
723+
const T &obj) {
724724
auto fory_handle = fory_pool_.acquire();
725-
return fory_handle->serialize_to(obj, output);
725+
return fory_handle->serialize_to(output, obj);
726726
}
727727

728728
template <typename T>

docs/guide/cpp/basic-serialization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ if (result.ok()) {
114114
```cpp
115115
// Serialize to existing Buffer (fastest path)
116116
Buffer buffer;
117-
auto result = fory.serialize_to(obj, buffer);
117+
auto result = fory.serialize_to(buffer, obj);
118118
if (result.ok()) {
119119
size_t bytes_written = result.value();
120120
// buffer now contains serialized data
121121
}
122122

123123
// Serialize to existing vector (zero-copy)
124124
std::vector<uint8_t> output;
125-
auto result = fory.serialize_to(obj, output);
125+
auto result = fory.serialize_to(output, obj);
126126
if (result.ok()) {
127127
size_t bytes_written = result.value();
128128
// output now contains serialized data
@@ -225,7 +225,7 @@ fory.register_struct<Outer>(2);
225225

226226
## Performance Tips
227227

228-
- **Buffer Reuse**: Use `serialize_to(obj, buffer)` with pre-allocated buffers
228+
- **Buffer Reuse**: Use `serialize_to(buffer, obj)` with pre-allocated buffers
229229
- **Pre-registration**: Register all types before serialization starts
230230
- **Single-Threaded**: Use `build()` instead of `build_thread_safe()` when possible
231231
- **Disable Tracking**: Use `track_ref(false)` when references aren't needed

docs/guide/rust/basic-serialization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ let decoded: MyStruct = fory.deserialize(&bytes)?;
144144

145145
// Serialize to existing buffer
146146
let mut buf: Vec<u8> = vec![];
147-
fory.serialize_to(&obj, &mut buf)?;
147+
fory.serialize_to(&mut buf, &obj)?;
148148

149149
// Deserialize from reader
150150
let mut reader = Reader::new(&buf);

docs/guide/rust/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn main() -> Result<(), Error> {
8181

8282
// Serialize to specified buffer
8383
let mut buf: Vec<u8> = vec![];
84-
fory.serialize_to(&user, &mut buf)?;
84+
fory.serialize_to(&mut buf, &user)?;
8585
// Deserialize from specified buffer
8686
let mut reader = Reader::new(&buf);
8787
let decoded: User = fory.deserialize_from(&mut reader)?;

rust/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn main() -> Result<(), Error> {
6666

6767
// Serialize to specified buffer
6868
let mut buf: Vec<u8> = vec![];
69-
fory.serialize_to(&user, &mut buf)?;
69+
fory.serialize_to(&mut buf, &user)?;
7070
// Deserialize from specified buffer
7171
let mut reader = Reader::new(&buf);
7272
let decoded: User = fory.deserialize_from(&mut reader)?;

rust/fory-core/src/fory.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ impl Fory {
401401
///
402402
/// # Arguments
403403
///
404-
/// * `record` - A reference to the value to serialize.
405404
/// * `buf` - A mutable reference to the byte buffer to append the serialized data to.
406405
/// The buffer will be resized as needed during serialization.
406+
/// * `record` - A reference to the value to serialize.
407407
///
408408
/// # Returns
409409
///
@@ -431,7 +431,7 @@ impl Fory {
431431
/// let point = Point { x: 1, y: 2 };
432432
///
433433
/// let mut buf = Vec::new();
434-
/// let bytes_written = fory.serialize_to(&point, &mut buf).unwrap();
434+
/// let bytes_written = fory.serialize_to(&mut buf, &point).unwrap();
435435
/// assert_eq!(bytes_written, buf.len());
436436
/// ```
437437
///
@@ -454,11 +454,11 @@ impl Fory {
454454
/// let mut buf = Vec::new();
455455
///
456456
/// // First serialization
457-
/// let len1 = fory.serialize_to(&p1, &mut buf).unwrap();
457+
/// let len1 = fory.serialize_to(&mut buf, &p1).unwrap();
458458
/// let offset1 = buf.len();
459459
///
460460
/// // Second serialization - appends to existing data
461-
/// let len2 = fory.serialize_to(&p2, &mut buf).unwrap();
461+
/// let len2 = fory.serialize_to(&mut buf, &p2).unwrap();
462462
/// let offset2 = buf.len();
463463
///
464464
/// assert_eq!(offset1, len1);
@@ -495,20 +495,20 @@ impl Fory {
495495
/// buf.resize(16, 0); // Set length to 16 to append the write, capacity stays 1024
496496
///
497497
/// let initial_capacity = buf.capacity();
498-
/// fory.serialize_to(&point, &mut buf).unwrap();
498+
/// fory.serialize_to(&mut buf, &point).unwrap();
499499
///
500500
/// // Reset to smaller size to append the write - capacity unchanged
501501
/// buf.resize(16, 0);
502502
/// assert_eq!(buf.capacity(), initial_capacity); // Capacity not shrunk
503503
///
504504
/// // Reuse buffer efficiently without reallocation
505-
/// fory.serialize_to(&point, &mut buf).unwrap();
505+
/// fory.serialize_to(&mut buf, &point).unwrap();
506506
/// assert_eq!(buf.capacity(), initial_capacity); // Still no reallocation
507507
/// ```
508508
pub fn serialize_to<T: Serializer>(
509509
&self,
510-
record: &T,
511510
buf: &mut Vec<u8>,
511+
record: &T,
512512
) -> Result<usize, Error> {
513513
let start = buf.len();
514514
self.with_write_context(|context| {

rust/fory/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
//!
8484
//! // Serialize to specified buffer and deserialize from it
8585
//! let mut buf: Vec<u8> = vec![];
86-
//! fory.serialize_to(&user, &mut buf)?;
86+
//! fory.serialize_to(&mut buf, &user)?;
8787
//! let mut reader = Reader::new(&buf);
8888
//! let decoded: User = fory.deserialize_from(&mut reader)?;
8989
//! assert_eq!(user, decoded);

rust/tests/tests/compatible/test_basic_type.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,82 +44,82 @@ const FLOAT64_ARRAY: [f64; 1] = [53.0];
4444

4545
fn serialize_non_null(fory: &Fory) -> Vec<u8> {
4646
let mut buf = Vec::new();
47-
fory.serialize_to(&BOOL_VAL, &mut buf).unwrap();
48-
fory.serialize_to(&I8_VAL, &mut buf).unwrap();
49-
fory.serialize_to(&I16_VAL, &mut buf).unwrap();
50-
fory.serialize_to(&I32_VAL, &mut buf).unwrap();
51-
fory.serialize_to(&I64_VAL, &mut buf).unwrap();
52-
fory.serialize_to(&F32_VAL, &mut buf).unwrap();
53-
fory.serialize_to(&F64_VAL, &mut buf).unwrap();
54-
fory.serialize_to(&STR_LATIN1_VAL.to_string(), &mut buf)
47+
fory.serialize_to(&mut buf, &BOOL_VAL).unwrap();
48+
fory.serialize_to(&mut buf, &I8_VAL).unwrap();
49+
fory.serialize_to(&mut buf, &I16_VAL).unwrap();
50+
fory.serialize_to(&mut buf, &I32_VAL).unwrap();
51+
fory.serialize_to(&mut buf, &I64_VAL).unwrap();
52+
fory.serialize_to(&mut buf, &F32_VAL).unwrap();
53+
fory.serialize_to(&mut buf, &F64_VAL).unwrap();
54+
fory.serialize_to(&mut buf, &STR_LATIN1_VAL.to_string())
5555
.unwrap();
56-
fory.serialize_to(&LOCAL_DATE_VAL, &mut buf).unwrap();
57-
fory.serialize_to(&TIMESTAMP_VAL, &mut buf).unwrap();
58-
fory.serialize_to(&BOOL_ARRAY.to_vec(), &mut buf).unwrap();
59-
fory.serialize_to(&INT8_ARRAY.to_vec(), &mut buf).unwrap();
60-
fory.serialize_to(&INT16_ARRAY.to_vec(), &mut buf).unwrap();
61-
fory.serialize_to(&INT32_ARRAY.to_vec(), &mut buf).unwrap();
62-
fory.serialize_to(&INT64_ARRAY.to_vec(), &mut buf).unwrap();
63-
fory.serialize_to(&FLOAT32_ARRAY.to_vec(), &mut buf)
56+
fory.serialize_to(&mut buf, &LOCAL_DATE_VAL).unwrap();
57+
fory.serialize_to(&mut buf, &TIMESTAMP_VAL).unwrap();
58+
fory.serialize_to(&mut buf, &BOOL_ARRAY.to_vec()).unwrap();
59+
fory.serialize_to(&mut buf, &INT8_ARRAY.to_vec()).unwrap();
60+
fory.serialize_to(&mut buf, &INT16_ARRAY.to_vec()).unwrap();
61+
fory.serialize_to(&mut buf, &INT32_ARRAY.to_vec()).unwrap();
62+
fory.serialize_to(&mut buf, &INT64_ARRAY.to_vec()).unwrap();
63+
fory.serialize_to(&mut buf, &FLOAT32_ARRAY.to_vec())
6464
.unwrap();
65-
fory.serialize_to(&FLOAT64_ARRAY.to_vec(), &mut buf)
65+
fory.serialize_to(&mut buf, &FLOAT64_ARRAY.to_vec())
6666
.unwrap();
6767
buf
6868
}
6969

7070
fn serialize_nullable(fory: &Fory) -> Vec<u8> {
7171
let mut buf = Vec::new();
72-
fory.serialize_to(&Some(BOOL_VAL), &mut buf).unwrap();
73-
fory.serialize_to(&Some(I8_VAL), &mut buf).unwrap();
74-
fory.serialize_to(&Some(I16_VAL), &mut buf).unwrap();
75-
fory.serialize_to(&Some(I32_VAL), &mut buf).unwrap();
76-
fory.serialize_to(&Some(I64_VAL), &mut buf).unwrap();
77-
fory.serialize_to(&Some(F32_VAL), &mut buf).unwrap();
78-
fory.serialize_to(&Some(F64_VAL), &mut buf).unwrap();
79-
fory.serialize_to(&Some(STR_LATIN1_VAL.to_string()), &mut buf)
72+
fory.serialize_to(&mut buf, &Some(BOOL_VAL)).unwrap();
73+
fory.serialize_to(&mut buf, &Some(I8_VAL)).unwrap();
74+
fory.serialize_to(&mut buf, &Some(I16_VAL)).unwrap();
75+
fory.serialize_to(&mut buf, &Some(I32_VAL)).unwrap();
76+
fory.serialize_to(&mut buf, &Some(I64_VAL)).unwrap();
77+
fory.serialize_to(&mut buf, &Some(F32_VAL)).unwrap();
78+
fory.serialize_to(&mut buf, &Some(F64_VAL)).unwrap();
79+
fory.serialize_to(&mut buf, &Some(STR_LATIN1_VAL.to_string()))
8080
.unwrap();
81-
fory.serialize_to(&Some(LOCAL_DATE_VAL), &mut buf).unwrap();
82-
fory.serialize_to(&Some(TIMESTAMP_VAL), &mut buf).unwrap();
83-
fory.serialize_to(&Some(BOOL_ARRAY.to_vec()), &mut buf)
81+
fory.serialize_to(&mut buf, &Some(LOCAL_DATE_VAL)).unwrap();
82+
fory.serialize_to(&mut buf, &Some(TIMESTAMP_VAL)).unwrap();
83+
fory.serialize_to(&mut buf, &Some(BOOL_ARRAY.to_vec()))
8484
.unwrap();
85-
fory.serialize_to(&Some(INT8_ARRAY.to_vec()), &mut buf)
85+
fory.serialize_to(&mut buf, &Some(INT8_ARRAY.to_vec()))
8686
.unwrap();
87-
fory.serialize_to(&Some(INT16_ARRAY.to_vec()), &mut buf)
87+
fory.serialize_to(&mut buf, &Some(INT16_ARRAY.to_vec()))
8888
.unwrap();
89-
fory.serialize_to(&Some(INT32_ARRAY.to_vec()), &mut buf)
89+
fory.serialize_to(&mut buf, &Some(INT32_ARRAY.to_vec()))
9090
.unwrap();
91-
fory.serialize_to(&Some(INT64_ARRAY.to_vec()), &mut buf)
91+
fory.serialize_to(&mut buf, &Some(INT64_ARRAY.to_vec()))
9292
.unwrap();
93-
fory.serialize_to(&Some(FLOAT32_ARRAY.to_vec()), &mut buf)
93+
fory.serialize_to(&mut buf, &Some(FLOAT32_ARRAY.to_vec()))
9494
.unwrap();
95-
fory.serialize_to(&Some(FLOAT64_ARRAY.to_vec()), &mut buf)
95+
fory.serialize_to(&mut buf, &Some(FLOAT64_ARRAY.to_vec()))
9696
.unwrap();
97-
fory.serialize_to(&Option::<bool>::None, &mut buf).unwrap();
98-
fory.serialize_to(&Option::<i8>::None, &mut buf).unwrap();
99-
fory.serialize_to(&Option::<i16>::None, &mut buf).unwrap();
100-
fory.serialize_to(&Option::<i32>::None, &mut buf).unwrap();
101-
fory.serialize_to(&Option::<i64>::None, &mut buf).unwrap();
102-
fory.serialize_to(&Option::<f32>::None, &mut buf).unwrap();
103-
fory.serialize_to(&Option::<f64>::None, &mut buf).unwrap();
104-
fory.serialize_to(&Option::<String>::None, &mut buf)
97+
fory.serialize_to(&mut buf, &Option::<bool>::None).unwrap();
98+
fory.serialize_to(&mut buf, &Option::<i8>::None).unwrap();
99+
fory.serialize_to(&mut buf, &Option::<i16>::None).unwrap();
100+
fory.serialize_to(&mut buf, &Option::<i32>::None).unwrap();
101+
fory.serialize_to(&mut buf, &Option::<i64>::None).unwrap();
102+
fory.serialize_to(&mut buf, &Option::<f32>::None).unwrap();
103+
fory.serialize_to(&mut buf, &Option::<f64>::None).unwrap();
104+
fory.serialize_to(&mut buf, &Option::<String>::None)
105105
.unwrap();
106-
fory.serialize_to(&Option::<NaiveDate>::None, &mut buf)
106+
fory.serialize_to(&mut buf, &Option::<NaiveDate>::None)
107107
.unwrap();
108-
fory.serialize_to(&Option::<NaiveDateTime>::None, &mut buf)
108+
fory.serialize_to(&mut buf, &Option::<NaiveDateTime>::None)
109109
.unwrap();
110-
fory.serialize_to(&Option::<Vec<bool>>::None, &mut buf)
110+
fory.serialize_to(&mut buf, &Option::<Vec<bool>>::None)
111111
.unwrap();
112-
fory.serialize_to(&Option::<Vec<i8>>::None, &mut buf)
112+
fory.serialize_to(&mut buf, &Option::<Vec<i8>>::None)
113113
.unwrap();
114-
fory.serialize_to(&Option::<Vec<i16>>::None, &mut buf)
114+
fory.serialize_to(&mut buf, &Option::<Vec<i16>>::None)
115115
.unwrap();
116-
fory.serialize_to(&Option::<Vec<i32>>::None, &mut buf)
116+
fory.serialize_to(&mut buf, &Option::<Vec<i32>>::None)
117117
.unwrap();
118-
fory.serialize_to(&Option::<Vec<i64>>::None, &mut buf)
118+
fory.serialize_to(&mut buf, &Option::<Vec<i64>>::None)
119119
.unwrap();
120-
fory.serialize_to(&Option::<Vec<f32>>::None, &mut buf)
120+
fory.serialize_to(&mut buf, &Option::<Vec<f32>>::None)
121121
.unwrap();
122-
fory.serialize_to(&Option::<Vec<f64>>::None, &mut buf)
122+
fory.serialize_to(&mut buf, &Option::<Vec<f64>>::None)
123123
.unwrap();
124124
buf
125125
}

rust/tests/tests/compatible/test_struct_enum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ fn simple_write_continuous() {
9393
last: 44,
9494
};
9595
let mut buffer = vec![];
96-
fory.serialize_to(&animal, &mut buffer).unwrap();
97-
fory.serialize_to(&animal, &mut buffer).unwrap();
98-
fory.serialize_to(&animal, &mut buffer).unwrap();
96+
fory.serialize_to(&mut buffer, &animal).unwrap();
97+
fory.serialize_to(&mut buffer, &animal).unwrap();
98+
fory.serialize_to(&mut buffer, &animal).unwrap();
9999

100100
let reader = &mut Reader::new(buffer.as_slice());
101101
let animal1: Animal1 = fory.deserialize_from(reader).unwrap();

0 commit comments

Comments
 (0)