Skip to content

Avoid cloning in Array::filter_map_to (for map_each)#183

Merged
marmeladema merged 4 commits into
cloudflare:masterfrom
utkarshgupta137:ugupta/perf/map_each
Jul 8, 2026
Merged

Avoid cloning in Array::filter_map_to (for map_each)#183
marmeladema merged 4 commits into
cloudflare:masterfrom
utkarshgupta137:ugupta/perf/map_each

Conversation

@utkarshgupta137

Copy link
Copy Markdown
Collaborator

No description provided.

@utkarshgupta137 utkarshgupta137 self-assigned this Jul 3, 2026
let mut write = 0;
for read in 0..vec.len() {
let elem = &mut vec[read];
if let Some(elem) = func(std::mem::replace(elem, LhsValue::Bool(false))) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first glance, I am not convince by this change. Before/after seem equivalent? Before we would only allocate a vec for the borrowed case; which is done anyway in the new code further down.

The only thing that seems to be avoided in the new code is the mem::replace in the borrowed case. Is that really responsible for a substantial speedup?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is in the deleted line 166: slice.to_vec(), which clones & allocates, only to throw away the cloned/allocated LhsValues (often bytes) & using the Vec to store bools. It is better to pass a borrowed LhsValue & let the called function be optimised for borrowed cases.

@marmeladema
marmeladema merged commit 61936e5 into cloudflare:master Jul 8, 2026
8 checks passed
@utkarshgupta137
utkarshgupta137 deleted the ugupta/perf/map_each branch July 14, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants