From: Andy Wingo <wingo@pobox.com>
To: Julian Graham <joolean@gmail.com>
Cc: guile-devel <guile-devel@gnu.org>
Subject: Re: [PATCH] Performance improvement for R6RS records
Date: Mon, 04 Oct 2010 21:09:25 +0200 [thread overview]
Message-ID: <m3y6adhj16.fsf@unquote.localdomain> (raw)
In-Reply-To: <AANLkTinMKC1x-OfKmx4gMDACWRYakhvmZX7_GsFegRt8@mail.gmail.com> (Julian Graham's message of "Mon, 4 Oct 2010 09:48:33 -0400")
Hello :)
;; First, get the set of required fields in a vtable. There should be a
;; better way for this, but there isn't.
;;
(define vtable-base-layout
(symbol->string (struct-layout (make-vtable ""))))
;; Now make the vtable for record-types. It has the base layout, plus
;; one more field to hold the list of fields in its instances.
;;
(define record-type-vtable
(let ((rtv (make-vtable (string-append vtable-base-layout "pr")
(lambda (x port)
(format port "<record-type ~a ~x>"
(struct-vtable-name x)
(object-address x))))))
(set-struct-vtable-name! rtv 'record-type)
rtv))
;; Each field of a record will be writable. Obviously here we could do
;; "pr" fields instead.
;;
;; fields := NAME...
;;
(define (fields->layout fields)
(apply symbol-append (map (lambda (x) 'pw) fields)))
;; A function to allow us to make new record types. They will be
;; instances of record-type-vtable. They themselves will be vtables.
;;
(define (make-record-type name fields)
(let ((rt (make-struct/no-tail record-type-vtable
(fields->layout fields) ; layout
record-printer ; printer
fields))) ; fields, the 'pr slot
; from above
(set-struct-vtable-name! rt name)
rt))
;; Since fields and names are stored in the vtable, we can access them
;; from a printer.
;;
(define (record-type-name rt)
(struct-vtable-name rt))
(define (record-type-fields rt)
(struct-ref rt vtable-offset-user))
(define (record-printer x port)
(define fields (record-type-fields (struct-vtable x)))
(format port "<~a ~x"
(record-type-name (struct-vtable x))
(object-address x))
(for-each (lambda (f i)
(format port " ~a: ~a" f (struct-ref x i)))
fields (iota (length fields)))
(format port ">"))
;; Record types are instances of record-type-vtable.
;;
(define (record-type? x)
(and (struct? x) (eq? (struct-vtable x) record-type-vtable)))
;; Records are instances of record types. Obviously you could inline
;; record-type? here.
;;
(define (record? x)
(and (struct? x) (record-type? (struct-vtable x))))
;; A little syntax, just to try things out.
(define-syntax define-record-type
(syntax-rules ()
((_ (name make) field ...)
(begin
(define name (make-record-type 'name '(field ...)))
(define (make field ...)
(make-struct/no-tail name field ...))))))
(define-record-type (foo make-foo)
bar baz)
(make-foo 'a 'b)
=> <foo 1ea1a40 bar: a baz: b>
Hope that helps,
Andy
--
http://wingolog.org/
next prev parent reply other threads:[~2010-10-04 19:09 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-19 0:20 [PATCH] Performance improvement for R6RS records Julian Graham
2010-09-19 10:39 ` Andy Wingo
2010-09-19 15:28 ` Julian Graham
2010-09-20 7:20 ` Andy Wingo
2010-09-27 20:05 ` Andy Wingo
2010-10-04 13:48 ` Julian Graham
2010-10-04 19:09 ` Andy Wingo [this message]
2010-10-10 5:46 ` Julian Graham
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=m3y6adhj16.fsf@unquote.localdomain \
--to=wingo@pobox.com \
--cc=guile-devel@gnu.org \
--cc=joolean@gmail.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).