{"id":16116278,"url":"https://github.com/frol/rust-benchmark-option-replace_with-rfc","last_synced_at":"2025-04-06T08:44:43.745Z","repository":{"id":139346745,"uuid":"139108867","full_name":"frol/rust-benchmark-option-replace_with-rfc","owner":"frol","description":null,"archived":false,"fork":false,"pushed_at":"2018-06-29T10:12:16.000Z","size":7,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-12T14:19:17.733Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-29T06:15:56.000Z","updated_at":"2021-03-13T22:31:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"6cc59370-f951-4f19-a07b-cbb10def8fd3","html_url":"https://github.com/frol/rust-benchmark-option-replace_with-rfc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Frust-benchmark-option-replace_with-rfc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Frust-benchmark-option-replace_with-rfc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Frust-benchmark-option-replace_with-rfc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Frust-benchmark-option-replace_with-rfc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frol","download_url":"https://codeload.github.com/frol/rust-benchmark-option-replace_with-rfc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457740,"owners_count":20941906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T20:21:55.104Z","updated_at":"2025-04-06T08:44:43.727Z","avatar_url":"https://github.com/frol.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Benchmark `Option::replace_with` RFC\n\nThis benchmark compares three implementations of the following idea ([more details in Rust RFC #2940](https://github.com/rust-lang/rfcs/pull/2490)):\n\n```rust\nlet mut some_option: Option\u003ci32\u003e = Some(123);\n\nsome_option = consume_option_i32_and_produce_option_i32(some_option.take());\n```\n\nThis straightforward implementation turns out to be non-optimal and thus some `unsafe` + `mem::forget` produces faster code and thus it is proposed to add a special `Option::replace_with` method.\n\nIn this benchmark compares:\n\n1. Naive `Option::take` + reassignment implementation\n2. Ad-hoc `mem::swap` + `mem::forget` implementation\n3. Proposed `Option::replace_with` implementation\n\nTo run the benchmark, just do `cargo run --release`.\n\nHere is the output on my Core i7-4710HQ laptop (Arch Linux, x64, 4.17.2 kernel):\n\n```\nReplace Option with a new value computed from an old value/naive assignment\n                        time:   [31.159 ns 31.259 ns 31.368 ns]\nFound 4 outliers among 100 measurements (4.00%)\n  3 (3.00%) high mild\n  1 (1.00%) high severe\n\n\nReplace Option with a new value computed from an old value/mem::swap + mem::f...\n                        time:   [26.729 ns 26.836 ns 26.956 ns]\nFound 10 outliers among 100 measurements (10.00%)\n  8 (8.00%) high mild\n  2 (2.00%) high severe\n\n\nReplace Option with a new value computed from an old value/Option::replace_with\n                        time:   [26.742 ns 26.816 ns 26.903 ns]\nFound 4 outliers among 100 measurements (4.00%)\n  2 (2.00%) high mild\n  2 (2.00%) high severe\n```\n\nThus, naive implementation is about 14% slower (31 ns VS 26 ns) than the proposed implementation.\n\nHere is a comparison of the produced assembly code: https://godbolt.org/g/6Cukig (naive implementation is on the left, and optimized implementation is on the right).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrol%2Frust-benchmark-option-replace_with-rfc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrol%2Frust-benchmark-option-replace_with-rfc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrol%2Frust-benchmark-option-replace_with-rfc/lists"}