unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: Maxime Devos <maximedevos@telenet.be>
Cc: guile-devel@gnu.org,  Andy Wingo <wingo@pobox.com>
Subject: Re: [PATCH] Add 'bytevector-slice'.
Date: Fri, 13 Jan 2023 12:32:50 +0100	[thread overview]
Message-ID: <87358eekkd.fsf@gnu.org> (raw)
In-Reply-To: <57a2d974-0278-409c-678a-6daf7672ba81@telenet.be> (Maxime Devos's message of "Thu, 12 Jan 2023 01:10:57 +0100")

Hi,

Maxime Devos <maximedevos@telenet.be> skribis:

> The only thing missing for me, is a procedure
> 'bytevector-slice/read-only' and 'bytevector-slice/write-only', then I
> could throw the module implementing the wrapping in Scheme-GNUnet and
> the overhead incurred by wrapping away.

I did consider the read-only part, but it’s not really implementable
currently as SCM_F_BYTEVECTOR_IMMUTABLE flag is even ignored by
instructions, and fixing it is far beyond the scope of this patch:

  https://issues.guix.gnu.org/60779

> On 11-01-2023 16:00, Ludovic Courtès wrote:

[...]

>> +(use-modules (rnrs bytevectors)
>> +             (rnrs bytevectors gnu))
>
> I thought that R6RS reserved (rnrs ...) to the RnRS process, so I
> would think that naming it (rnrs bytevectors gnu) would be
> standards-incompliant, though I cannot find in the specification, so
> maybe it isn't actually reserved.
>
> (SRFI looks a bit looser to me, so I find the (srfi ... gnu)
> acceptable, but (rnrs ... gnu) looks weird to me, I would propose
> (ice-9 bytevector-extensions) or such.).

I’ll stick to gnu.scm because that’s the convention we’ve used for
extensions so far, and “gnu” is arguably “reserved”.

>> +Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
>
>
> Where does this year 2022 come from?  Does a previous version of this
> patch predate the new year?

I started working on it in December and my ‘write-file-hooks’ updated it.

>> +  c_offset = scm_to_size_t (offset);
>> +
>> +  if (SCM_UNBNDP (size))
>> +    {
>> +      if (c_offset < SCM_BYTEVECTOR_LENGTH (bv))
>> +        c_size = SCM_BYTEVECTOR_LENGTH (bv) - c_offset;
>> +      else
>> +        c_size = 0; > +    }
>> +  else
>> +    c_size = scm_to_size_t (size);
>> +
>> +  if (c_offset + c_size > SCM_BYTEVECTOR_LENGTH (bv))
>> +    scm_out_of_range (FUNC_NAME, offset);
>
>
> If offset=SIZE_MAX-1 and size=1, this will overflow to 0 and hence not
> trigger the error reporting.  This bounds check needs to be rewritten,
> with corresponding additional tests.

OK.

> IIUC, if you use bytevector-slice iteratively, say:
>
> (let loop ((bv some-initial-value)
>            (n large-number))
>   (if (> n 0)
>       (loop (bytevector-slice bv 0 (bytevector-length bv))
>             (- n 1))
>       bv))
>
> you will end up with a bytevector containing a reference to a
> bytevector containing a reference to ... containing a reference to the
> original reference, in a chain of length ≃ large-number.

The ‘parent’ word is here just so the backing memory isn’t reclaimed
while the slice is still alive.

Whether there’s a long chain of ‘parent’ links or not makes no
difference to GC performance nor to memory usage.

> Do you know what this 'parent' field even is for?  Going by some
> comments in 'libguile/bytevectors.c', it is for GC reasons, but going
> by the existence of the 'bytevector->pointer' + 'pointer->bytevector'
> trick which destroys 'parent' information, it seems unnecessary to me.

Like I wrote, it was introduced to keep backing memory alive, generally.
With ‘pointer->bytevector’, we want to make sure the original pointer
remains live as long as the bytevector is live (pointer objects can have
a finalizer, and that finalizer must not run while the bytevector is
live).

> Nowadays a https://.../ instead of a mail address, is more
> conventional and useful, I'd think.  Its even used in old files,
> e.g. libguile/r6rs-ports.c.

Noted.

> A test is missing for the case where the size is unaligned instead of
> the offset.

Noted.

I’ll come up with a second version taking this into account.

Thanks!

Ludo’.



  reply	other threads:[~2023-01-13 11:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-11 15:00 [PATCH] Add 'bytevector-slice' Ludovic Courtès
2023-01-11 15:21 ` Thompson, David
2023-01-11 15:29 ` Jean Abou Samra
2023-01-11 17:34   ` Ludovic Courtès
2023-01-11 17:37     ` [EXT] " Thompson, David
2023-01-11 19:05       ` [EXT] " lloda
2023-01-12 22:27         ` Ludovic Courtès
2023-01-13  9:30           ` lloda
2023-01-11 17:39 ` The mysterious ‘SCM_F_BYTEVECTOR_CONTIGUOUS’ Ludovic Courtès
2023-01-11 18:51   ` lloda
2023-01-11 19:00     ` lloda
2023-01-12  0:10 ` [PATCH] Add 'bytevector-slice' Maxime Devos
2023-01-13 11:32   ` Ludovic Courtès [this message]
2023-01-13 11:56     ` Maxime Devos
2023-01-13 23:48       ` Ludovic Courtès
2023-01-14 14:45         ` Maxime Devos
2023-01-14 15:19   ` Ludovic Courtès

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=87358eekkd.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=guile-devel@gnu.org \
    --cc=maximedevos@telenet.be \
    --cc=wingo@pobox.com \
    /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).