optimize CharStream class in liblangutil
summary:
- replaced unnecessary copying of strings with string views and move semantics where appropriate.
- used std::min and std::max instead of ternary operators for readability.
- avoided redundant string operations such as
substrandfind, especially within loops. - ensured const correctness by marking appropriate member functions as const.
details:
advanceAndGet: refactored to return early when reaching the end of input and simplified return statement.rollback: no changes, but added comments for clarity.setPosition: added assertion to ensure the position is within bounds of the source.lineAtPosition: simplified logic for finding the line and end position.translatePositionToLineColumn: simplified logic for calculating line number and line start position.text: added additional conditions to check for valid location and source name.singleLineSnippet: no changes, but added comments for clarity.translateLineColumnToPosition: simplified logic for finding the position corresponding to a given line and column.
test plan:
- ran existing unit tests to ensure functionality is preserved. Unit Testing in C++
- compiled and tested the code with sample inputs to verify optimizations.
Thank you for your contribution to the Solidity compiler! A team member will follow up shortly.
If you haven't read our contributing guidelines and our review checklist before, please do it now, this makes the reviewing process and accepting your contribution smoother.
If you have any questions or need our help, feel free to post them in the PR or talk to us directly on the #solidity-dev channel on Matrix.
Hi @moroii69. Unfortunately, I'll be closing the PR as it doesn't actually optimize much, and we're generally not fans of using ternary expressions so frequently as it detracts from the code readability. In addition, this isn't really a bottleneck that would require sacrificing readability for no actual performance gain.
For future reference however, note that we use tabs instead of spaces, which is why the check_style step of the build failed. Also, the build fails on all of our unix-like (Linux, MacOS) platforms, i.e. it does not compile.