unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Andrew Tropin <andrew@trop.in>
To: Maxime Devos <maximedevos@telenet.be>, guile-devel@gnu.org
Subject: Re: [PATCH v2] Add atomic-box-update! function to (ice-9 atomic)
Date: Thu, 24 Aug 2023 20:19:27 +0400	[thread overview]
Message-ID: <87zg2gmn00.fsf@trop.in> (raw)
In-Reply-To: <e4abc76c-fec3-69a2-0e66-c19e4f1d69f7@telenet.be>

[-- Attachment #1: Type: text/plain, Size: 3573 bytes --]

On 2023-08-22 19:51, Maxime Devos wrote:

> Op 22-08-2023 om 12:59 schreef Andrew Tropin:
>> 
>> * module/ice-9/atomic.scm (atomic-box-update!): New variable.
>> ---
>> Changes since v1. Use single-argument proc to avoid potential perfomance
>> problems cause of call to apply.
>> 
>>   module/ice-9/atomic.scm | 14 +++++++++++++-
>>   1 file changed, 13 insertions(+), 1 deletion(-)
>> 
>> diff --git a/module/ice-9/atomic.scm b/module/ice-9/atomic.scm
>> index 2a8af901d..6bfa2e8ee 100644
>> --- a/module/ice-9/atomic.scm
>> +++ b/module/ice-9/atomic.scm
>> @@ -25,7 +25,8 @@
>>               atomic-box-ref
>>               atomic-box-set!
>>               atomic-box-swap!
>> -            atomic-box-compare-and-swap!))
>> +            atomic-box-compare-and-swap!
>> +            atomic-box-update!))
>>   
>>   (eval-when (expand load eval)
>>     (load-extension (string-append "libguile-" (effective-version))
>> @@ -36,3 +37,14 @@
>>     (add-interesting-primitive! 'atomic-box-set!)
>>     (add-interesting-primitive! 'atomic-box-swap!)
>>     (add-interesting-primitive! 'atomic-box-compare-and-swap!))
>> +
>> +(define (atomic-box-update! box proc)
>> +  "Atomically updates value of BOX to (PROC BOX-VALUE), returns new
>> +value.
>
> * , returns new value -> and returns the new value
>
> While descriptive works, for consistency with other atomics 
> documentation, this imperative procedure needs to be documented in the 
> imperative mood:
>
> Atomically update the value of BOX to (PROC BOX-VALUE) and return the 
> new value.

Went this way, thank you.

>
> Alternatively, you can adjust the other documentation of atomics to the 
> indicative mood and preserve the original docstring (except for the 
> grammar correction mentioned in the beginning).
>
>    PROC may be called multiple times, and thus PROC should be
>> +free of side effects." > +  (let loop ()
>> +    (let* ((old-value (atomic-box-ref box))
>> +           (new-value (proc old-value)))
>> +      (if (eq? old-value (atomic-box-compare-and-swap! box old-value new-value))
>> +          new-value
>> +          (loop)))))
>
> This can be optimised, by using the return value of CAS as the new old 
> value instead of calling atomic-box-ref again:
>
> (let loop ((old-value (atomic-box-ref box)))
>    (let* ((new-value (proc new-value))
>           (new-old-value (atomic-box-compare-and-swap! box old-value 
> new-value)))
>      (if (eq? new-old-value old-value)
>          new-value
>          (loop new-old-value))))

Make sense, updated the implementation.

> Maybe there is some concurrency weirdness that can cause slower 
> iterations to reduce the number of iterations (*); I'm assuming this 
> isn't the case here.  But if it is, in fact, the case here, and the goal 
> is to exploit this effect, I would think it's better to explicitly 
> implement the back-off.
>
> (I haven't benchmarked any of this, I'm purely going by the number of 
> operations.)
>
> (*) E.g., from Wikipedia: 
> https://en.wikipedia.org/w/index.php?title=Compare-and-swap&oldid=1141385700
>
> [...] Instead of immediately retrying after a CAS operation fails, 
> researchers have found that total system performance can be improved in 
> multiprocessor systems—where many threads constantly update some 
> particular shared variable—if threads that see their CAS fail use 
> exponential backoff—in other words, wait a little before retrying the 
> CAS.[4]

Sent v3.

-- 
Best regards,
Andrew Tropin

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

  reply	other threads:[~2023-08-24 16:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-19 12:20 [PATCH] Add atomic-box-update! function to (ice-9 atomic) Andrew Tropin
2023-06-21  9:06 ` Jean Abou Samra
2023-06-21  9:06   ` Jean Abou Samra
2023-06-21 16:46     ` Andrew Tropin
2023-06-21 16:54       ` Jean Abou Samra
2023-06-22  3:59         ` Andrew Tropin
2023-06-22  5:21           ` Philip McGrath
2023-06-22  9:02             ` Andrew Tropin
2023-06-22 21:42               ` Philip McGrath
2023-07-13  3:30                 ` Andrew Tropin
2023-08-22 10:51 ` Andrew Tropin
2023-08-22 10:59 ` [PATCH v2] " Andrew Tropin
2023-08-22 17:51   ` Maxime Devos
2023-08-24 16:19     ` Andrew Tropin [this message]
2023-08-24 15:38 ` [PATCH v3] " Andrew Tropin
2023-09-20  8:17 ` [PATCH v4] " Andrew Tropin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zg2gmn00.fsf@trop.in \
    --to=andrew@trop.in \
    --cc=guile-devel@gnu.org \
    --cc=maximedevos@telenet.be \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).