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

Improve line, col calculate performance by use move cursor on Pairs Iterator. #754

Merged
merged 5 commits into from Dec 29, 2022

Conversation

huacnlee
Copy link
Member

@huacnlee huacnlee commented Dec 27, 2022

ref: #707, #560

This implementation is based by I use in AutoCorrect: https://github.com/huacnlee/autocorrect/blob/v2.5.5/autocorrect/src/result.rs#L267

Add line_col method for get (line, col) in pair

let mut pairs = JSONParser::parse(Rule::a, "....").unwrap();
let pair = pairs.next().unwrap();
let (line, col) = pair.line_col()

In this method, line, col has managed by move_cursor on Pairs iterator next method. For a large input string this method will be faster, because it does not need to re-calculate all of the input string. The line, col value has been cached in the pair and accumulate by next method.

And I keep the old line_col method in Position struct, because I found that has been used in other without Pairs iterator. This method still works, but it will be slower than the new method.

Benchmark

pair.line_col                              time:   [2.9937 µs 2.9975 µs 3.0018 µs]
position.line_col                          time:   [212.59 µs 213.38 µs 214.29 µs]
position.line_col (with fast-line-col)     time:   [18.241 µs 18.382 µs 18.655 µs]

🎊 9x fast than position.line_col with fast-line-col
🌈 100x fast than old position.line_col

Limitation

This implementation only work for forward iteration for Pairs. So iterate backward will fallback to Position::line_col method.

@huacnlee huacnlee requested a review from a team as a code owner December 27, 2022 10:29
@huacnlee huacnlee requested review from NoahTheDuke and removed request for a team December 27, 2022 10:29
@huacnlee huacnlee marked this pull request as draft December 27, 2022 10:30
@huacnlee huacnlee marked this pull request as ready for review December 27, 2022 12:51
Copy link
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

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

one alternative for testing may be a different test parser definition, plus check that position and pair results match ?

btw is there a need for fast-line-col with this, or should that feature be removed? @huacnlee @NoahTheDuke

pest/src/iterators/pairs.rs Outdated Show resolved Hide resolved
pest/src/position.rs Outdated Show resolved Hide resolved
@huacnlee
Copy link
Member Author

We still need fast-line-col, because my implementation only work for call from Pairs.

There have many case use Position in directly.

@huacnlee huacnlee marked this pull request as draft December 29, 2022 03:46
@huacnlee huacnlee force-pushed the improve-line-col branch 2 times, most recently from ee0f1fc to a06a837 Compare December 29, 2022 07:06
@huacnlee huacnlee marked this pull request as ready for review December 29, 2022 08:20
@huacnlee
Copy link
Member Author

Hi @tomtau

I have done all, please review again.

@tomtau tomtau merged commit 79746cf into pest-parser:master Dec 29, 2022
@huacnlee huacnlee deleted the improve-line-col branch January 31, 2023 11:28
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.

None yet

3 participants