Repository navigation
Direct serializer for "Serde Way" - #139
Conversation
|
Thanks for the PR! |
e2b0d22 to
422fbaa
Compare
|
Oops, I hadn't realized my branch had fallen behind that much. Merge conflicts should be resolved now. |
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
| const COLLECTION_SERIALIZER_DEFAULT_INIT_ITEM_CAPACITY: usize = 32; | ||
| const SINGLE_VALUE_INIT_BUFFER_SIZE: usize = 128; | ||
|
|
||
| pub struct DirectSerializeSeq<'a, 's, W: Write> { |
There was a problem hiding this comment.
Rustdoc please for all public items!
With as much as possible details because even the current ser.rs and de.rs are hard to maintain.
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Is that recursive schema a legal one, (or at least able to be non-infinite)? For the recursive |
|
You might be right! |
Will do 👍 |
|
Ok, recursive schema and big decimal "just works" test cases should be updated and passing now. Thanks for adding those test cases! |
|
Oops, forgot to push. Now recursive schema and big decimal updates should be there. |
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
…pache#150) This crate is used to generate unit tests and there is no known issue using it. Signed-off-by: Martin Tzvetanov Grigorov <[email protected]> (cherry picked from commit a9c10d5)
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Since there is no indexed access the LinkedList should be better than Vector Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
|
@jdarais I don't have more ideas for improvements. |
| names, | ||
| enclosing_namespace, | ||
| schema_stack: Vec::new(), | ||
| schema_stack: Default::default(), |
There was a problem hiding this comment.
You may want to run the benchmark tests to validate whether a linked list is faster in this case. When running on my own machine, Vec is faster. A vector may need to occasionally reallocate if its capacity is reached, but when it's used as a stack, inserts and deletes only happen at the end, which happens in constant time, (except for the scenario where capacity is exceeded and a larger space must be allocated, but those should be infrequent,) and you get the benefit of cache locality since all the elements are next to each other in memory.
There was a problem hiding this comment.
For me it shows small improvements with LinkedList:
cargo bench serde
Compiling apache-avro v0.18.0 (/home/martin/git/apache/avro-rs/avro)
Compiling apache-avro-derive v0.18.0 (/home/martin/git/apache/avro-rs/avro_derive)
Compiling hello-wasm v0.1.0 (/home/martin/git/apache/avro-rs/wasm-demo)
Finished `bench` profile [optimized] target(s) in 12.52s
Running benches/serde.rs (target/release/deps/serde-cc1227daf82840e0)
small schema, write 1 record (serde way)
time: [2.3658 µs 2.4030 µs 2.4490 µs]
change: [-4.4973% -2.9172% -1.2806%] (p = 0.00 < 0.05)
Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
6 (6.00%) high mild
4 (4.00%) high severe
small schema, write 100 records (serde way)
time: [10.437 µs 10.519 µs 10.620 µs]
change: [-5.0968% -2.9766% -0.9836%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 15 outliers among 100 measurements (15.00%)
6 (6.00%) high mild
9 (9.00%) high severe
big schema, write 1 record (serde way)
time: [4.6004 µs 4.6281 µs 4.6591 µs]
change: [-10.071% -7.9251% -5.8801%] (p = 0.00 < 0.05)
Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
5 (5.00%) high mild
5 (5.00%) high severe
big schema, write 100 records (serde way)
time: [41.988 µs 42.455 µs 43.000 µs]
change: [-1.8518% -1.0529% -0.1373%] (p = 0.02 < 0.05)
Change within noise threshold.
Found 13 outliers among 100 measurements (13.00%)
4 (4.00%) high mild
9 (9.00%) high severe
small schema, write 10k records (serde way)
time: [822.03 µs 826.87 µs 832.20 µs]
change: [+0.4821% +1.4965% +2.4936%] (p = 0.01 < 0.05)
Change within noise threshold.
big scheam, write 10k records (serde way)
time: [3.6829 ms 3.7066 ms 3.7332 ms]
change: [-4.5266% -3.5488% -2.6061%] (p = 0.00 < 0.05)
Performance has improved.
Going back to Vec:
cargo bench serde
Compiling apache-avro v0.18.0 (/home/martin/git/apache/avro-rs/avro)
Compiling apache-avro-derive v0.18.0 (/home/martin/git/apache/avro-rs/avro_derive)
Compiling hello-wasm v0.1.0 (/home/martin/git/apache/avro-rs/wasm-demo)
Finished `bench` profile [optimized] target(s) in 15.77s
Running benches/serde.rs (target/release/deps/serde-cc1227daf82840e0)
small schema, write 1 record (serde way)
time: [2.3788 µs 2.3980 µs 2.4242 µs]
change: [+5.6754% +7.4976% +9.3008%] (p = 0.00 < 0.05)
Performance has regressed.
Found 9 outliers among 100 measurements (9.00%)
3 (3.00%) high mild
6 (6.00%) high severe
small schema, write 100 records (serde way)
time: [10.412 µs 10.479 µs 10.554 µs]
change: [-0.5517% +0.4947% +1.6596%] (p = 0.41 > 0.05)
No change in performance detected.
Found 12 outliers among 100 measurements (12.00%)
5 (5.00%) high mild
7 (7.00%) high severe
big schema, write 1 record (serde way)
time: [4.6428 µs 4.6718 µs 4.7082 µs]
change: [+3.5714% +5.4697% +7.5133%] (p = 0.00 < 0.05)
Performance has regressed.
Found 13 outliers among 100 measurements (13.00%)
4 (4.00%) high mild
9 (9.00%) high severe
big schema, write 100 records (serde way)
time: [41.769 µs 41.981 µs 42.247 µs]
change: [+0.9885% +2.1415% +3.5064%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 12 outliers among 100 measurements (12.00%)
5 (5.00%) high mild
7 (7.00%) high severe
small schema, write 10k records (serde way)
time: [812.16 µs 818.92 µs 826.53 µs]
change: [-3.4624% -2.2517% -1.0241%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 20 measurements (10.00%)
1 (5.00%) high mild
1 (5.00%) high severe
big scheam, write 10k records (serde way)
time: [3.6963 ms 3.7240 ms 3.7561 ms]
change: [-3.6033% -2.6406% -1.6162%] (p = 0.00 < 0.05)
Performance has improved.
Found 1 outliers among 20 measurements (5.00%)
and again LinkedList:
cargo bench serde
Compiling apache-avro v0.18.0 (/home/martin/git/apache/avro-rs/avro)
Compiling apache-avro-derive v0.18.0 (/home/martin/git/apache/avro-rs/avro_derive)
Compiling hello-wasm v0.1.0 (/home/martin/git/apache/avro-rs/wasm-demo)
Finished `bench` profile [optimized] target(s) in 11.19s
Running benches/serde.rs (target/release/deps/serde-cc1227daf82840e0)
small schema, write 1 record (serde way)
time: [2.2771 µs 2.2932 µs 2.3141 µs]
change: [-8.0503% -6.2473% -4.6485%] (p = 0.00 < 0.05)
Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
4 (4.00%) high mild
5 (5.00%) high severe
small schema, write 100 records (serde way)
time: [10.690 µs 10.806 µs 10.940 µs]
change: [+0.1857% +1.6209% +2.9866%] (p = 0.02 < 0.05)
Change within noise threshold.
Found 9 outliers among 100 measurements (9.00%)
5 (5.00%) high mild
4 (4.00%) high severe
big schema, write 1 record (serde way)
time: [4.6742 µs 4.7222 µs 4.7790 µs]
change: [-3.0179% -0.6372% +1.7778%] (p = 0.62 > 0.05)
No change in performance detected.
Found 10 outliers among 100 measurements (10.00%)
3 (3.00%) high mild
7 (7.00%) high severe
big schema, write 100 records (serde way)
time: [41.741 µs 42.020 µs 42.347 µs]
change: [-2.2702% -0.7627% +0.7781%] (p = 0.33 > 0.05)
No change in performance detected.
Found 6 outliers among 100 measurements (6.00%)
2 (2.00%) high mild
4 (4.00%) high severe
small schema, write 10k records (serde way)
time: [826.20 µs 830.19 µs 835.08 µs]
change: [+0.2122% +1.3489% +2.3329%] (p = 0.02 < 0.05)
Change within noise threshold.
big scheam, write 10k records (serde way)
time: [3.7003 ms 3.7235 ms 3.7506 ms]
change: [-0.8469% +0.2209% +1.1796%] (p = 0.68 > 0.05)
No change in performance detected.
There was a problem hiding this comment.
Ah, interesting. It may be platform dependent, since I initially ran the bench tests on Windows, but after running the tests on my Linux machine, I get results consistent with yours.
I was a bit surprised to see that result, since the conventional wisdom is that linked list is almost always worse than vector, since it ends up being more spread out in memory, incurring a performance penalty simply from dealing with CPU cache misses. Funny enough, looking again at the tests and the DirectSerializer implementation, I don't think anything is ever stored in the schema_stack field during the bench tests, since it is only used for unions and type references, which aren't used in the bench test data. The linked list is probably being measured as faster because it costs less to construct an empty linked list vs an empty vector.
I may do another pass to see if I can improve the use of the schema_stack field. If I can further improve the performance I'll submit another PR.
|
Just had a comment on the container type to store the |
|
I will merge it! |
|
I wonder whether SchemaAwareSerializer is a better name ?! The indirection thru types::Value was an implementation detail in the old serializer. IMO the real value of the new one is the schema validation! The speed improvements is very nice too! |
Yeah it did cross my mind that the |
|
Thanks for taking the PR! |
Deprecate the old Serializer impl (apache_avro::ser::Serializer) Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
* Issue #139: Rename DirectSerializer to SchemaAwareWriteSerializer Deprecate the old Serializer impl (apache_avro::ser::Serializer) Signed-off-by: Martin Tzvetanov Grigorov <[email protected]> * Undeprecate de::Serializer Reasons: - some people may depend on serializing to types::Value - de::Deserializer works only with types::Value Signed-off-by: Martin Tzvetanov Grigorov <[email protected]> --------- Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
#163) * add nullable values to big schema in bench tests * remove schema_stack in SchemaAwareWriteSerializer * Rename support structs to match SchemaAwareWriteSerializer
|
@jdarais do you remember why you changed If I read the spec correctly (and look at the implementation of |
|
I do not recall the reason I added that flag and made SpecificSingleObjectWriter only write the header once. It's possible I thought this was the original behavior of GenericSingleObjectWriter, and was trying to replicate that behavior in the new SpecificSingleObjectWriter functions that directly used SchemaAwareWriteSerializer. I'm seeing now that this is not the behavior of GenericSingleObjectWriter, and SpecificSingleObjectWriter should behave the same as that one, so I think it makes sense remove the |
|
Thanks! Then we'll revert that specific change. I do have to say I'm very happy with the new serializer, so thanks for implementing it! |
Hi, I started playing with avro-rs to build some Rust-based Avro tools, and noticed some things I could take a stab at improving in the avro-rs library itself. This PR has a lot of changes in it, but it mostly centers around a new
DirectSerializer, who's behavior is described below. Feel free to consider this PR as a whole, or take bits and pieces from it. There are some things in it that could be useful even independently of the addition ofDirectSerializer:This PR proposes a direct serialization of objects when serializing the "Serde Way", which provides roughly a 5x performance improvement, and fixes issues caused by lost schema information when first converting to a
Valuebefore serializing to the Write stream (#70).Some notes on these changes:
DirectSerializer, provides direct serialization of a type using the "Serde Way". TheDirectSerializeris initialized with the schema to be used for writing data, and uses that schema as a guide to know how to serialize the serde types that it encounters.Valuetypes, which is only part of the process in the "Serde Way", so some additional benchmark tests were added to measure the end-to-end performance of "Serde Way" serialization#[serde(untagged)]attribute is added, which serializes an enum value as the variant type itself rather than a union type, and it's up to the (de)serializer to detect that the schema calls for a union, search the union schemas for a match, and use the first schema found that matches the data. This PR adds aUnionDeserializertode.rs, which, in combination withDirectSerializer, allows "Avro union / Rust enum" (de)serialization without requiring the#[serde(untagged)]attribute.Valueas an intermediate step, but one thing I was not able to replicate is serialization of enums that do have the#[serde(untagged)]attribute. The closest I got was making theDirectSerializerable to search a union schema for the first schema type that matches the input data, but in the case of "record" types, there's no easy way to ensure that the field names are also a match. This feature of schema matching on write does seem a bit odd to me, since the writer should know the structure of the data being written, and schema matching should only really be a concern for the reader, where schema resolution is happening between a reader and writer schema. But, worth noting that this difference in behavior is not backwards-compatible.All unit tests are passing. You can take a look at the changes in the unit tests to see where there are differences in behavior.