Skip to content

Conversation

@kfly8
Copy link
Owner

@kfly8 kfly8 commented Dec 9, 2025

Problem

Previously, assert($cond, $msg) always evaluated the message expression even when the condition was true:

assert($x > 0, expensive_debug_info());
# expensive_debug_info() was ALWAYS called, even when $x > 0

This caused:

  • Unnecessary performance overhead with expensive computations in error messages
  • Unwanted side effects executing when assertions pass

Solution

Changed the implementation to use OP_OR for lazy evaluation, equivalent to:

$cond || do { croak($msg) }

Now the message expression is only evaluated when the assertion fails.

🤖 Generated with Claude Code

Previously, assert($cond, $msg) always evaluated the message expression
even when the condition was true. This caused unnecessary performance
overhead when using expensive computations in error messages.

Now the message expression is lazily evaluated using OP_OR, equivalent to:
  $cond || do { croak($msg) }

This means expensive_debug_info() in assert($x > 0, expensive_debug_info())
is NOT called when $x > 0, improving performance and preventing unwanted
side effects.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@coveralls
Copy link

Pull Request Test Coverage Report for Build 20052858643

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.3%) to 85.455%

Totals Coverage Status
Change from base Build 20030239384: -0.3%
Covered Lines: 188
Relevant Lines: 220

💛 - Coveralls

@kfly8 kfly8 merged commit d43ea13 into main Dec 9, 2025
14 checks passed
kfly8 added a commit that referenced this pull request Dec 9, 2025
Changelog diff is:

diff --git a/Changes b/Changes
index b9e7aba..6d909ea 100644
--- a/Changes
+++ b/Changes
@@ -2,6 +2,9 @@ Revision history for Perl extension Syntax-Keyword-Assert
 
 {{$NEXT}}
 
+0.18 2025-12-09T05:21:47Z
+    - Implement lazy evaluation for custom message expression #14
+
 0.17 2025-12-08T13:44:46Z
     - Add custom error message support #11
     - Fix test failures on Windows and older Perl #13
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.

3 participants