Skip to content

Optimize boolean operations#527

Merged
Chemaclass merged 3 commits into
mainfrom
perf/optimize-boolean-operations
Nov 30, 2025
Merged

Optimize boolean operations#527
Chemaclass merged 3 commits into
mainfrom
perf/optimize-boolean-operations

Conversation

@Chemaclass
Copy link
Copy Markdown
Member

@Chemaclass Chemaclass commented Nov 30, 2025

📚 Description

Performance optimization for boolean operations in the assertion guard system. This PR replaces string-based boolean comparisons with integer arithmetic, which is significantly faster in Bash.

🔖 Changes

1. Replace boolean strings with integers (src/state.sh)

Changed _ASSERTION_FAILED_IN_TEST from string "true"/"false" to integer 1/0:

# Before
_ASSERTION_FAILED_IN_TEST=false

# After  
_ASSERTION_FAILED_IN_TEST=0

Why this matters: Integer comparison using arithmetic evaluation (( var )) is faster than string comparison [[ "$var" == "true" ]] because:

  • Arithmetic evaluation is a single operation
  • No string parsing or comparison overhead
  • Bash optimizes arithmetic expressions at a lower level

2. Inline the guard check in all assertion functions (src/assert.sh, src/bashunit.sh)

Replaced function calls with direct inline arithmetic checks:

# Before - function call overhead
function assert::guard() {
  if state::is_assertion_failed_in_test; then
    return 1
  fi
  return 0
}

function assert_true() {
  assert::guard || return 0
  # ...
}

# After - direct inline check
function assert_true() {
  (( _ASSERTION_FAILED_IN_TEST )) && return 0
  # ...
}

Why this matters:

  • Eliminates function call overhead (subshell/scope creation)
  • Removes nested function call (assert::guardstate::is_assertion_failed_in_test)
  • Direct variable access is faster than function indirection
  • The guard is called at the start of every assertion, so this is a hot path

3. Extract and reuse str::strip_ansi function (src/str.sh)

Created a dedicated function to strip ANSI escape codes and reused it across the codebase:

function str::strip_ansi() {
  local input="$1"
  echo -e "$input" | sed -E 's/\x1B\[[0-9;]*[mK]//g; s/[[:cntrl:]]//g'
}

This consolidates the duplicated ANSI stripping logic that was previously inline in assert_equals, assert_not_equals, and str::rpad, combining two separate operations (ANSI removal + control char removal) into a single sed command.

✅ To-do list

  • I updated the CHANGELOG.md to reflect the new feature or fix
  • I updated the documentation to reflect the changes

@Chemaclass Chemaclass added the enhancement New feature or request label Nov 30, 2025
@Chemaclass Chemaclass self-assigned this Nov 30, 2025
@Chemaclass Chemaclass added the enhancement New feature or request label Nov 30, 2025
@TypedDevs TypedDevs deleted a comment from chatgpt-codex-connector Bot Nov 30, 2025
@Chemaclass Chemaclass merged commit 78b0ffe into main Nov 30, 2025
15 checks passed
@Chemaclass Chemaclass deleted the perf/optimize-boolean-operations branch November 30, 2025 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants