Skip to content

Fix runtime optimization correctness regressions#14

Merged
loreste merged 1 commit into
loreste:mainfrom
attahn:fix/perf-correctness
Jul 22, 2026
Merged

Fix runtime optimization correctness regressions#14
loreste merged 1 commit into
loreste:mainfrom
attahn:fix/perf-correctness

Conversation

@attahn

@attahn attahn commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Fixes correctness regressions introduced by the recent runtime optimizations.

  • keeps borrowed string keys out of the map set_take path
  • restores portable task-result joins on Windows
  • removes the unused stack conversion helper and corrects the slice aliasing comment
  • adds sort coverage and an ASan regression test for map-key collisions

Verified with the Rust test suite, the sort and task-result suites on Windows, and the map-key regression under ASan.

@coderabbitai[bot] review

Summary by CodeRabbit

  • Performance

    • Improved integer-slice sorting with optimized paths for sorted, reverse-sorted, and duplicate-heavy inputs.
    • Reduced unnecessary cloning of freshly constructed map keys while preserving safe handling of borrowed keys.
    • Improved integer-to-string conversion behavior.
  • Bug Fixes

    • Improved task waiting behavior to preserve task results correctly.
    • Added coverage for map key ownership and aliasing scenarios.
  • Tests

    • Expanded sorting tests to cover boundary sizes and depth-limit fallback behavior.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3520746c-4d6b-4806-b129-b842ad1164eb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The changes add fresh string-key detection for map assignment, expand ownership and integer-sorting tests, adjust introsort depth handling, preserve task results during thread joins, and update CI and changelog entries.

Changes

Map key ownership

Layer / File(s) Summary
Fresh string key detection
src/codegen/mod.rs
Code generation identifies transferable string-key expressions and tests the owned-versus-borrowed expression cases.
Map assignment and alias validation
src/codegen/mod.rs, examples/testing/map_set_take_alias_test.mko, .github/workflows/ci.yml
Map assignments select cloning or ownership-taking setters, with tests covering borrowed keys, fresh keys, string conversion, and AddressSanitizer execution.

Integer sorting

Layer / File(s) Summary
Introsort behavior and validation
runtime/mako_rt.h, examples/testing/sort_test.mko, CHANGELOG.md
The introsort depth calculation and tests for boundary shapes, fast paths, duplicates, and fallback behavior are updated, with corresponding unreleased performance notes.

Task-thread joining

Layer / File(s) Summary
Task result preservation
runtime/mako_rt.h
Await paths use a join helper that does not overwrite task results, alongside related runtime comment and helper cleanup changes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: loreste

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main theme: fixing correctness regressions introduced by runtime optimizations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@attahn
attahn marked this pull request as ready for review July 22, 2026 09:18
@attahn

attahn commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai[bot] review

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

@attahn: I’ll review the changes in #14.

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.github/workflows/ci.yml:
- Around line 374-379: Add read-only token permissions for the ASan job
containing “Check map key ownership” by setting its permissions to contents:
read. Keep the existing ASAN_OPTIONS, MAKO_TEST_TIMEOUT_SECS, and test command
unchanged.

In `@examples/testing/map_set_take_alias_test.mko`:
- Around line 10-18: Extend TestBorrowedStringArrayKeyAndValueAreCloned with an
assertion that items[2] remains "second" after the second map assignment, while
preserving the existing assertions.

In `@runtime/mako_rt.h`:
- Around line 6408-6411: Serialize calls to mako_task_join_thread so concurrent
mako_await and mako_await_timeout_ms callers cannot invoke pthread_join on the
same task. Add or reuse a per-task mutex/state that elects one joiner, makes
other callers wait for the join to complete, and only publishes joined after the
winner finishes; preserve the existing NULL retval behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fef569eb-e44e-4683-b5bd-053a14e407aa

📥 Commits

Reviewing files that changed from the base of the PR and between ba73948 and 9a3eb94.

📒 Files selected for processing (6)
  • .github/workflows/ci.yml
  • CHANGELOG.md
  • examples/testing/map_set_take_alias_test.mko
  • examples/testing/sort_test.mko
  • runtime/mako_rt.h
  • src/codegen/mod.rs

Comment thread .github/workflows/ci.yml
Comment on lines +374 to +379
- name: Check map key ownership
env:
ASAN_OPTIONS: detect_leaks=0
MAKO_TEST_TIMEOUT_SECS: "120"
run: |
./target/release/mako test --sanitize address examples/testing/map_set_take_alias_test.mko

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the workflow around the ASan job and any permissions blocks.
file=".github/workflows/ci.yml"

echo "== workflow snippets =="
sed -n '330,420p' "$file"

echo
echo "== permissions mentions =="
grep -nE '^\s*permissions:|^\s*[A-Za-z0-9_-]+:\s*$|^\s*contents:\s*read|^\s*contents:\s*write|^\s*actions:\s*read|^\s*id-token:\s*' "$file" | sed -n '1,120p'

Repository: loreste/mako

Length of output: 4436


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '1,40p' .github/workflows/ci.yml

Repository: loreste/mako

Length of output: 1112


Restrict the ASan job token to read-only contents. This job runs PR-controlled code and currently inherits the workflow/repository default GITHUB_TOKEN permissions; add permissions: contents: read at the job or workflow level.

🧰 Tools
🪛 zizmor (1.26.1)

[warning] 343-379: overly broad permissions (excessive-permissions): default permissions used due to no permissions: block

(excessive-permissions)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In @.github/workflows/ci.yml around lines 374 - 379, Add read-only token
permissions for the ASan job containing “Check map key ownership” by setting its
permissions to contents: read. Keep the existing ASAN_OPTIONS,
MAKO_TEST_TIMEOUT_SECS, and test command unchanged.

Source: Linters/SAST tools

Comment on lines +10 to +18
fn TestBorrowedStringArrayKeyAndValueAreCloned() {
let items = ["key", "first", "second"]
let mut values = make(map[string]string)
values[items[0]] = items[1]
values[items[0]] = items[2]
assert_eq_str(values["key"], "second")
assert_eq_str(items[0], "key")
assert_eq_str(items[1], "first")
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert that the second borrowed value remains valid.

The test checks items[1] but never reads items[2] after the update assignment on Line 14. Add assert_eq_str(items[2], "second") to cover both borrowed-value assignments.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/testing/map_set_take_alias_test.mko` around lines 10 - 18, Extend
TestBorrowedStringArrayKeyAndValueAreCloned with an assertion that items[2]
remains "second" after the second map assignment, while preserving the existing
assertions.

Comment thread runtime/mako_rt.h
Comment on lines +6408 to +6411
static inline void mako_task_join_thread(MakoTask *t) {
/* Keep retval NULL: the Windows pthread shim would replace t->result with NULL. */
pthread_join(t->thread, NULL);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

# Inspect every native join and all uses of the completion flag.
rg -n -C 6 '\bpthread_join\s*\(' runtime/mako_rt.h
rg -n -C 10 '\bjoined\b' runtime/mako_rt.h

Repository: loreste/mako

Length of output: 13198


Serialize the native thread join. mako_await and mako_await_timeout_ms both read joined before storing it, so two concurrent callers can enter pthread_join on the same task. That is undefined for pthreads and can double-close the Windows shim handle; guard the join with a separate in-progress state or mutex, and let losing callers wait for the winner to publish completion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@runtime/mako_rt.h` around lines 6408 - 6411, Serialize calls to
mako_task_join_thread so concurrent mako_await and mako_await_timeout_ms callers
cannot invoke pthread_join on the same task. Add or reuse a per-task mutex/state
that elects one joiner, makes other callers wait for the join to complete, and
only publishes joined after the winner finishes; preserve the existing NULL
retval behavior.

@loreste
loreste merged commit 61fe613 into loreste:main Jul 22, 2026
12 checks passed
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.

2 participants