Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Supply getRange to scanInRange iterator - #64

Merged
nathansobo merged 9 commits into
masterfrom
as-scan-range-computation
May 26, 2015
Merged

nathansobo merged 9 commits into
masterfrom
as-scan-range-computation

Conversation

@as-cii

@as-cii as-cii commented May 25, 2015

Copy link
Copy Markdown
Contributor

While scanning a large file, we may not want to compute the position for every result. This is especially important for searches where there’s an elevated amount of matches and we’re interested only in a subset of them.

This commit introduces a getRange parameter in the iterator function, which is suited to be a drop-in replacement for range. The logic is the same, but passing a function, or a getter if you will, enables us to perform all the position math (i.e. positionForCharacterIndex is rather expensive) lazily. 🐎

This is still “slow”, however, because we cannot delete the range parameter without first changing all the scan methods' clients to use getRange instead. Thus, we need to work incrementally by shipping both versions (eagerly and lazily computed range) and remove the eagerly computed one as soon as we complete the migration.

After that, we will benefit from a significant performance boost thereby solving bracket-matcher slowness when matching parentheses in a reasonably long file.

Core Repos To Be Updated

/cc: @nathansobo @maxbrunsfeld @atom/feedback

While scanning a large file, we may not want to compute the position
for every result. This is especially important for searches where
there’s an elevated amount of matches and we’re interested only in a
subset of them.

This commit introduces a `getRange` function in the `iterator`
function, which is suited to be a drop-in replacement for `range`. The
logic is the same, but passing a function, or a getter if you will,
enables us to perform all the position math (which is rather expensive)
lazily. 🐎

This is still “slow”, however, because we cannot delete the `range`
parameter without an incremental approach, therefore I needed to keep
the eager position computation. As soon as we’ve migrated all the
clients, we can get rid of it and benefit from a significant
performance boost.
@as-cii

as-cii commented May 25, 2015

Copy link
Copy Markdown
Contributor Author

The benchmarks provided in the screenshots below show the potential of the proposed changes.

(Tested on 55k LOC file, with a parenthesis at the beginning and the matching one at the end. Think to atom/benchmark/fixtures/huge.js but with the content between the parentheses 5 times larger)

Before

before

After

after

🐎 🐎 🐎

When the file is larger the performance impact is even more evident 📈

@lee-dohm

Copy link
Copy Markdown
Contributor

Third-party packages may be using scanInRange as well. If we are going to hold to the API freeze, will we be able to remove the range parameter?

@as-cii

as-cii commented May 25, 2015

Copy link
Copy Markdown
Contributor Author

Third-party packages may be using scanInRange as well. If we are going to hold to the API freeze, will we be able to remove the range parameter?

That's a fair question: thank you for bringing this up @lee-dohm 🙇

If that's the constraint, we might as well pass a parameter to tell this function to explicitly skip the eager position computation; namely, I am thinking to change scanInRange's signature to something like:

scanInRange: (regex, range, iterator, reverse=false, computePositionsEagerly = false) ->

I am 😢 because this feels a bit awkward to say the least, given that the boolean doesn't really change this function's behavior. On the other hand we won't break clients and they can always choose to opt-in this improvement gradually (to the point that we'll be able to remove the boolean).

Thoughts? 💭

@lee-dohm

Copy link
Copy Markdown
Contributor

I'm 😢 too because this is a huge win as far as performance goes. The only alternative I've been able to think of is to create a new scanInRangeLazy function (stupid name, I know), but that just clutters up the API. I think your solution is the best one that doesn't break the API.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

I'd suggest moving all our packages to use getRange, but defining range as a lazily-computed property via Object.defineProperty, so that third property packages can still use it. Maybe at some point we could deprecate it.

I'm not sure what the cost is of calling Object.defineProperty in a tight loop like this. If it's significant, we could introduce a class for these event objects, and only define the property once: on the prototype.

@nathansobo

Copy link
Copy Markdown
Contributor

We should absolutely move to a property getter on the prototype of the event object. Should be completely drop in unless I'm missing something.

as-cii added 3 commits May 26, 2015 09:40
This enables us to declare `range` as a computed property, thus
allowing not to break existing clients while taking advantage of the
performance improvement.
@as-cii

as-cii commented May 26, 2015

Copy link
Copy Markdown
Contributor Author

Thanks for your suggestions, @nathansobo and @maxbrunsfeld. Eventually, I went with an event object which defines range as a computed property on the prototype. Everything is still snappy 🐎

However, as suggested by @maxbrunsfeld, I also included getRange as a public method: I have mixed feelings about this, but ultimately I think it's clearer if clients just use getRange instead, as to remark that we may have some computation in there and to avoid assignments to that variable (which from now on will be simply ignored unless strict mode is enabled and things may not work as expected).

I think the roadmap for this should be:

  • :shipit: and bump version of text-buffer
  • ⬆️ text-buffer on atom/atom, migrating clients as needed
  • Migrate all the other clients that I have listed in this PR's body, bumping their version
  • ⬆️ such clients on atom/atom
  • Release Atom 🚀

What do you think? 💭

@lee-dohm

Copy link
Copy Markdown
Contributor

Sounds like a good plan to me! Thanks for so elegantly addressing my concerns about the API 😀

Comment thread src/text-buffer.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure about the name "callback" as it implies a function to me, whereas this is an object. What about just SearchResult? Or are you looking for some other word to indicate the control methods it provides?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure about the name "callback" as it implies a function to me, whereas this is an object.

Agreed. 👍

Or are you looking for some other word to indicate the control methods it provides?

Exactly. I'd expect a SearchResult to be a simple value object, whereas this one provides a bunch of features which seem to be outside the scope of an object which merely holds data.

Also, I would stray a bit from SearchResult as it may imply that we return the result of the whole search, whilst what we're really passing along is a search occurrence.

I am thinking to something along the lines of SearchOccurrence, but that clearly expresses that it provides control over the iteration as well. SearchOccurrenceIteratee maybe? 😕

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

avoid assignments to that variable

Nice catch.

Migrate all the other clients that I have listed in this PR's body, bumping their version.

Usually, we wait to use features in packages until they are in a released version of Atom. That way, the packages' CI builds can continue to pass against the latest Atom release, and if any issues come up that delay our release of Atom, we can continue to release new package versions.

So I would say that we should release this change in a completely backward-compatible manner before changing any package code. To deal with the case that you caught above, where find-and-replace assigned to the range property, we should add a set descriptor to the range property that sets the @computedRange memoized value. I think that keep find-and-replace working as it did before.

I still think we should add getRange function for the reasons you mentioned above, but we should release a new version of Atom before we migrate any packages to use that function.

Comment thread src/text-buffer.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same with this local variable. Can we call this something else like result?

@nathansobo

Copy link
Copy Markdown
Contributor

As for adding the getRange method, I see it this way:

Pros:

  • Consistency: Other things on that object are methods
  • Clarity: It's clearer that accessing the range could involve computation

Cons:

  • Duplication: We have to leave the range property for backward compatibility, so now we have 2 ways to access it on this object.
  • API churn: If we want to eliminate the duplicated property someday we would need to push people to use the method before 2.0 someday in the future.

I can get behind moving to getRange because it's nicer and more consistent, but it's not without downsides. If we're going to add it this PR should also update the API docs.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

I can get behind moving to getRange because it's nicer and more consistent, but it's not without downsides.

I agree. I'd be ok with either adding it or not adding it.

@as-cii

as-cii commented May 26, 2015

Copy link
Copy Markdown
Contributor Author

API churn is definitely a major concern for me and I agree with @nathansobo that having two ways of doing the same thing is a bit awkward. On the other hand, I dislike having a field whose behavior is a bit obscure for all the reasons I stated above.

Since we still need to keep backwards compatibility, I'll go on and simply remove getRange and introduce a setter, as suggested.

as-cii added 2 commits May 26, 2015 19:28
This guarantees full backwards compatibility for clients which reassign
`range` during the iteration (e.g. atom/find-and-replace)
@as-cii

as-cii commented May 26, 2015

Copy link
Copy Markdown
Contributor Author

I went ahead and renamed SearchResultCallback to SearchResultIteratee as well as adding a setter for range. I have tested it against find-and-replace and everything seems to work smoothly.

I'd ❤️ to have your feedback on this, especially about this last renaming operation. After that I think we can :shipit:! 🎉

@nathansobo

Copy link
Copy Markdown
Contributor

What about SearchCallbackArgument. It's super literal to the point of being a bit lame, but also very explicit.

@as-cii

as-cii commented May 26, 2015

Copy link
Copy Markdown
Contributor Author

What about SearchCallbackArgument. It's super literal to the point of being a bit lame, but also very explicit.

It's ok but I feel quite good about the concept of iteratee and I believe it fits quite well this use case. I see a callback as a fire and forget kind of a thing, whereas an iteratee implies the knowledge of what an iteration is, providing ways to control it (e.g. stopping or replacing, in this case).

However, I can change it to SearchCallbackArgument if you think it suits this object better 👍

@nathansobo

Copy link
Copy Markdown
Contributor

Is "iteratee" a word you've seen used elsewhere? It sounds a bit odd to me.

@as-cii

as-cii commented May 26, 2015

Copy link
Copy Markdown
Contributor Author

Is "iteratee" a word you've seen used elsewhere? It sounds a bit odd to me.

I think it comes from the functional programming world, but I have seen it used in underscorejs as well. On the other hand, MDN shows no result for iteratee.

At the end of the day, we'll have to maintain this and I am ok if we want to go for a more literal solution as the one you have proposed rather than introducing some programming jargon which we may not understand in the future. 👍

I have renamed it in 6625537: feel free to jump on this for further refinements 🎨

Thank you everyone for the feedback provided so far! 🙇

@nathansobo

Copy link
Copy Markdown
Contributor

@as-cii Okay I just researched "iteratee" a bit, and it's not immediately clear to me it's the same concept as this argument we're passing to the iterator. But I see your reason for naming it that as the thing being iterated over. Still, I think especially since this is a fairly formal concept from FP, perhaps we should avoid the name unless we are totally sure we have the same semantics. I agree it would be nice to name this concept though.

nathansobo pushed a commit that referenced this pull request May 26, 2015
Supply `getRange` to `scanInRange` iterator
@nathansobo
nathansobo merged commit 4683570 into master May 26, 2015
@nathansobo
nathansobo deleted the as-scan-range-computation branch May 26, 2015 19:51
@nathansobo

Copy link
Copy Markdown
Contributor

❤️ Thanks for investigating and fixing this issue!

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Great job as usual, @as-cii! This'll ship in Atom 0.202.

@as-cii

as-cii commented May 27, 2015

Copy link
Copy Markdown
Contributor Author

🎉 🎉 🎉

Thank you for your support guys 🙇

as-cii added a commit to atom/bracket-matcher that referenced this pull request May 29, 2015
…because `range` is now lazily computed (see
atom/text-buffer#64 for further information)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants