unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* SRFI-9 on top of raw structs
@ 2009-12-07 17:25 Ludovic Courtès
  2009-12-08 23:08 ` Neil Jerram
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic Courtès @ 2009-12-07 17:25 UTC (permalink / raw)
  To: guile-devel

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

Hello,

In the ‘wip-vlist’ branch (which is about implementing Bagwell’s vlists)
SRFI-9 is reimplemented in terms of raw structs, instead of records.

The advantage is that it makes it easy to write a macro-generating macro
akin to Dybvig’s ‘define-integrable’ [0] and use it to define record
accessors such that direct calls to accessors are effectively inlined.

On the attached use case, inlining reduces execution time by ~15%.  It
also has a noticeable impact on the vlist implementation itself.

If there are no objections I’ll commit it to ‘master’.

Thanks,
Ludo’.

[0] http://www.scheme.com/tspl3/syntax.html#./syntax:s57
    Thanks to Andy for pointing it out!


[-- Attachment #2: The \"bench\" --]
[-- Type: text/plain, Size: 467 bytes --]

(use-modules (srfi srfi-9)
             (ice-9 time))

(define-record-type <foo>
  (make-foo x)
  foo?
  (x get-x))

(define s (make-foo 1))

(define n 7000000)

(time (let loop ((i n))
        (and (> i 0)
             (begin
               (get-x s)
               (loop (1- i))))))

(time (let ((get-x get-x)) ;; prevent inlining
        (let loop ((i n))
          (and (> i 0)
               (begin
                 (get-x s)
                 (loop (1- i)))))))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: SRFI-9 on top of raw structs
  2009-12-07 17:25 SRFI-9 on top of raw structs Ludovic Courtès
@ 2009-12-08 23:08 ` Neil Jerram
  2009-12-09  0:04   ` Ludovic Courtès
  0 siblings, 1 reply; 4+ messages in thread
From: Neil Jerram @ 2009-12-08 23:08 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-devel

ludo@gnu.org (Ludovic Courtès) writes:

> Hello,

Hi Ludo,

> In the ‘wip-vlist’ branch (which is about implementing Bagwell’s vlists)
> SRFI-9 is reimplemented in terms of raw structs, instead of records.

Well I enjoyed a bit of reading, and I don't see any problem with these
changes, but I don't see why "it makes it easy to write a
macro-generating macro akin to Dybvig’s ‘define-integrable’".  Couldn't
that have been achieved just by rewriting the SRFI-9 define-macro as
define-syntax, but still using make-record-type etc.?

Regards,
     Neil




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: SRFI-9 on top of raw structs
  2009-12-08 23:08 ` Neil Jerram
@ 2009-12-09  0:04   ` Ludovic Courtès
  2009-12-09 23:28     ` Neil Jerram
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic Courtès @ 2009-12-09  0:04 UTC (permalink / raw)
  To: Neil Jerram; +Cc: guile-devel

Hi Neil,

Neil Jerram <neil@ossau.uklinux.net> writes:

> ludo@gnu.org (Ludovic Courtès) writes:

[...]

>> In the ‘wip-vlist’ branch (which is about implementing Bagwell’s vlists)
>> SRFI-9 is reimplemented in terms of raw structs, instead of records.
>
> Well I enjoyed a bit of reading, and I don't see any problem with these
> changes, but I don't see why "it makes it easy to write a
> macro-generating macro akin to Dybvig’s ‘define-integrable’".  Couldn't
> that have been achieved just by rewriting the SRFI-9 define-macro as
> define-syntax, but still using make-record-type etc.?

The problem with records is that accessors are defined with
‘record-accessor’, as in:

  (define module-name
    (record-accessor module-type 'name))

Here, ‘record-accessor’ returns a procedure.  Until the compiler has a
smart inliner, each ‘module-name’ call is actually a procedure call.

Conversely, the ‘define-inlineable’ macro in srfi-9.scm leads to
accessor definitions along these lines:

  (define module-name-procedure
    (lambda (x) (struct-ref x 0)))

  (define-syntax module-name
    (lambda (x)
      (syntax-case x ()
        ((_ obj)
         #'(struct-ref obj 0))
        (_
         #'module-name-procedure))))

Thus, an expression like ‘(module-name (current-module))’ is effectively
expanded to ‘(struct-ref (current-module) 0)’, whereas
‘(procedure? module-name)’ is expanded to ‘(procedure? module-name-procedure)’.

IOW, this is a poor man’s inliner.  But still, I find the approach
pretty neat.  :-)

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: SRFI-9 on top of raw structs
  2009-12-09  0:04   ` Ludovic Courtès
@ 2009-12-09 23:28     ` Neil Jerram
  0 siblings, 0 replies; 4+ messages in thread
From: Neil Jerram @ 2009-12-09 23:28 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-devel

ludo@gnu.org (Ludovic Courtès) writes:

> The problem with records is that accessors are defined with
> ‘record-accessor’, as in:
>
>   (define module-name
>     (record-accessor module-type 'name))
>
> Here, ‘record-accessor’ returns a procedure.  Until the compiler has a
> smart inliner, each ‘module-name’ call is actually a procedure call.

OK, I see now.  Thanks for explaining!

Regards,
        Neil




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-12-09 23:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-07 17:25 SRFI-9 on top of raw structs Ludovic Courtès
2009-12-08 23:08 ` Neil Jerram
2009-12-09  0:04   ` Ludovic Courtès
2009-12-09 23:28     ` Neil Jerram

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).