Repository navigation
Supply getRange to scanInRange iterator - #64
Conversation
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.
|
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 BeforeAfter🐎 🐎 🐎 When the file is larger the performance impact is even more evident 📈 |
|
Third-party packages may be using |
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: (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? 💭 |
|
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 |
|
I'd suggest moving all our packages to use I'm not sure what the cost is of calling |
|
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. |
This enables us to declare `range` as a computed property, thus allowing not to break existing clients while taking advantage of the performance improvement.
|
Thanks for your suggestions, @nathansobo and @maxbrunsfeld. Eventually, I went with an event object which defines However, as suggested by @maxbrunsfeld, I also included I think the roadmap for this should be:
What do you think? 💭 |
|
Sounds like a good plan to me! Thanks for so elegantly addressing my concerns about the API 😀 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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? 😕
Nice catch.
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 I still think we should add |
There was a problem hiding this comment.
Same with this local variable. Can we call this something else like result?
|
As for adding the Pros:
Cons:
I can get behind moving to |
I agree. I'd be ok with either adding it or not adding it. |
|
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 |
This guarantees full backwards compatibility for clients which reassign `range` during the iteration (e.g. atom/find-and-replace)
|
I went ahead and renamed I'd ❤️ to have your feedback on this, especially about this last renaming operation. After that I think we can |
|
What about |
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 |
|
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 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! 🙇 |
|
@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. |
Supply `getRange` to `scanInRange` iterator
|
❤️ Thanks for investigating and fixing this issue! |
|
Great job as usual, @as-cii! This'll ship in Atom 0.202. |
|
🎉 🎉 🎉 Thank you for your support guys 🙇 |
…because `range` is now lazily computed (see atom/text-buffer#64 for further information)


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
getRangeparameter in theiteratorfunction, which is suited to be a drop-in replacement forrange. 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.positionForCharacterIndexis rather expensive) lazily. 🐎This is still “slow”, however, because we cannot delete the
rangeparameter without first changing all the scan methods' clients to usegetRangeinstead. Thus, we need to work incrementally by shipping both versions (eagerly and lazily computedrange) 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