8000 refactor(bytesview): change bytesview back to concrete type by Yu-zh · Pull Request #2104 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor(bytesview): change bytesview back to concrete type #2104

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Yu-zh
Copy link
Collaborator
@Yu-zh Yu-zh commented May 16, 2025

The compiler does not need @bytes.View to be abstract to perform optimizations. So it's changed back.

Copy link
peter-jerry-ye-code-review bot commented May 16, 2025
Direct field access instead of function calls may improve performance

Category
Performance
Code Snippet
pub fn View::length(self : View) -> Int { self.len }
Recommendation
Consider making this an inline function with #inline annotation if performance is critical
Reasoning
While direct field access is already efficient, if this is a frequently called method, marking it as inline could help in hot paths

The struct definition comment could be more descriptive

Category
Maintainability
Code Snippet
#builtin.valtype
struct View {
bytes : Bytes
start : Int
len : Int
}
Recommendation
Add field-level documentation explaining the invariants:

#builtin.valtype
struct View {
  /// The underlying bytes buffer
  bytes : Bytes
  /// Starting offset in the buffer (>= 0)
  start : Int
  /// Length of the view (>= 0)
  len : Int
}```
**Reasoning**
Documenting field invariants helps prevent misuse and makes maintenance easier

</details>
<details>

<summary> No validation of struct field invariants during construction </summary>

**Category**
Correctness
**Code Snippet**
pub fn Bytes::op_as_view(self : Bytes, start~ : Int = 0, end? : Int) -> View {
  ...
  { bytes: self, start, len: end - start }
}
**Recommendation**
Consider adding runtime checks or making the View constructor private with a validated factory method
**Reasoning**
Invalid field values could lead to out of bounds access. While the public methods do validate, preventing invalid construction would be safer

</details>

@coveralls
Copy link
Collaborator
coveralls commented May 16, 2025

Pull Request Test Coverage Report for Build 7150

Details

  • 17 of 18 (94.44%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.002%) to 93.103%

Changes Missing Coverage Covered Lines Changed/Added Lines %
bytes/view.mbt 17 18 94.44%
Totals Coverage Status
Change from base Build 7146: -0.002%
Covered Lines: 8572
Relevant Lines: 9207

💛 - Coveralls

@Yu-zh Yu-zh marked this pull request as draft May 16, 2025 09:34
@Yu-zh Yu-zh marked this pull request as ready for review May 21, 2025 08:19
@bobzhang
Copy link
Contributor

is there any benefit changing it back? Note we still did some magic operations on View, so it may make sense to keep it abstract

@Yu-zh
Copy link
Collaborator Author
Yu-zh commented May 23, 2025

is there any benefit changing it back? Note we still did some magic operations on View, so it may make sense to keep it abstract

  • To be consistent with @string.View and @array.View since there will be no benefit to change these two types to abstract type.
  • Less hardcoded magic in the compiler, which makes it more maintainable. @bytes.View and other views are all treated as value types but they trigger different handling logic.

@Yu-zh Yu-zh force-pushed the revert-bytesview-primitive branch from db8294b to 97971fd Compare May 23, 2025 08:48
@Guest0x0
Copy link
Contributor

this can also help simplify debug info generation for bytes view in LLVM backend

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
0