8000 Optimize `Vec::split_off` · Issue #161 · oxc-project/backlog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000
Optimize Vec::split_off #161
Open
Open
@overlookmotel

Description

@overlookmotel

Now that we have our own Vec implementation, we can add some APIs which were previously impossible.

Vec::split_at can convert a Vec into 2 x Vecs, without copying any data or making any allocations.

let original_len = vec.len();
let original_capacity = vec.capacity();
let original_ptr = vec.as_ptr();

// `split_at` returns `None` if `vec.len() < 2`
let (vec1, vec2) = vec.split_at(2).unwrap();

assert!(vec1.len() == 2);
assert!(vec1.capacity() == 2);
assert!(vec2.len() == original_len - 2);
assert!(vec2.capacity() == original_capacity - 2);
// `vec1` and `vec2` point to chunks of the original `vec`
assert!(vec1.as_ptr() == original_ptr);
assert!(vec2.as_ptr() == original_ptr.add(2));

This would be useful in various places in transformer where we currently have to allocate new Vecs and copy data around. e.g. oxc-project/oxc#10434 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0