Re: [racket-users] Racket BC versus CS unsafe flonums

2020-11-06 Thread Matthew Flatt
At Fri, 6 Nov 2020 20:14:51 +0100, Dominik Pantůček wrote:
> I assume that CS' unsafe-flvector-set! is actually pretty safe when it
> comes to flonum-convertible numbers. It might be a bit faster because it
> lacks the contract, but definitely it is not a "low level" variant.

Hm, right --- it's implemented by `bytevector-ieee-double-native-set!`,
which is defined to convert its last argument to a flonum (so the
conversion applies even in unsafe mode). I hadn't noticed that before,
so thanks for pointing it out!

I will investigate faster option. A primitive without conversion could
make the safe `flvector-set!` slightly faster, too, by avoiding a
redundant check.

> Also given the fact that unsafe-fl+ and others give no performance
> advantage

I think `unsafe-fl+` can still be faster. For example, the second loop
below is the unsafe one, and it's 10-20% faster. I constructed this
program carefully, though, to avoid flonum inference and to avoid
allocating flonum results (which swamps the cost of checks).

The performance guide is pretty conservative about suggesting
performance improvements via unsafe operations, already, but
suggestions are welcome.

Mathew



#lang racket/base
(require racket/unsafe/ops
 racket/flonum)

(define N 1)

(define (f x)
  (fl< (fl+ x x) 0.0))

(define (u-f x)
  (fl< (unsafe-fl+ x x) 0.0))

(set! f f)
(set! u-f u-f)

(collect-garbage)

(time
 (for/fold ([v #f]) ([i (in-range N)])
   (f 1.0)))

(collect-garbage)

(time
 (for/fold ([v #f]) ([i (in-range N)])
   (u-f 1.0)))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/20201106124546.1c8%40sirmail.smtps.cs.utah.edu.


[racket-users] Racket BC versus CS unsafe flonums

2020-11-06 Thread Dominik Pantůček
Hello Racketeers,

going through some more possible optimizations of my side projects, I
encountered a different behavior of unsafe-flvector-set! between BC and
CS implementations:

 CUT HERE 
#lang racket

(require racket/unsafe/ops
 racket/flonum)

(define flv (make-flvector 10))
(for ((i (in-range 10)))
  (unsafe-flvector-set! flv i i))
(for ((i (in-range 10)))
  (display (~a #:width 4 (unsafe-flvector-ref flv i
(newline)
 CUT HERE 

$ racket test-flvector.rkt
SIGSEGV MAPERR si_code 1 fault on addr 0x9
Aborted (core dumped)
$ racketcs test-flvector.rkt
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
$

The SIGSEGV is expected behavior (although might not get triggered every
time). The latter looks like implicit fx->fl operation.

Changing to safe flvector-set! results in expected contract violation
with both variants:

$ racketcs test-flvector.rkt
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
$ racket test-flvector.rkt
flvector-set!: contract violation
  expected: flonum?
  given: 0
  argument position: 3rd
  other arguments...:
   (flvector 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0)
   0
  context...:
   "/home/joe/Projects/Programming/TD4/test-flvector.rkt": [running body]
   temp35_0
   for-loop
   run-module-instance!
   perform-require!
$ racketcs test-flvector.rkt
flvector-set!: contract violation
  expected: flonum?
  given: 0
  context...:

/home/joe/Projects/Programming/racket-lang/racket/racket/collects/racket/private/for.rkt:1534:9
   body of "/home/joe/Projects/Programming/TD4/test-flvector.rkt"
$

I assume that CS' unsafe-flvector-set! is actually pretty safe when it
comes to flonum-convertible numbers. It might be a bit faster because it
lacks the contract, but definitely it is not a "low level" variant.

Also given the fact that unsafe-fl+ and others give no performance
advantage (the opposite is true for well-written algorithms), maybe the
performance guide and unsafe ops documentation should get an update
before CS becomes the default implementation.

Any thoughts?


Cheers,
Dominik

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/c39293d0-51fa-41d8-3e0e-1fd99bca59ae%40trustica.cz.


Re: [racket-users] cross-module `error` changed?

2020-11-06 Thread Sorawee Porncharoenwase
This is the same problem as https://github.com/racket/racket/issues/3355.
It's already been fixed in Racket 7.9.

On Fri, Nov 6, 2020 at 5:30 AM Shriram Krishnamurthi 
wrote:

> Has something changed in how `error` works across modules? This code that
> I used last year without trouble seems to run into trouble now.
>
> Here's the code all in one module:
>
> #lang racket
>
> (provide yield resume)
>
> (define resumer #f)
>
> (define (yield)
>   (let/cc k
> (set! resumer k)
> (error 'yield "yielding computation")))
>
> (define (resume)
>   (resumer 'dummy))
>
> (define (fact n)
>   (if (= n 0)
>   1
>   (begin
> (when (= (remainder n 10) 0)
>   (yield))
> (* n (fact (- n 1))
>
> It results in the following (desired) interaction:
>
> > (fact 10)
> . . yield: yielding computation
> > (resume)
> 3628800
>
> But now I'm going to remove the definition of `fact` and put it in another
> module, saving the remainder as `yielder.rkt`:
>
> #lang racket
>
> (require "yielder.rkt")
>
> (define (fact n)
>   (if (= n 0)
>   1
>   (begin
> (when (= (remainder n 10) 0)
>   (yield))
> (* n (fact (- n 1))
>
> Now I get the following error. What should I be doing instead?
>
> > (fact 10)
> -- #(struct:exn:fail:contract "vector-ref: contract violation\n
> expected: vector?\n  given: '((error 'yield \"yielding computation\")
> # 10 4 113 37)\n  argument position:
> 1st\n  other arguments...:\n   0" #)
>   (errortrace-stack-item->srcloc . #(struct:srcloc
> # v7.8/share/pkgs/drracket/drracket/private/stack-checkpoint.rkt> 168 0 6297
> 203))
>   (pick-first-defs . #(struct:srcloc # v7.8/share/pkgs/drracket/drracket/private/stack-checkpoint.rkt> 331 0 13000
> 425))
>   (get-exn-source-locs . #(struct:srcloc # v7.8/share/pkgs/drracket/drracket/private/stack-checkpoint.rkt> 585 0 23184
> 391))
>   (#f . #(struct:srcloc # v7.8/collects/racket/contract/private/arrow-val-first.rkt> 486 18 20735 32))
>   (error-display-handler/stacktrace . #(struct:srcloc
> # v7.8/share/pkgs/drracket/drracket/private/debug.rkt> 362 2 15076 2612))
>   (call-with-exception-handler . #(struct:srcloc
> #
> 266 2 9251 256))
>   (fact . #(struct:srcloc # 5 0 40 139))
>   (eval-one-top . #f)
>   (call-with-exception-handler . #(struct:srcloc
> #
> 266 2 9251 256))
>   (loop . #(struct:srcloc # v7.8/share/pkgs/drracket/drracket/private/rep.rkt> 1210 24 50804 979))
>   (call-with-break-parameterization . #(struct:srcloc
> #
> 148 2 4909 517))
>   (#f . #(struct:srcloc # v7.8/share/pkgs/drracket/drracket/private/rep.rkt> 1180 9 49153 5062))
>   (#f . #(struct:srcloc # v7.8/share/pkgs/drracket/drracket/private/rep.rkt> 1493 15 64385 1548))
>   (#f . #(struct:srcloc # v7.8/share/pkgs/gui-lib/mred/private/wx/common/queue.rkt> 435 6 19067 1056))
>   (#f . #(struct:srcloc # v7.8/share/pkgs/gui-lib/mred/private/wx/common/queue.rkt> 486 32 21054 120))
>   (call-with-break-parameterization . #(struct:srcloc
> #
> 148 2 4909 517))
>   (eventspace-handler-thread-proc . #(struct:srcloc
> # v7.8/share/pkgs/gui-lib/mred/private/wx/common/queue.rkt> 370 11 16515 690))
> exception raised by error display handler: vector-ref: contract violation
>   expected: vector?
>   given: '((error 'yield "yielding computation")
> # 10 4 113 37)
>   argument position: 1st
>   other arguments...:
>0; original exception raised: yield: yielding computation
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/6aa1ad8a-9f1a-4c11-8b75-5ed3ca09797dn%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcueguC-yYNcqx13JA_zqEopn_fzUX%2BzFPq_L3NnDvgK1xMNA%40mail.gmail.com.


[racket-users] cross-module `error` changed?

2020-11-06 Thread Shriram Krishnamurthi
Has something changed in how `error` works across modules? This code that I 
used last year without trouble seems to run into trouble now.

Here's the code all in one module:

#lang racket

(provide yield resume)

(define resumer #f)

(define (yield)
  (let/cc k
(set! resumer k)
(error 'yield "yielding computation")))

(define (resume)
  (resumer 'dummy))

(define (fact n)
  (if (= n 0)
  1
  (begin
(when (= (remainder n 10) 0)
  (yield))
(* n (fact (- n 1))

It results in the following (desired) interaction:

> (fact 10)
. . yield: yielding computation
> (resume)
3628800

But now I'm going to remove the definition of `fact` and put it in another 
module, saving the remainder as `yielder.rkt`:

#lang racket

(require "yielder.rkt")

(define (fact n)
  (if (= n 0)
  1
  (begin
(when (= (remainder n 10) 0)
  (yield))
(* n (fact (- n 1))

Now I get the following error. What should I be doing instead?

> (fact 10)
-- #(struct:exn:fail:contract "vector-ref: contract violation\n  expected: 
vector?\n  given: '((error 'yield \"yielding computation\") 
# 10 4 113 37)\n  argument position: 
1st\n  other arguments...:\n   0" #)
  (errortrace-stack-item->srcloc . #(struct:srcloc 
# 168 0 6297 
203))
  (pick-first-defs . #(struct:srcloc # 331 0 13000 
425))
  (get-exn-source-locs . #(struct:srcloc # 585 0 23184 
391))
  (#f . #(struct:srcloc # 486 18 20735 32))
  (error-display-handler/stacktrace . #(struct:srcloc 
# 362 2 15076 2612))
  (call-with-exception-handler . #(struct:srcloc 
# 
266 2 9251 256))
  (fact . #(struct:srcloc # 5 0 40 139))
  (eval-one-top . #f)
  (call-with-exception-handler . #(struct:srcloc 
# 
266 2 9251 256))
  (loop . #(struct:srcloc # 1210 24 50804 979))
  (call-with-break-parameterization . #(struct:srcloc 
# 
148 2 4909 517))
  (#f . #(struct:srcloc # 1180 9 49153 5062))
  (#f . #(struct:srcloc # 1493 15 64385 1548))
  (#f . #(struct:srcloc # 435 6 19067 1056))
  (#f . #(struct:srcloc # 486 32 21054 120))
  (call-with-break-parameterization . #(struct:srcloc 
# 
148 2 4909 517))
  (eventspace-handler-thread-proc . #(struct:srcloc 
# 370 11 16515 690))
exception raised by error display handler: vector-ref: contract violation
  expected: vector?
  given: '((error 'yield "yielding computation") 
# 10 4 113 37)
  argument position: 1st
  other arguments...:
   0; original exception raised: yield: yielding computation

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/6aa1ad8a-9f1a-4c11-8b75-5ed3ca09797dn%40googlegroups.com.