{"id":26159612,"url":"https://github.com/virxec/combo_vec","last_synced_at":"2025-04-14T10:53:47.705Z","repository":{"id":107554012,"uuid":"537217121","full_name":"VirxEC/combo_vec","owner":"VirxEC","description":"A library for creating a \"combo stack array-heap vector\", or simply a re-sizable array","archived":false,"fork":false,"pushed_at":"2024-03-24T00:35:46.000Z","size":73,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-25T10:21:37.087Z","etag":null,"topics":["data-structures","rust","stack"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/combo_vec","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/VirxEC.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":"2022-09-15T21:51:08.000Z","updated_at":"2023-11-22T04:01:40.000Z","dependencies_parsed_at":"2023-11-22T01:59:12.965Z","dependency_job_id":"bb988f83-68e5-4e93-99fb-2a595d36f7b9","html_url":"https://github.com/VirxEC/combo_vec","commit_stats":{"total_commits":16,"total_committers":1,"mean_commits":16.0,"dds":0.0,"last_synced_commit":"cae18a85da81348784caf01012b49a2d178dba46"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirxEC%2Fcombo_vec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirxEC%2Fcombo_vec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirxEC%2Fcombo_vec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VirxEC%2Fcombo_vec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VirxEC","download_url":"https://codeload.github.com/VirxEC/combo_vec/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248868773,"owners_count":21174757,"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":["data-structures","rust","stack"],"created_at":"2025-03-11T11:33:04.775Z","updated_at":"2025-04-14T10:53:47.698Z","avatar_url":"https://github.com/VirxEC.png","language":"Rust","readme":"# combo_vec\n\n[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)\n\n`ComboVec` is for creating a \"combo stack array-heap vector\", or simply a resizable array with a vector for extra allocations.\n\nNot only that, but this library has `ReArr` if you just want the resizable array part!\n\nCreate a new `ComboVec` with the `combo_vec!` macro and a new `ReArr` with the `re_arr!` macro.\n\nThis works by allocating an array of `T` on the stack, and then using a Vec on the heap for overflow.\n\nThe stack-allocated array is always used to store the first `N` elements, even when the array is resized.\n\n_No_ `Default`, `Copy`, or `Clone` traits are required for `T` at all;\nbut if T does implement any of them, then `ComboVec` and `ReArr` will also implement them.\nThis also applied to `PartialEq`, `PartialOrd`, `Eq`, `Ord`, `Hash`, `Debug`, and `Display`.\n\n## Why use ComboVec\n\nThis is mostly used for when you know the maximum number of elements that will be stored 99% if the time, but don't want to cause errors in the last 1% and also won't want to give up on the performance of using the stack instead of the heap most of the time.\n\nI've gotten performance bumps with `ComboVec` over the similar type SmallVec (both with and without it's `union` feature.)\n\nIn a test of pushing 2048 (pre-allocated) elements, almost a 54% performance increase is shown:\n\n- `ComboVec`: 4.54 µs\n- `SmallVec`: 9.33 µs\n\nThe `combo_vec!` macro is very nice and convenient to use even in const contexts.\n\n```rust\nuse combo_vec::{combo_vec, ComboVec};\n\nconst SOME_ITEMS: ComboVec\u003ci8, 3\u003e = combo_vec![1, 2, 3];\nconst MANY_ITEMS: ComboVec\u003cu16, 90\u003e = combo_vec![5; 90];\nconst EXTRA_ITEMS: ComboVec\u003c\u0026str, 5\u003e = combo_vec![\"Hello\", \"world\", \"!\"; None, None];\n\n// Infer the type and size of the ComboVec\nconst NO_STACK_F32: ComboVec\u003cf32, 0\u003e = combo_vec![];\n\n// No const-initialization is needed to create a ComboVec with allocated elements on the stack\nuse std::collections::HashMap;\nconst EMPTY_HASHMAP_ALLOC: ComboVec\u003cHashMap\u003c\u0026str, i32\u003e, 3\u003e = combo_vec![];\n\n// Creating a new ComboVec at compile time and doing this does have performance benefits\nlet my_combo_vec = EMPTY_HASHMAP_ALLOC;\n```\n\n`ComboVec` also implements many methods that are exclusive to `Vec` such as `extend`, `truncate`, `push`, `join` etc.\n\n## Why use ReArr\n\nIn a test of pushing 2048 (pre-allocated) elements, it ties for performance with `ArrayVec`:\n\n- `ReArr`: 4.07 µs\n- `ArrayVec`: 4.00 µs\n\nThe `re_arr!` macro is very nice and convenient to use even in const contexts.\n\n```rust\nuse combo_vec::{re_arr, ReArr};\n\nconst SOME_ITEMS: ReArr\u003ci8, 3\u003e = re_arr![1, 2, 3];\nconst MANY_ITEMS: ReArr\u003cu16, 90\u003e = re_arr![5; 90];\nconst EXTRA_ITEMS: ReArr\u003c\u0026str, 5\u003e = re_arr![\"Hello\", \"world\", \"!\"; None, None];\n\n// Infer the type and size of the ReArr\nconst NO_STACK_F32: ReArr\u003cf32, 0\u003e = re_arr![];\n\n// No const-initialization is needed to create a ComboVec with allocated elements on the stack\nuse std::collections::HashMap;\nconst EMPTY_HASHMAP_ALLOC: ReArr\u003cHashMap\u003c\u0026str, i32\u003e, 3\u003e = re_arr![];\n\n// Creating a new ReArr at compile time and doing this does have performance benefits\nlet my_re_arr = EMPTY_HASHMAP_ALLOC;\n```\n\n`ReArr` also implements many methods that are exclusive to `Vec` such as `extend`, `truncate`, `push`, `join` etc.\n\n## Examples\n\nA quick look at a basic example and some methods that are available:\n\n```rust\nuse combo_vec::combo_vec;\n\nlet mut combo_vec = combo_vec![1, 2, 3];\n// Allocate an extra element on the heap\ncombo_vec.push(4);\n// Truncate to a length of 2\ncombo_vec.truncate(2);\n// Fill the last element on the stack, then allocate the next items on the heap\ncombo_vec.extend([3, 4, 5]);\n```\n\n### Allocating empty memory on the stack\n\nYou can allocate memory on the stack for later use without settings values to them!\n\nNo Copy or Default traits required.\n\n```rust\nuse combo_vec::{ComboVec, ReArr};\n\n// Allocate a new space to store 17 elements on the stack.\nlet empty_combo_vec = ComboVec::\u003cf32, 17\u003e::new();\nlet empty_re_arr = ReArr::\u003cf32, 17\u003e::new();\n```\n\n### Allocating memory on the stack in const contexts\n\nThe main benefit of using the `combo_vec!`/`re_arr!` macros is that everything it does can be used in const contexts.\n\nThis allows you to allocate a ComboVec at the start of your program in a Mutex or RwLock, and have minimal runtime overhead.\n\n```rust\nuse combo_vec::{combo_vec, ComboVec, re_arr, ReArr};\n\n// Create a global variable for the various program states for a semi-unspecified length\nuse std::{collections::HashMap, sync::RwLock};\nstatic PROGRAM_STATES: RwLock\u003cComboVec\u003cHashMap\u003cString, i32\u003e, 20\u003e\u003e = RwLock::new(combo_vec![]);\n\n// If we know the stack will never be larger than 20 elements,\n// we can get a performance boost by using ReArr instead of ComboVec\nlet mut runtime_stack = ReArr::\u003ci32, 20\u003e::new();\n```\n\n### Go fast with const \u0026 copy\n\nWe can take advantage of `ComboVec` and `ReArr` by creating one const context then copying it to our runtime variable.\nThis is much faster than creating a new `ComboVec` at runtime, and `T` does _not_ need to be `Copy`.\n\nHere's a basic look at what this looks like:\n\n```rust\nuse combo_vec::{combo_vec, ComboVec};\n\nconst SOME_ITEMS: ComboVec\u003cString, 2\u003e = combo_vec![];\n\nfor _ in 0..50 {\n    let mut empty_combo_vec = SOME_ITEMS;\n    empty_combo_vec.push(\"Hello\".to_string());\n    empty_combo_vec.push(\"world\".to_string());\n    println!(\"{}!\", empty_combo_vec.join(\" \"));\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirxec%2Fcombo_vec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvirxec%2Fcombo_vec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirxec%2Fcombo_vec/lists"}