Comment 4 for bug 1277690

Revision history for this message
Alastair Bridgewater (alastair-bridgewater) wrote : Re: [Bug 1277690] Re: Generic LOGTEST conses even when given fixnum arguments.

On x86-64: (sb-bignum:%bignum-ref
#x1234567890123456789012345678901234567890 0) => #x5678901234567890. For
any other platform, you'd have to mask it down from there to
positive-fixnum or similar. All backends should support %BIGNUM-REF. Good
enough?

On Thu, Dec 3, 2015 at 9:40 AM, Christophe Rhodes <
<email address hidden>> wrote:

> Christophe Rhodes <email address hidden> writes:
>
> > Lutz Euler <email address hidden> writes:
> >
> >> In case anyone wants to investigate this: There was some discussion on
> >> #sbcl about this on 2014-02-07 and 2014-02-08.
> >
> > For this bug, we have to do generic arithmetic at some point (because
> > the argument is generic). Does the problem boil down to the fact that
> > the out-of-line implementation of mask-signed-field is itself very
> > generic? If it were redefined along the lines of two-arg-and, to handle
> > small-argument cases (and maybe word-sized cases) more effectively,
> > maybe some of these performance problems would go away...
>
> Proof-of-concept patch attached. It "fixes" the logtest issue, at least
> with fixnum arguments, with a 90% speedup in the test case in this bug.
>
> It's not a complete fix. If backends had a primitive to get the low
> N-FIXNUM-BITS of a bignum, then we could use that to improve the
> (MASK-SIGNED-FIELD [1,63] <BIGNUM>) case, which would make LOGTEST not
> cons even on BIGNUM arguments -- as it is, it will do all the
> out-of-line DPB consing.
>
> Thoughts?
>
>
>
> Christophe
>
>
> ** Patch added: "0001-better-out-of-line-MASK-SIGNED-FIELD.patch"
>
> https://bugs.launchpad.net/bugs/1277690/+attachment/4529063/+files/0001-better-out-of-line-MASK-SIGNED-FIELD.patch
>
> --
> You received this bug notification because you are subscribed to SBCL.
> https://bugs.launchpad.net/bugs/1277690
>
> Title:
> Generic LOGTEST conses even when given fixnum arguments.
>
> Status in SBCL:
> Triaged
>
> Bug description:
> See this transcript (SBCL 1.1.15.33-142dc0d on Linux/x86-64):
>
> * (defun f (g)
> (let ((x 0))
> (dotimes (i 100000)
> (when (funcall g i)
> (incf x)))
> x))
>
> F
> * (let ((before (sb-ext:get-bytes-consed)))
> (f #'(lambda (i) (logtest i 1)))
> (- (sb-ext:get-bytes-consed) before))
>
> 12779520
>
> This is bad because (LOGTEST X 1) is used in ODDP which is used in
> INTEXP, so the generic ODDP and EXPT (and EVENP and surely more) on
> fixnums all cons unnecessarily.
>
> ROOM shows that the consing consists of bignums:
>
> * (room)
> […]
> 12,551,712 bytes for 647,394 bignum objects.
> […]
>
> This is a regression. (Before the regression the second expression
> above returned 0 and ROOM shows no bignums). I have bisected it to
> sbcl-1.1.8-32-ga8419eb:
>
> a8419eb994f3b59b70cfa12e1004711a830a43fa is the first bad commit
> commit a8419eb994f3b59b70cfa12e1004711a830a43fa
> Author: Paul Khuong <email address hidden>
> Date: Fri Jun 7 18:46:25 2013 -0400
>
> Complete cut-to-width for modular arithmetic
>
> For each modular argument, go through the nodes that provide its
> value and try to narrow down their bitwidth. If we fail on any and
> the result might be too wide, splice in an explicit call to LOGAND
> or MASK-SIGNED-FIELD. Skip that last step if the value is an argument
> to an equivalent LOGAND or MASK-SIGNED-FIELD.
>
> Test case by Eric Marsden.
>
> Disassembling ODDP shows a call to TWO-ARG-AND in the SBCL versions
> before
> this commit and a call to SB-C::MASK-SIGNED-FIELD from this commit on.
>
> Greetings,
>
> Lutz
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/sbcl/+bug/1277690/+subscriptions
>