From 6b98d372abe4fd01f26f7b48045abae59d9c7519 Mon Sep 17 00:00:00 2001 From: Pure White Date: Fri, 5 May 2023 11:47:13 +0800 Subject: [PATCH] feat: optimize faststr usage (#151) --- Cargo.lock | 364 ++++++++++++++++++++++++----------- Cargo.toml | 10 + pilota-build/src/test/mod.rs | 6 +- pilota/Cargo.toml | 15 +- pilota/benches/faststr.rs | 170 ++++++++++++++++ pilota/src/thrift/binary.rs | 6 +- pilota/src/thrift/compact.rs | 6 +- pilota/src/thrift/mod.rs | 13 +- 8 files changed, 461 insertions(+), 129 deletions(-) create mode 100644 pilota/benches/faststr.rs diff --git a/Cargo.lock b/Cargo.lock index 2c112fe4..ef578a38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,30 +10,30 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anyhow" -version = "1.0.69" +version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" +checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" [[package]] name = "async-recursion" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b015a331cc64ebd1774ba119538573603427eaace0a1950c423ab971f903796" +checksum = "0e97ce7de6cf12de5d7226c73f5ba9811622f4db3a5b91b55c53e987e5f91cba" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.15", ] [[package]] name = "async-trait" -version = "0.1.64" +version = "0.1.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd7fce9ba8c3c042128ce72d8b2ddbf3a05747efb67ea0313c635e10bda47a2" +checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.15", ] [[package]] @@ -76,9 +76,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bumpalo" -version = "3.12.0" +version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" [[package]] name = "byteorder" @@ -101,6 +101,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + [[package]] name = "cfg-if" version = "1.0.0" @@ -136,9 +142,9 @@ dependencies = [ [[package]] name = "clap" -version = "3.2.23" +version = "3.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "bitflags", "clap_lex", @@ -227,9 +233,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.14" +version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" +checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" dependencies = [ "cfg-if", ] @@ -242,7 +248,7 @@ checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -260,6 +266,27 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "errno" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -302,9 +329,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ "cfg-if", "libc", @@ -356,11 +383,17 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" + [[package]] name = "indexmap" -version = "1.9.2" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown", @@ -385,6 +418,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "io-lifetimes" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" +dependencies = [ + "hermit-abi 0.3.1", + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "itertools" version = "0.10.5" @@ -417,9 +461,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.139" +version = "0.2.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "libm" @@ -438,6 +482,12 @@ dependencies = [ "tokio", ] +[[package]] +name = "linux-raw-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f" + [[package]] name = "lock_api" version = "0.4.9" @@ -499,11 +549,11 @@ dependencies = [ [[package]] name = "normpath" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "972dec05f98e7c787ede35d7a9ea4735eb7788c299287352757b3def6cc1f7b5" +checksum = "ec60c60a693226186f5d6edf073232bfb6464ed97eb22cf3b01c1e8198fd97f5" dependencies = [ - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -580,16 +630,16 @@ dependencies = [ "cfg-if", "instant", "libc", - "redox_syscall", + "redox_syscall 0.2.16", "smallvec", "winapi", ] [[package]] name = "paste" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" +checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" [[package]] name = "petgraph" @@ -631,7 +681,7 @@ dependencies = [ "phf_shared", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -645,12 +695,13 @@ dependencies = [ [[package]] name = "pilota" -version = "0.6.2" +version = "0.6.3" dependencies = [ "anyhow", "async-recursion", "async-trait", "bytes", + "criterion", "derivative", "faststr", "integer-encoding", @@ -658,6 +709,7 @@ dependencies = [ "linkedbytes", "paste", "proptest", + "rand", "serde", "thiserror", "tokio", @@ -689,7 +741,7 @@ dependencies = [ "quote", "salsa", "scoped-tls", - "syn", + "syn 1.0.109", "tempfile", "tokio", "tracing", @@ -745,9 +797,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.51" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] @@ -767,7 +819,7 @@ dependencies = [ "rand", "rand_chacha", "rand_xorshift", - "regex-syntax", + "regex-syntax 0.6.29", "rusty-fork", "tempfile", "unarray", @@ -823,9 +875,9 @@ checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" [[package]] name = "quote" -version = "1.0.23" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -900,13 +952,22 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" -version = "1.7.1" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" dependencies = [ - "regex-syntax", + "regex-syntax 0.7.1", ] [[package]] @@ -915,23 +976,20 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" dependencies = [ - "regex-syntax", + "regex-syntax 0.6.29", ] [[package]] name = "regex-syntax" -version = "0.6.28" +version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] -name = "remove_dir_all" -version = "0.5.3" +name = "regex-syntax" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] +checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" [[package]] name = "rustc-hash" @@ -939,6 +997,20 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustix" +version = "0.37.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys 0.48.0", +] + [[package]] name = "rusty-fork" version = "0.3.0" @@ -983,7 +1055,7 @@ dependencies = [ "heck 0.3.3", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -1009,29 +1081,29 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.152" +version = "1.0.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +checksum = "71b2f6e1ab5c2b98c05f0f35b236b22e8df7ead6ffbf51d7808da7f8817e7ab6" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.152" +version = "1.0.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +checksum = "a2a0814352fd64b58489904a44ea8d90cb1a91dcb6b4f5ebabc32c8318e93cb6" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.15", ] [[package]] name = "serde_json" -version = "1.0.95" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" dependencies = [ "itoa", "ryu", @@ -1061,9 +1133,20 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "syn" -version = "1.0.107" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" dependencies = [ "proc-macro2", "quote", @@ -1072,16 +1155,15 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.3.0" +version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" +checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" dependencies = [ "cfg-if", "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi", + "redox_syscall 0.3.5", + "rustix", + "windows-sys 0.45.0", ] [[package]] @@ -1092,22 +1174,22 @@ checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" [[package]] name = "thiserror" -version = "1.0.38" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.38" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.15", ] [[package]] @@ -1132,15 +1214,14 @@ dependencies = [ [[package]] name = "tokio" -version = "1.25.0" +version = "1.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af" +checksum = "c3c786bf8134e5a3a166db9b29ab8f48134739014a3eca7bc6bfa95d673b136f" dependencies = [ "autocfg", "bytes", - "memchr", "pin-project-lite", - "windows-sys 0.42.0", + "windows-sys 0.48.0", ] [[package]] @@ -1157,13 +1238,13 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" +checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.15", ] [[package]] @@ -1189,9 +1270,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" dependencies = [ "matchers", "nu-ansi-term", @@ -1213,9 +1294,9 @@ checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" [[package]] name = "unicode-ident" -version = "1.0.6" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" [[package]] name = "unicode-segmentation" @@ -1275,7 +1356,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 1.0.109", "wasm-bindgen-shared", ] @@ -1297,7 +1378,7 @@ checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.109", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1362,81 +1443,132 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.42.0" +version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows-targets 0.42.2", ] [[package]] name = "windows-sys" -version = "0.45.0" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.0", ] [[package]] name = "windows-targets" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] +[[package]] +name = "windows-targets" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" +dependencies = [ + "windows_aarch64_gnullvm 0.48.0", + "windows_aarch64_msvc 0.48.0", + "windows_i686_gnu 0.48.0", + "windows_i686_msvc 0.48.0", + "windows_x86_64_gnu 0.48.0", + "windows_x86_64_gnullvm 0.48.0", + "windows_x86_64_msvc 0.48.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" -version = "0.42.1" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" [[package]] name = "windows_aarch64_msvc" -version = "0.42.1" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] name = "windows_i686_gnu" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] name = "windows_i686_msvc" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] name = "windows_x86_64_gnu" -version = "0.42.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] name = "windows_x86_64_gnullvm" -version = "0.42.1" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" [[package]] name = "windows_x86_64_msvc" -version = "0.42.1" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" diff --git a/Cargo.toml b/Cargo.toml index eff9160b..741701d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,13 @@ [workspace] members = ["pilota", "pilota-build", "pilota-thrift-parser"] resolver = "2" + +[profile.bench] +opt-level = 3 +debug = true +debug-assertions = false +overflow-checks = false +lto = true +incremental = false +codegen-units = 1 +rpath = false diff --git a/pilota-build/src/test/mod.rs b/pilota-build/src/test/mod.rs index cb1b88ca..732ea45a 100644 --- a/pilota-build/src/test/mod.rs +++ b/pilota-build/src/test/mod.rs @@ -7,9 +7,11 @@ use tempfile::tempdir; use crate::plugin::SerdePlugin; fn diff_file(old: impl AsRef, new: impl AsRef) { - let old_content = unsafe { String::from_utf8_unchecked(std::fs::read(old).unwrap()) }; + let old_content = + unsafe { String::from_utf8_unchecked(std::fs::read(old).unwrap()) }.replace("\r", ""); - let new_content = unsafe { String::from_utf8_unchecked(std::fs::read(new).unwrap()) }; + let new_content = + unsafe { String::from_utf8_unchecked(std::fs::read(new).unwrap()) }.replace("\r", ""); let patch = diffy::create_patch(&old_content, &new_content); if !patch.hunks().is_empty() { diff --git a/pilota/Cargo.toml b/pilota/Cargo.toml index 4a600918..dcb98583 100644 --- a/pilota/Cargo.toml +++ b/pilota/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pilota" -version = "0.6.2" +version = "0.6.3" edition = "2021" description = "Pilota is a thrift and protobuf implementation in pure rust with high performance and extensibility." documentation = "https://docs.rs/pilota" @@ -29,7 +29,18 @@ derivative = "2" anyhow = "1" thiserror = "1" faststr = { version = "0.2", features = ["serde"] } -integer-encoding = { version = "3", features = ["async-trait", "tokio", "tokio_async"] } +integer-encoding = { version = "3", features = [ + "async-trait", + "tokio", + "tokio_async", +] } proptest = "1" serde = { version = "1", features = ["derive"] } +[dev-dependencies] +criterion = "0.4" +rand = "0.8" + +[[bench]] +name = "faststr" +harness = false diff --git a/pilota/benches/faststr.rs b/pilota/benches/faststr.rs new file mode 100644 index 00000000..4197f534 --- /dev/null +++ b/pilota/benches/faststr.rs @@ -0,0 +1,170 @@ +#![allow(clippy::redundant_clone)] +use std::{iter::repeat_with, sync::Arc}; + +use bytes::BytesMut; +use criterion::{black_box, criterion_group, criterion_main}; +use faststr::FastStr; +use rand::Rng; + +const INLINE_CAP: usize = 22; +const KB: usize = 1024; + +fn faststr_bench(c: &mut criterion::Criterion) { + let mut group = c.benchmark_group("Bench FastStr Inline"); + + let s = gen_string(INLINE_CAP); + group.bench_function("faststr inline no clone", |b| { + b.iter(|| { + black_box(FastStr::new_inline(&s)); + }) + }); + group.bench_function("Arc new no clone", |b| { + b.iter(|| { + let _: Arc = black_box(s.as_str().into()); + }) + }); + + group.bench_function("faststr inline 1 clone", |b| { + b.iter(|| { + let s = FastStr::new_inline(&s); + let s1 = black_box(s.clone()); + black_box(s1); + }) + }); + group.bench_function("Arc new 1 clone", |b| { + b.iter(|| { + let s: Arc = s.as_str().into(); + let s1 = black_box(s.clone()); + black_box(s1); + }) + }); + + group.bench_function("faststr inline 3 clones", |b| { + b.iter(|| { + let s = FastStr::new_inline(&s); + let s1 = black_box(s.clone()); + let s2 = black_box(s.clone()); + let s3 = black_box(s.clone()); + black_box(s1); + black_box(s2); + black_box(s3); + }) + }); + group.bench_function("Arc new 3 clone", |b| { + b.iter(|| { + let s: Arc = s.as_str().into(); + let s1 = black_box(s.clone()); + let s2 = black_box(s.clone()); + let s3 = black_box(s.clone()); + black_box(s1); + black_box(s2); + black_box(s3); + }) + }); + + group.finish(); + + let mut group = c.benchmark_group("Bench FastStr BytesMut"); + + let lens = [KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB, 32 * KB]; + for len in lens { + let s = gen_string(len); + + #[inline] + fn gen_bytes_mut(s: &str) -> BytesMut { + let mut s2 = String::with_capacity(s.len() * 3); + s2.push_str(s); + s2.push_str(s); + s2.push_str(s); + BytesMut::from(s2.as_str()) + } + + #[inline] + fn read_faststr_from_bytes_mut(buf: &mut BytesMut) -> FastStr { + let l = buf.len(); + let b = buf.split_to(l / 3).freeze(); + unsafe { FastStr::from_bytes_unchecked(b) } + } + + group.bench_function(format!("from bytes no clone, length: {}", len), |b| { + b.iter_batched_ref( + || gen_bytes_mut(s.as_str()), + |buf| { + black_box(read_faststr_from_bytes_mut(buf)); + }, + criterion::BatchSize::PerIteration, + ); + }); + group.bench_function(format!("faststr new no clone, length: {}", len), |b| { + b.iter(|| { + black_box(FastStr::new(&s)); + }) + }); + + group.bench_function(format!("from bytes 1 clone, length: {}", len), |b| { + b.iter_batched_ref( + || gen_bytes_mut(s.as_str()), + |buf| { + let s = read_faststr_from_bytes_mut(buf); + let s1 = black_box(s.clone()); + black_box(s1); + }, + criterion::BatchSize::PerIteration, + ); + }); + group.bench_function(format!("faststr new 1 clone, length: {}", len), |b| { + b.iter(|| { + let s = FastStr::new(&s); + let s1 = black_box(s.clone()); + black_box(s1); + }) + }); + + group.bench_function(format!("from bytes 3 clones, length: {}", len), |b| { + b.iter_batched_ref( + || gen_bytes_mut(s.as_str()), + |buf| { + let s = read_faststr_from_bytes_mut(buf); + let s1 = black_box(s.clone()); + let s2 = black_box(s.clone()); + let s3 = black_box(s.clone()); + black_box(s1); + black_box(s2); + black_box(s3); + }, + criterion::BatchSize::PerIteration, + ); + }); + group.bench_function(format!("faststr new 3 clones, length: {}", len), |b| { + b.iter(|| { + let s = FastStr::new(&s); + let s1 = black_box(s.clone()); + let s2 = black_box(s.clone()); + let s3 = black_box(s.clone()); + black_box(s1); + black_box(s2); + black_box(s3); + }) + }); + } + + group.finish(); +} + +fn gen_string(size: usize) -> String { + const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\ + abcdefghijklmnopqrstuvwxyz\ + 0123456789"; + let mut rng = rand::thread_rng(); + let b: Vec = repeat_with(|| { + let i = rng.gen_range(0..CHARSET.len()); + CHARSET[i] + }) + .take(size) + .collect(); + let chars: Vec = b.into_iter().map(|b| b as char).collect(); + chars.into_iter().collect() +} + +criterion_group!(benches, faststr_bench); +criterion_main!(benches); diff --git a/pilota/src/thrift/binary.rs b/pilota/src/thrift/binary.rs index 16083a7e..90668b17 100644 --- a/pilota/src/thrift/binary.rs +++ b/pilota/src/thrift/binary.rs @@ -13,7 +13,7 @@ use super::{ DecodeError, DecodeErrorKind, EncodeError, ProtocolError, TAsyncInputProtocol, TFieldIdentifier, TInputProtocol, TLengthProtocol, TListIdentifier, TMapIdentifier, TMessageIdentifier, TMessageType, TOutputProtocol, TSetIdentifier, TStructIdentifier, TType, - INLINE_CAP, ZERO_COPY_THRESHOLD, + ZERO_COPY_THRESHOLD, }; static VERSION_1: u32 = 0x80010000; @@ -904,10 +904,10 @@ impl TInputProtocol for TBinaryProtocol<&mut BytesMut> { fn read_faststr(&mut self) -> Result { let len = self.trans.read_i32()? as usize; let bytes = self.trans.split_to(len).freeze(); - if len > INLINE_CAP { + if len >= ZERO_COPY_THRESHOLD { unsafe { return Ok(FastStr::from_bytes_unchecked(bytes)) }; } - unsafe { Ok(FastStr::new_inline(str::from_utf8_unchecked(bytes.deref()))) } + unsafe { Ok(FastStr::new(str::from_utf8_unchecked(bytes.deref()))) } } #[inline] diff --git a/pilota/src/thrift/compact.rs b/pilota/src/thrift/compact.rs index 1b82b0cb..6e5f76a0 100644 --- a/pilota/src/thrift/compact.rs +++ b/pilota/src/thrift/compact.rs @@ -18,7 +18,7 @@ use super::{ DecodeError, DecodeErrorKind, EncodeError, ProtocolError, TAsyncInputProtocol, TFieldIdentifier, TInputProtocol, TLengthProtocol, TListIdentifier, TMapIdentifier, TMessageIdentifier, TMessageType, TOutputProtocol, TSetIdentifier, TStructIdentifier, TType, - INLINE_CAP, ZERO_COPY_THRESHOLD, + ZERO_COPY_THRESHOLD, }; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -1417,10 +1417,10 @@ impl TInputProtocol for TCompactInputProtocol<&mut BytesMut> { fn read_faststr(&mut self) -> Result { let size = self.read_varint::()? as usize; let bytes = self.trans.split_to(size); - if size > INLINE_CAP { + if size >= ZERO_COPY_THRESHOLD { unsafe { return Ok(FastStr::from_bytes_mut_unchecked(bytes)) } } - unsafe { Ok(FastStr::new_inline(str::from_utf8_unchecked(bytes.deref()))) } + unsafe { Ok(FastStr::new(str::from_utf8_unchecked(bytes.deref()))) } } #[inline] diff --git a/pilota/src/thrift/mod.rs b/pilota/src/thrift/mod.rs index 5defa714..92aa9834 100644 --- a/pilota/src/thrift/mod.rs +++ b/pilota/src/thrift/mod.rs @@ -17,8 +17,15 @@ use faststr::FastStr; pub use self::{binary::TAsyncBinaryProtocol, compact::TAsyncCompactProtocol}; const MAXIMUM_SKIP_DEPTH: i8 = 64; -const ZERO_COPY_THRESHOLD: usize = 4 * 1024; // 4KB -const INLINE_CAP: usize = 22; + +#[cfg(all(target_os = "macos", target_arch = "aarch64"))] +// According to the benchmark, 1KB is the suitable threshold for zero-copy on +// Apple Silicon. +const ZERO_COPY_THRESHOLD: usize = 1024; + +#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))] +// While 4KB is better for other platforms (mainly amd64 linux). +const ZERO_COPY_THRESHOLD: usize = 4 * 1024; lazy_static::lazy_static! { pub static ref VOID_IDENT: TStructIdentifier = TStructIdentifier { name: "void" }; @@ -639,7 +646,7 @@ pub trait TAsyncInputProtocol: Send { /// Read the end of a Thrift message. async fn read_message_end(&mut self) -> Result<(), DecodeError>; - /// Read the beginning of a Thrift struct. + /// Read the beginning of a Thrift struct. async fn read_struct_begin(&mut self) -> Result, DecodeError>; /// Read the end of a Thrift struct.