From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andy Wingo Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH] Performance improvement for R6RS records Date: Mon, 04 Oct 2010 21:09:25 +0200 Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1286220420 23673 80.91.229.12 (4 Oct 2010 19:27:00 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 4 Oct 2010 19:27:00 +0000 (UTC) Cc: guile-devel To: Julian Graham Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Oct 04 21:26:59 2010 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1P2qgc-0000M6-Jr for guile-devel@m.gmane.org; Mon, 04 Oct 2010 21:26:58 +0200 Original-Received: from localhost ([127.0.0.1]:42631 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P2qgb-0006fw-OJ for guile-devel@m.gmane.org; Mon, 04 Oct 2010 15:26:57 -0400 Original-Received: from [140.186.70.92] (port=45025 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P2qTS-00070F-QE for guile-devel@gnu.org; Mon, 04 Oct 2010 15:13:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P2qMp-0001Gh-V1 for guile-devel@gnu.org; Mon, 04 Oct 2010 15:06:33 -0400 Original-Received: from a-pb-sasl-quonix.pobox.com ([208.72.237.25]:43664 helo=sasl.smtp.pobox.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P2qMp-000191-Ru for guile-devel@gnu.org; Mon, 04 Oct 2010 15:06:31 -0400 Original-Received: from sasl.smtp.pobox.com (unknown [127.0.0.1]) by a-pb-sasl-quonix.pobox.com (Postfix) with ESMTP id 98402DBC9B; Mon, 4 Oct 2010 15:06:03 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=sasl; bh=x1vIWDe46TT0+CBuOx4ksXOmNVc=; b=v5lDYc x7aQJaE94jca+2HNBfngFaU2AZhyAD4dWrQkIzhA6OGHfXD4EqbzXh5AgtwJFYfH W3k+FRQFfQ2WTKOKmL1NV7tsBFClNGH80/BUIjfyAOE8fy8HeYB9XuW8o1XVJf/s GrvnwXwP0dsSi5K0t0WNnpg4M8vMuIWhh4GhE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; q=dns; s=sasl; b=YQ2rHcuSO8RYBb0s7mmL/Os1jHOT+w/o QhvTZT+tbPSJtdku+/q5mL5tu9xmQUnW7oW7THIsp3MDqOQzXoHlI8DSMheGGUMU zHq3+H4E+uMm7id5U72foD0o2+WwGHJnI4WGCnZbv5IM36O37K3/zFhtlE0a70zK K04Iv2OQz5E= Original-Received: from a-pb-sasl-quonix. (unknown [127.0.0.1]) by a-pb-sasl-quonix.pobox.com (Postfix) with ESMTP id 7DC65DBC97; Mon, 4 Oct 2010 15:06:02 -0400 (EDT) Original-Received: from unquote.localdomain (unknown [81.38.187.246]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by a-pb-sasl-quonix.pobox.com (Postfix) with ESMTPSA id 9BC24DBC8B; Mon, 4 Oct 2010 15:06:00 -0400 (EDT) In-Reply-To: (Julian Graham's message of "Mon, 4 Oct 2010 09:48:33 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) X-Pobox-Relay-ID: 6E54EDCC-CFEA-11DF-BCDB-030CEE7EF46B-02397024!a-pb-sasl-quonix.pobox.com X-detected-operating-system: by eggs.gnu.org: Solaris 10 (beta) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:10996 Archived-At: 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 "" (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) => Hope that helps, Andy -- http://wingolog.org/