* memory costs of storing lambdas?
@ 2003-04-30 11:06 Han-Wen Nienhuys
2003-05-01 9:36 ` Mikael Djurfeldt
0 siblings, 1 reply; 3+ messages in thread
From: Han-Wen Nienhuys @ 2003-04-30 11:06 UTC (permalink / raw)
(please CC replies, I'm not subscribed to guile-user)
Hi there,
I have a small question: I was wondering how much memory it costs to
store an anonymous function. I have to make choices between storing
various entities in lilypond either as a function, or as an
expression.
The following fictional example illustrates the distinction (#
introduces in-line Scheme in Lilypond syntax):
1. using anon functions:
(define (style-setter style)
(lambda (head) (set-note-head-style head style)))
...
\property Voice.NoteHead \set #'style-procedure
= #(style-setter "cross")
During the lilypond run, the anonymous function is called. Storage
cost: one anonymous function
2. using expressions
\property Voice.NoteHead \set #'style-expression
= #(list set-note-head-style "cross")
During the lilypond-run, a note-head pointer is prepended to the
argument list, and the list (set-note-head-style head style) is
evaluated.
Storage cost: a list of length 2.
I have the feeling that solution 1. is more expensive in terms of
memory than solution 2. My question: is this true, and how could I
have found out this answer myself? (i.e. where is the related
interpreter code?)
Thanks,
Han-Wen
--
Han-Wen Nienhuys | hanwen@cs.uu.nl | http://www.cs.uu.nl/~hanwen
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: memory costs of storing lambdas?
2003-04-30 11:06 memory costs of storing lambdas? Han-Wen Nienhuys
@ 2003-05-01 9:36 ` Mikael Djurfeldt
2003-05-01 11:38 ` memory costs of storing lambdas? (correction) Mikael Djurfeldt
0 siblings, 1 reply; 3+ messages in thread
From: Mikael Djurfeldt @ 2003-05-01 9:36 UTC (permalink / raw)
Cc: guile-user
Han-Wen Nienhuys <hanwen@cs.uu.nl> writes:
> 1. using anon functions:
>
> (define (style-setter style)
> (lambda (head) (set-note-head-style head style)))
> ...
>
> \property Voice.NoteHead \set #'style-procedure
> = #(style-setter "cross")
>
> During the lilypond run, the anonymous function is called. Storage
> cost: one anonymous function
According to eval.c:scm_closure and procs.h:
closure = < closcar, env >
closcar = < code, properties >
code = < formals, body >
So, the extra cost (not counting the cost of the list of expressions
in `body') is 3 pairs + pairs in formals list.
In your case, you also surround the closure with an environment
captured by the closure. This environment has the structure
(explained in the Guile docs):
new-env = < frame, env >
frame = < formals, values >
formals = < 'style, '() >
values = < style-value, '() >
So, you have 4 pairs for the environment, 3 pairs for closure
overhead, 1 pair for formals list, 1 pair for list of expressions in
body, and, 3 pairs for the expression. So, in total, the object
returned by `style-setter' above will consume 12 pairs.
> During the lilypond-run, a note-head pointer is prepended to the
> argument list, and the list (set-note-head-style head style) is
> evaluated.
>
> Storage cost: a list of length 2.
If you have only two items to store, the most efficient data structure
available is one pair.
For more items, the most efficient structure is a GOOPS object or a
vector. The cost of ordinary (not applicable) GOOPS objects is one
pair + mallocated memory of size sizeof(ptr) * n where n is number of
items.
Best regards,
Mikael
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: memory costs of storing lambdas? (correction)
2003-05-01 9:36 ` Mikael Djurfeldt
@ 2003-05-01 11:38 ` Mikael Djurfeldt
0 siblings, 0 replies; 3+ messages in thread
From: Mikael Djurfeldt @ 2003-05-01 11:38 UTC (permalink / raw)
Cc: djurfeldt
Mikael Djurfeldt <djurfeldt@nada.kth.se> writes:
> In your case, you also surround the closure with an environment
> captured by the closure. This environment has the structure
> (explained in the Guile docs):
>
> new-env = < frame, env >
> frame = < formals, values >
> formals = < 'style, '() >
> values = < style-value, '() >
>
> So, you have 4 pairs for the environment
Oops. The `formals' pair is constant, so it doesn't add to the cost
of new environments.
> So, in total, the object returned by `style-setter' above will
> consume 12
Rather 11, then.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-05-01 11:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-04-30 11:06 memory costs of storing lambdas? Han-Wen Nienhuys
2003-05-01 9:36 ` Mikael Djurfeldt
2003-05-01 11:38 ` memory costs of storing lambdas? (correction) Mikael Djurfeldt
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).