Skip to content

Fix accidentally quadratic algorithm #102

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

Merged
merged 2 commits into from
Mar 19, 2016
Merged

Fix accidentally quadratic algorithm #102

merged 2 commits into from
Mar 19, 2016

Conversation

SimonSapin
Copy link
Member

Tokenize::source_location takes an "position" counting UTF-8 bytes since the start of the stylesheet and returns a "location" made of a line number and column number. To avoid counting lines from the start every time, it saves some results after each call.

Previously we would only save the position of the last known line break, so calling source_location() for name positions within a single long line would keep searching from that point for the next line break and therefore take O(n²) time.

Very long lines can easily happen when a CSS minifier is used.

We now save the full (line, column) result from the last call.

See servo/servo#9897 (comment) and servo/servo#9897 (comment)

r? @jdm


This change is Review on Reviewable

@@ -409,7 +409,7 @@ fn line_numbers() {
assert_eq!(input.next_including_whitespace(), Ok(Token::Ident(Borrowed("foo"))));
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 4 });
assert_eq!(input.next_including_whitespace(), Ok(Token::WhiteSpace(" ")));
assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 5 });
// assert_eq!(input.current_source_location(), SourceLocation { line: 1, column: 5 });
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove or keep, but don't comment it out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yes, I didn’t mean to commit that. Reverted.

@jdm
Copy link
Member

jdm commented Mar 9, 2016

@bors-servo: r+
Thanks!

@bors-servo
Copy link
Contributor

📌 Commit d1eefdf has been approved by jdm

@bors-servo
Copy link
Contributor

⌛ Testing commit d1eefdf with merge 5f40718...

bors-servo pushed a commit that referenced this pull request Mar 9, 2016
Fix accidentally quadratic algorithm

`Tokenize::source_location` takes an "position" counting UTF-8 bytes since the start of the stylesheet and returns a "location" made of a line number and column number. To avoid counting lines from the start every time, it saves some results after each call.

Previously we would only save the position of the last known line break, so calling source_location() for name positions within a single long line would keep searching from that point for the next line break and therefore take O(n²) time.

Very long lines can easily happen when a CSS minifier is used.

We now save the full (line, column) result from the last call.

See servo/servo#9897 (comment) and servo/servo#9897 (comment)

r? @jdm

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/102)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

💔 Test failed - travis

@bors-servo
Copy link
Contributor

☔ The latest upstream changes (presumably #99) made this pull request unmergeable. Please resolve the merge conflicts.

Previously we would only save the position of the last known line break,
so calling source_location() for name positions within a single long line
would keep searching from that point for the next line break
and therefore take O(n²) time.

We now save the full (line, column) result from the last call.

servo/servo#9897 (comment)
servo/servo#9897 (comment)
@SimonSapin
Copy link
Member Author

Rebased.

@bors-servo r=jdm

Previous failure was ~/rust-installer/rustup.sh exiting with 1 for no apparent reason.

@bors-servo
Copy link
Contributor

📌 Commit 375ad58 has been approved by jdm

@bors-servo
Copy link
Contributor

⌛ Testing commit 375ad58 with merge 1950c75...

bors-servo pushed a commit that referenced this pull request Mar 19, 2016
Fix accidentally quadratic algorithm

`Tokenize::source_location` takes an "position" counting UTF-8 bytes since the start of the stylesheet and returns a "location" made of a line number and column number. To avoid counting lines from the start every time, it saves some results after each call.

Previously we would only save the position of the last known line break, so calling source_location() for name positions within a single long line would keep searching from that point for the next line break and therefore take O(n²) time.

Very long lines can easily happen when a CSS minifier is used.

We now save the full (line, column) result from the last call.

See servo/servo#9897 (comment) and servo/servo#9897 (comment)

r? @jdm

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/102)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

☀️ Test successful - travis

@bors-servo bors-servo merged commit 375ad58 into master Mar 19, 2016
@SimonSapin SimonSapin deleted the quad branch March 19, 2016 00:33
@SimonSapin
Copy link
Member Author

Published v0.5.5 on crates.io

@SimonSapin SimonSapin mentioned this pull request Jun 25, 2017
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.

4 participants