Skip to content

Conversation

@giacomocavalieri
Copy link
Member

This PR closes #5297.
This seems to be a really straightforward fix: the docvec! macro ended up creating deeply nested documents vec(vec(vec(vec(...), ...), ...), ...). Now if the first item passed to the macro is a vector, it behaves the same as appending to that vector rather than creating a new vector every single time!

  • The changes in this PR have been discussed beforehand in an issue
  • The issue for this PR has been linked
  • Tests have been added for new behaviour
  • The changelog has been updated for any user-facing changes

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Quick question inline!

// So in case a `docvec!` starts with a `Vec` we flatten it out to avoid
// having deeply nested documents.
match $first.to_doc() {
first@Document::Vec(_) => first.append(docvec![$($rest.to_doc()),+]),
Copy link
Member

Choose a reason for hiding this comment

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

Why not append all the items to the inner vector instead of recursively calling the macro?

Copy link
Member Author

Choose a reason for hiding this comment

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

This way we can flatten all vectors at the top of the macro:

docvec![vec1, vec2, a, b] // becomes =>
vec1.append(vec2.append(vec![a, b]))
// Meaning the first two vectors are flattened out, rather than only the first one

Copy link
Member

@lpil lpil Jan 18, 2026

Choose a reason for hiding this comment

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

That moves the elements multiples times needlessly, potentially allocating new vectors and copying the existing elements each time. If the Vec is worked with directly it allocates at most once, and moves the new elements once. Is there a reason to do more work to restructure the tree?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mhm you're right and just doing it for the first element seems to be fixing the issue anyway. I'll change it to that!

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.

Formatter stack overflows with long lists

2 participants