{"id":17326039,"url":"https://github.com/lucretiel/defer-drop","last_synced_at":"2025-08-06T08:19:13.041Z","repository":{"id":57617265,"uuid":"265897872","full_name":"Lucretiel/defer-drop","owner":"Lucretiel","description":"Defer dropping large structs to a background thread","archived":false,"fork":false,"pushed_at":"2022-11-02T23:44:57.000Z","size":28,"stargazers_count":62,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T21:14:32.666Z","etag":null,"topics":["cleanup","raii","rust","rust-library"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lucretiel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-21T16:28:36.000Z","updated_at":"2025-02-07T10:32:56.000Z","dependencies_parsed_at":"2023-01-21T12:49:06.372Z","dependency_job_id":null,"html_url":"https://github.com/Lucretiel/defer-drop","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucretiel%2Fdefer-drop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucretiel%2Fdefer-drop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucretiel%2Fdefer-drop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lucretiel%2Fdefer-drop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lucretiel","download_url":"https://codeload.github.com/Lucretiel/defer-drop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595335,"owners_count":20963943,"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":["cleanup","raii","rust","rust-library"],"created_at":"2024-10-15T14:14:53.413Z","updated_at":"2025-04-07T05:11:28.553Z","avatar_url":"https://github.com/Lucretiel.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# defer-drop\n\nA utility type that allows you to defer dropping your data to a background thread. Inspired by [this article](https://abramov.io/rust-dropping-things-in-another-thread) by Aaron Abramov.\n\n## At a glance\n\n```rust\n// examples/demo.rs\n\nprintln!(\"Allocating a ridiculously large vector\");\nlet vec1: Vec\u003cVec\u003cString\u003e\u003e = repeat_with(|| {\n    repeat_with(|| \"Hello, World\".to_string())\n        .take(1000)\n        .collect()\n})\n.take(1000)\n.collect();\n\nprintln!(\"Duplicating that vector\");\nlet vec2 = vec1.clone();\n\n// Drop vec1 in a background thread\nlet defer_vec1 = DeferDrop::new(vec1);\n\nprintln!(\"Dropping the vectors\");\n\nlet vec1_timer = timer(move || drop(defer_vec1));\nlet vec2_timer = timer(move || drop(vec2));\n\nprintln!(\"Duration of deferred drop: {:?}\", vec1_timer);\nprintln!(\"Duration of foreground drop: {:?}\", vec2_timer);\n```\n\nOn my machine, this prints:\n\n```\nAllocating a ridiculously large vector\nDuplicating that vector\nDropping the vectors\nDuration of deferred drop: 178µs\nDuration of foreground drop: 102.5139ms\n```\n\nYou can run this example yourself with:\n\n```\ncargo run --example demo\n```\n\n## Summary\n\ndefer-drop provides a wrapper type that, when dropped, sends the inner value to a global background thread to be dropped. Useful in cases where a value takes a long time to drop (for instance, a windows file that might block on close, or a large data structure that has to extensively recursively trawl itself).\n\n## Notes\n\nCarefully consider whether this pattern is necessary for your use case. Like all worker-thread abstractions, sending the value to a separate thread comes with its own costs, so it should only be done if performance profiling indicates that it's a performance gain.\n\nThere is only one global worker thread. Dropped values are enqueued in an unbounded channel to be consumed by this thread; if you produce more garbage than the thread can handle, this will cause unbounded memory consumption. There is currently no way for the thread to signal or block if it is overwhelmed.\n\nAll of the standard non-determinism threading caveats apply here. The objects are guaranteed to be destructed in the order received through a channel, which means that objects sent from a single thread will be destructed in order. However, there is no guarantee about the ordering of interleaved values from different threads. Additionally, there are no guarantees about how long the values will be queued before being dropped, or even that they will be dropped at all. If your `main` thread terminates before all drops could be completed, they will be silently lost (as though via a `mem::forget`.This behavior is entirely up to your OS's thread scheduler. There is no way to receive a signal indicating when a particular object was dropped.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucretiel%2Fdefer-drop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucretiel%2Fdefer-drop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucretiel%2Fdefer-drop/lists"}