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

Optimize DupN code generation & fix testing bug #623

Merged
merged 6 commits into from
Dec 23, 2022
Merged

Conversation

jasonpaulos
Copy link
Contributor

@jasonpaulos jasonpaulos commented Dec 23, 2022

Optimize code generated by the DupN expression.

Prior to this PR, that expression always used the dupn op. However, if only 1 additional value is needed, the dup op can be used to save a byte. And if no additional values are needed and you still use DupN, no additional op is needed.

While testing this change, I noticed a bug in the assert_teal_as_expected function from tests/compile_asserts.py. This function used to only check that the expected line of code prefixes the actual line, and unfortunately dup prefixes dupn 1. I decided to upgrade the comparison function by using the difflib package and processing each line to strip away all auxiliary information.

@jasonpaulos
Copy link
Contributor Author

I believe the fact that 538f784 passes tests is a bug in our test code. I'm investigating further

@jasonpaulos jasonpaulos changed the title Optimize DupN code generation Optimize DupN code generation & fix testing bug Dec 23, 2022
options
)
return dupn_srt, dupn_end
return DupN(self.auto_instance, self.count - 1).__teal__(options)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -1,4 +1,5 @@
from pathlib import Path
from difflib import unified_diff
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

current_quote: str | None = None
for i, char in enumerate(line):
for qt in quote_types:
if char == qt:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually this probably needs to take into account escaped quotes, e.g. \"

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure the purpose of processing each line, versus just keeping the raw original. IMO, for this test code it's okay (even preferrable?) to fail because the generated teal is non-identical, even if it's semantically equivalent. I think we would want to know when this happens, and in that case we could decide to simply replace the old fixtures with the new ones.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I remember now. When I originally wrote this test, I had some hand-commented code which I didn't want to fail the diff. I think at this point, the usage has grown much larger than I originally anticipated. So the comments I just wrote, convince me that it's better to not try and pre-process. I.e., don't ignore comments. If we agree that this approach is reasonable, I'm more than happy to replace the original fixtures with fixtures which will pass the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that it's preferable to fail if the generated code is different (otherwise we shouldn't be checking in any code for this).

Some checked in code is augmented with comments, so a direct line by line comparison will fail, even though the ops are the same. This processing function is responsible for stripping away the comments plus any difference in whitespace.

I haven't looked in detail at all the code, but my impression is we'd want to keep the additional user-generated comments if at all possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, looks like we replied at the same time. If you're ok with getting rid of the comments, I'm happy to make that change to simplify this comparison code

Copy link
Contributor

@tzaffi tzaffi Dec 23, 2022

Choose a reason for hiding this comment

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

Yeah for sure. Those original old fixtures don't need their hand crafted comments anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 27f530e

Copy link
Contributor

@tzaffi tzaffi left a comment

Choose a reason for hiding this comment

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

I agree with the approach to replace dupn with dup or nothing whenever possible. I also support bringing in the more standard united_diff() into compile_asserts.py vs. the previous ad-hoc diffing function.

I left a question regarding processing of the lines from Teal file fixtures, vs. just reading them raw.

Copy link
Contributor

@tzaffi tzaffi left a comment

Choose a reason for hiding this comment

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

Looks great!

@jasonpaulos jasonpaulos merged commit 721532f into master Dec 23, 2022
@jasonpaulos jasonpaulos deleted the optimize-dupn branch December 23, 2022 21:20
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.

2 participants