Skip to content

Commit 6e80738

Browse files
committed
precommit fix
1 parent 2d7db72 commit 6e80738

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

codeflash/code_utils/time_utils.py

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,31 @@ def humanize_runtime(time_in_ns: int) -> str:
5353

5454
def format_time(nanoseconds: int) -> str:
5555
"""Format nanoseconds into a human-readable string with 3 significant digits when needed."""
56-
# Inlined significant digit check: >= 3 digits if value >= 100
56+
# Define conversion factors and units
57+
conversions = [(1_000_000_000, "s"), (1_000_000, "ms"), (1_000, "μs"), (1, "ns")]
58+
59+
# Handle nanoseconds case directly (no decimal formatting needed)
5760
if nanoseconds < 1_000:
5861
return f"{nanoseconds}ns"
59-
if nanoseconds < 1_000_000:
60-
microseconds_int = nanoseconds // 1_000
61-
if microseconds_int >= 100:
62-
return f"{microseconds_int}μs"
63-
microseconds = nanoseconds / 1_000
64-
# Format with precision: 3 significant digits
65-
if microseconds >= 100:
66-
return f"{microseconds:.0f}μs"
67-
if microseconds >= 10:
68-
return f"{microseconds:.1f}μs"
69-
return f"{microseconds:.2f}μs"
70-
if nanoseconds < 1_000_000_000:
71-
milliseconds_int = nanoseconds // 1_000_000
72-
if milliseconds_int >= 100:
73-
return f"{milliseconds_int}ms"
74-
milliseconds = nanoseconds / 1_000_000
75-
if milliseconds >= 100:
76-
return f"{milliseconds:.0f}ms"
77-
if milliseconds >= 10:
78-
return f"{milliseconds:.1f}ms"
79-
return f"{milliseconds:.2f}ms"
80-
seconds_int = nanoseconds // 1_000_000_000
81-
if seconds_int >= 100:
82-
return f"{seconds_int}s"
83-
seconds = nanoseconds / 1_000_000_000
84-
if seconds >= 100:
85-
return f"{seconds:.0f}s"
86-
if seconds >= 10:
87-
return f"{seconds:.1f}s"
88-
return f"{seconds:.2f}s"
62+
63+
# Find appropriate unit
64+
for divisor, unit in conversions:
65+
if nanoseconds >= divisor:
66+
value = nanoseconds / divisor
67+
int_value = nanoseconds // divisor
68+
69+
# Use integer formatting for values >= 100
70+
if int_value >= 100:
71+
formatted_value = str(int_value)
72+
# Format with precision for 3 significant digits
73+
elif value >= 100:
74+
formatted_value = f"{value:.0f}"
75+
elif value >= 10:
76+
formatted_value = f"{value:.1f}"
77+
else:
78+
formatted_value = f"{value:.2f}"
79+
80+
return f"{formatted_value}{unit}"
81+
82+
# This should never be reached, but included for completeness
83+
return f"{nanoseconds}ns"

codeflash/context/unused_definition_remover.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
import ast
44
from collections import defaultdict
55
from dataclasses import dataclass, field
6-
from pathlib import Path
7-
from typing import Optional
6+
from typing import TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
from pathlib import Path
10+
from typing import TYPE_CHECKING, Optional
811

912
import libcst as cst
1013

1114
from codeflash.cli_cmds.console import logger
1215
from codeflash.code_utils.code_replacer import replace_function_definitions_in_module
13-
from codeflash.models.models import CodeOptimizationContext, FunctionSource
16+
17+
if TYPE_CHECKING:
18+
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
19+
from codeflash.models.models import CodeOptimizationContext, FunctionSource
1420

1521

1622
@dataclass
@@ -493,11 +499,12 @@ def print_definitions(definitions: dict[str, UsageInfo]) -> None:
493499

494500

495501
def revert_unused_helper_functions(
496-
project_root, unused_helpers: list[FunctionSource], original_helper_code: dict[Path, str]
502+
project_root: Path, unused_helpers: list[FunctionSource], original_helper_code: dict[Path, str]
497503
) -> None:
498504
"""Revert unused helper functions back to their original definitions.
499505
500506
Args:
507+
project_root: project_root
501508
unused_helpers: List of unused helper functions to revert
502509
original_helper_code: Dictionary mapping file paths to their original code
503510
@@ -516,9 +523,6 @@ def revert_unused_helper_functions(
516523
for file_path, helpers_in_file in unused_helpers_by_file.items():
517524
if file_path in original_helper_code:
518525
try:
519-
# Read current file content
520-
current_code = file_path.read_text(encoding="utf8")
521-
522526
# Get original code for this file
523527
original_code = original_helper_code[file_path]
524528

@@ -557,7 +561,6 @@ def _analyze_imports_in_optimized_code(
557561
# Precompute a two-level dict: module_name -> func_name -> [helpers]
558562
helpers_by_file_and_func = defaultdict(dict)
559563
helpers_by_file = defaultdict(list) # preserved for "import module"
560-
helpers_append = helpers_by_file_and_func.setdefault
561564
for helper in code_context.helper_functions:
562565
jedi_type = helper.jedi_definition.type
563566
if jedi_type != "class":
@@ -606,11 +609,12 @@ def _analyze_imports_in_optimized_code(
606609

607610

608611
def detect_unused_helper_functions(
609-
function_to_optimize, code_context: CodeOptimizationContext, optimized_code: str
612+
function_to_optimize: FunctionToOptimize, code_context: CodeOptimizationContext, optimized_code: str
610613
) -> list[FunctionSource]:
611614
"""Detect helper functions that are no longer called by the optimized entrypoint function.
612615
613616
Args:
617+
function_to_optimize: The function to optimize
614618
code_context: The code optimization context containing helper functions
615619
optimized_code: The optimized code to analyze
616620
@@ -702,8 +706,9 @@ def detect_unused_helper_functions(
702706
logger.debug(f"Helper function {helper_qualified_name} is still called in optimized code")
703707
logger.debug(f" Called via: {possible_call_names.intersection(called_function_names)}")
704708

705-
return unused_helpers
709+
ret_val = unused_helpers
706710

707711
except Exception as e:
708712
logger.debug(f"Error detecting unused helper functions: {e}")
709-
return []
713+
ret_val = []
714+
return ret_val

0 commit comments

Comments
 (0)