Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid copying fragments #3136

Merged
merged 3 commits into from
Aug 4, 2022
Merged

Conversation

MDeiml
Copy link
Contributor

@MDeiml MDeiml commented Jul 21, 2022

Might fix #3127.

This changes the Range::fragment and Selection::fragments to return RopeSlice. This way we can avoid copying the ropes' contents in a few cases, specifically

  • shell_impl
  • Sometimes one less copy in switch_to_*case
  • Sometimes one less copy in all commands that "yank"
  • Sometimes one less copy in sort and rotate_selection_contents

Also, it will make the computational overhead of copying the fragments more apparent in places where Range::fragment is used (until now it almost looked like there was no copy, since a Cow was returned).

(I also rewrite the mouse event handling because (I think, without optimizations) until now it copied the entire file on each mouse up event)

@archseer
Copy link
Member

In most cases we do want to use a Cow though. Why not add a second slice() method that will return a RopeSlice for cases where that's preferrable?

@MDeiml
Copy link
Contributor Author

MDeiml commented Jul 22, 2022

It's more of an opinion, but I think it's always better to not hide computational complexity in APIs. A lot of use cases want to iterate over the fragment, which can be done without copying the contents.

Ideally we could also avoid the places where we turn the RopeSlice into a Cow since that could always end up copying the entire file. This is what happens if you press %s at the moment since the regex crates methods only take strings.

@archseer
Copy link
Member

The fragment/fragments API was intended for cases where we're operating on small user selections (or when rendering and a single scope is likely to fit inside a single rope chunk). In those cases it's guaranteed for the Cow to be a borrowed reference, but if not we allocate since the APIs downstream expect to operate on a single continuous string.

For cases where we're iterating over raw rope chunks, there's rope.chunks() and I'd prefer to see an analog for that, e.g. range.chunks(rope).

This is what happens if you press %s at the moment since the regex crates methods only take strings.

Yeah this is a known issue, we can't really solve this until the next version of regex-automata is finished (rust-lang/regex#425) or by implementing our own NFA.

Current implementation uses regex as a stopgap until we can shift to a better implementation. #211 (comment)

@MDeiml
Copy link
Contributor Author

MDeiml commented Jul 22, 2022

Ok I'll change this to add a chunks method.

The regex functions are actually the only ones that need Cows. Commands that yank need an owned string (which can be constructed from a Cow). All other commands can avoid one copy by using iterators.

Copy link
Member

@archseer archseer left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! 🎉

@archseer archseer merged commit 0ee2061 into helix-editor:master Aug 4, 2022
@MDeiml MDeiml deleted the avoid-fragment-copy branch August 4, 2022 07:42
thomasskk pushed a commit to thomasskk/helix that referenced this pull request Sep 9, 2022
* Avoid copying fragments

* Add slice / slices method

* Better documentation for fragment and slice methods
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.

:pipe hangs when piping a large file through sed
3 participants