Skip to content

Conversation

@mflatt
Copy link
Contributor

@mflatt mflatt commented Feb 5, 2026

The __errno convention makes a call for a foreign procedure return two values: the foreign procedure's direct result and the value of errno right after the foreign procedure returns.

On Windows, current-errno-source selects which errno is used for __errno. Also, __get_last_error is available as an alternative to __errno.

Since all backends are affected, I ran at least the foreign.ms test on all architecture+ABI combinations.

@mflatt mflatt force-pushed the errno branch 3 times, most recently from bb714f5 to 18526d8 Compare February 5, 2026 04:13
@jltaylor-us
Copy link
Contributor

#1009, #550

mflatt and others added 4 commits February 10, 2026 06:46
The `__errno` convention makes a call for a foreign procedure return
two values: the foreign procedure's direct result and the value of
`errno` right after the foreign procedure returns.

On Windows, `current-errno-source` selects which `errno` is used for
`__errno`. Also, `__get_last_error` is available as an alternative to
`__errno`.
Co-authored-by: Jamie Taylor <jltaylor-us@users.noreply.github.com>
Use `__atomic` or `__atomic __alloc` for many foreign procedures that
are used by the system, which can speed up some individual operations
by 5-10%.
The `__disable_interrupts` convention is intended mainly for
callables, where interrupts are disabled before the callable gets
control and reenabled after the callable returns.
@mflatt mflatt changed the title add __errno foreign procedure convention add __errno, _alloc, and __disable_interrupts foreign procedure conventions Feb 10, 2026
@mflatt
Copy link
Contributor Author

mflatt commented Feb 10, 2026

The latest revision includes two more commits for two more conventions:

  • __alloc modifies a preceding __atomic to indicate that the foreign function doesn't call back to Scheme, but it may call back to the Scheme C library routine to allocate Scheme values. Many functions in the Scheme kernel have this property, such as core bignum routines.
  • __disable_interrupts disables interrupts during a call or callable. It's not so useful for a call, since a with-interrupts-disabled wrapper works just as well. It's intended for use with callables, where __disable_interrupts ensures that interrupts are disabled even before reaching the callable body (where trying to call disable-interrupts is probably too late) and after leaving the callable body (where enable-interrupts to restore would be too early).

The commit to add __alloc also adds __atomic to __atomic __alloc to many foreign-procedure forms in the Chez Scheme implementation. I'll post a follow-up with details, but the effect is generally a 0% to 10% improvement for operations like = on bignums, ephemeron-cons, or random. Those annotations could use a close look to make sure they're correct. I did not try to annotate I/O foreign procedures, because they often raise exceptions.

@mflatt
Copy link
Contributor Author

mflatt commented Feb 10, 2026

Here's how I measured the performance effect of __atomic and __atomic __alloc additions to Chez Scheme's implementation, including the "before" and "after" results on my machine (Mac M2).

atomic.txt

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