Skip to content

Add missing type restrictions to benchmark directory #15688

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 2 commits into
base: master
Choose a base branch
from

Conversation

Vici37
Copy link
Contributor

@Vici37 Vici37 commented Apr 20, 2025

This is the output of compiling cr-source-typer and running it with the below incantation:

CRYSTAL_PATH="./src" ./typer spec/std_spec.cr --error-trace --exclude src/crystal/ --stats --progress --union-size-threshold 2 --ignore-private-defs src/benchmark

This is related to #15682 .

@@ -44,13 +44,13 @@ module Benchmark
end

# Reports a single benchmark unit.
def report(label = " ", &block)
def report(label : String = " ", &block) : Nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was actually leaking @reports below, I manually set this to Nil as I don't think that was intended.

@@ -44,13 +44,13 @@ module Benchmark
end

# Reports a single benchmark unit.
def report(label = " ", &block)
def report(label : String = " ", &block) : Nil
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: We could also add a restriction to the block parameter.

Suggested change
def report(label : String = " ", &block) : Nil
def report(label : String = " ", &block : ->) : Nil

@@ -31,7 +31,7 @@ module Benchmark
end

# Adds code to be benchmarked
def report(label = "", &action) : Benchmark::IPS::Entry
def report(label : String = "", &action) : Benchmark::IPS::Entry
Copy link
Member

Choose a reason for hiding this comment

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

ditto:

Suggested change
def report(label : String = "", &action) : Benchmark::IPS::Entry
def report(label : String = "", &action : ->) : Benchmark::IPS::Entry

@cycles = (iterations / duration.total_milliseconds * 100).to_i
@cycles = 1 if cycles <= 0
end

def calculate_stats(samples) : Nil
def calculate_stats(samples : Array(Float64) | Array(Int32)) : Nil
Copy link
Member

Choose a reason for hiding this comment

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

thought: This looks like an arbitrary choice of two numeric types for the array element type. Theoretically, any number type should be acceptable there. But that's impossible to express in the current type grammar. We could only use Array as a generic type.

Btw. Array(Int32) actually comes from a unit test for this method. When using the higher-level API, the type is always Array(Float64).

This method (and set_cycles as well) also looks very much like a helper method and does not seem very useful as a public method. Probably the entire Entry type is just an implementation detail and should not even be exposed in the API docs.

I'd suggest leaving these questionable out for now, and undocument Entry in a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants