8000 feat: `@bench.T::keep` by waterlens · Pull Request #1992 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: @bench.T::keep #1992

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 3 commits into from
Apr 25, 2025
Merged

feat: @bench.T::keep #1992

merged 3 commits into from
Apr 25, 2025

Conversation

waterlens
Copy link
Collaborator
@waterlens waterlens commented Apr 23, 2025

Some optimizations may eliminate the calculation that doesn't have side effects. #1974
A special function can be used to block such an optimization.

In other PLs or libraries,

Example:
from

test "batch_bench" (it : @bench.T) {
  it.bench(name="fast_fib", fn() { fast_fib(20) |> ignore })
  it.bench(name="native_fib", fn() { naive_fib(20) |> ignore })
}

to

test "batch_bench" (it : @bench.T) {
  it.bench(name="fast_fib", fn() { it.keep(fast_fib(20)) })
  it.bench(name="native_fib", fn() { it.keep(naive_fib(20)) })
}

Copy link
peter-jerry-ye-code-review bot commented Apr 23, 2025
Missing implementation of OpaqueValue trait for values

Category
Correctness
Code Snippet
pub fn keep[Any](self : T, value : Any) -> Unit {
let trait_object : &OpaqueValue = value
self.storage = trait_object
}
Recommendation
Add implementation for common types or provide a blanket implementation:

impl[T] OpaqueValue for T {}

Reasoning
The code attempts to convert Any type to &OpaqueValue but there's no visible implementation of the OpaqueValue trait. This could cause runtime errors if values don't implement the required trait.

Unclear initialization of storage field with unit value

Category
Maintainability
Code Snippet
pub fn new() -> T {
{ buffer, summaries, storage: () }
}
Recommendation
Initialize with a proper null reference or add documentation explaining the unit initialization:

{ buffer, summaries, storage: null as &OpaqueValue }

Reasoning
Using unit value () for reference initialization is unclear and could be confused with null. Should explicitly show it's a null reference if that's the intended behavior.

touch_storage function purpose is not well documented

Category
Maintainability
Code Snippet
///|
/// Suppress field never-read warning
fn touch_storage(self : T) -> Unit {
self.storage |> ignore
}
Recommendation
Improve documentation to explain when this should be called:

///|
/// Internal function to prevent compiler warnings about unused storage field.
/// Called after benchmarking to ensure kept values aren't optimized away.

Reasoning
The current documentation doesn't fully explain the function's role in the benchmarking process and when it should be called in the execution flow.

@waterlens waterlens requested a review from Guest0x0 April 23, 2025 06:54
@coveralls
Copy link
Collaborator
coveralls commented Apr 23, 2025

Pull Request Test Coverage Report for Build 6427

Details

  • 0 of 2 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 93.22%

Changes Missing Coverage Covered Lines Changed/Added Lines %
bench/bench.mbt 0 2 0.0%
Totals Coverage Status
Change from base Build 6421: -0.01%
Covered Lines: 5871
Relevant Lines: 6298

💛 - Coveralls

@hackwaly
Copy link
Contribu 8000 tor

There is no cleanup for .storage, may cause memleak.

@waterlens
Copy link
Collaborator Author
waterlens commented Apr 23, 2025

There is no cleanup for .storage, may cause memleak.

It will be recycled after @bench.T is freed, won't it? The @bench.T passing to test block doesn't have a permanent lifetime.

@waterlens waterlens force-pushed the bench-keep branch 2 times, most recently from 30df449 to 3041079 Compare April 23, 2025 10:55
@waterlens waterlens requested a review from bobzhang April 23, 2025 11:01
@Guest0x0
Copy link
Contributor

the windows CI should be fixed after next release

@waterlens waterlens merged commit ef6c605 into main Apr 25, 2025
12 checks passed
@waterlens waterlens deleted the bench-keep branch April 25, 2025 04:42
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