From: Alexey Nogin <nogin@cs.cornell.edu>
To: Wolfram Kahl <kahl@diogenes.informatik.unibw-muenchen.de>
Cc: caml-list@inria.fr
Subject: Re: List.filter in Ocaml 2.02
Date: Fri, 12 Mar 1999 13:18:16 -0500 [thread overview]
Message-ID: <36E95A68.5DA46AA2@CS.Cornell.EDU> (raw)
In-Reply-To: <19990312101049.6512.qmail@diogenes.informatik.unibw-muenchen.de>
Wolfram Kahl wrote:
> Alexey Nogin <nogin@cs.cornell.edu> writes:
>
> > The filter function implementation does not seem to be too efficient.
> > I did some testing once and it turned out that the most efficient
> > (for my applications) way to write the filter function was:
> >
> > let rec filter f = function
> > [] -> []
> > | (h::t) as l ->
> > if f h then
> > let rem = filter f t in
> > if rem == t then l else h::rem
> > else
> > filter f t
> >
> > The main gain here is that we do not allocate any new memory for sublist
> > (or the whole list) that does not change as a result of the filtering.
>
> The intended sharing here is not fully explicit, but partially implicit.
> If this works as described, then it should not make a difference from:
>
> let rec filter f = function
> [] as l -> l
> | ...
>
> , where the sharing is now fully explicit.
> The fact that this is reported to work anyway, implies
> that the compiler shares these common subexpressions ``[]'',
> and this gets me asking:
>
> How far does this kind of common subexpression sharing extend?
> Does it work for user-defined datatypes, too?
> Does it work only for zero-ary constructors, or are some
> more complicated constructions recognised, too?
As far as I understand it, for unboxed values such as integers and zero-ary
constants (such as []) in user-defined datatypes == and = are equivalent. It
has nothing to do with the fact that they are common subexpressions - if you
write let x = [] in some module and let y = [] in another, it will still be
the case that x == y.
> P.S.: Does it work for ``filter f'', or is it useful to write
> (as I often do):
>
> > let filter f =
> > let rec f1 = function
> > [] -> []
> > | (h::t) as l ->
> > if f h then
> > let rem = f1 t in
> > if rem == t then l else h::rem
> > else
> > f1 t
> > in f1
This will allocate memory for the closure which is contrary to the main goal -
not allocating anything unless really necessary and not allocate anything at
all when list does not change.
> Will filter be expanded for short constant lists at compile time in
> any way?
I do not think so.
> Or will e.g. List.fold_right or List.fold_left
> (known to be primitively recursive at compile-time of user modules :-)
> be expanded for short constant lists at compile time
> by the inlining mechanism?
As far as I know, recursive functions are never inlined.
Alexey
--------------------------------------------------------------
Home Page: http://www.cs.cornell.edu/nogin/
E-Mail: nogin@cs.cornell.edu (office), ayn2@cornell.edu (home)
Office: Upson 4139, tel: 1-607-255-4934
ICQ #: 24708107 (office), 24678341 (home)
next prev parent reply other threads:[~1999-03-15 13:29 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <Xavier.Leroy@inria.fr>
1999-03-05 10:41 ` Objective Caml 2.02 Xavier Leroy
1999-03-05 13:34 ` Camlp4 2.02 Daniel de Rauglaudre
1999-03-05 15:11 ` Objective Caml 2.02 Pierpaolo Bernardi
1999-03-05 19:59 ` doligez
1999-03-11 3:06 ` Upgrade from OCaml 2.01 to OCaml 2.02 made things _slower_! Alexey Nogin
1999-03-11 9:44 ` Xavier Leroy
1999-03-11 23:59 ` Alexey Nogin
1999-03-13 13:40 ` Anton Moscal
1999-03-24 4:20 ` Alexey Nogin
1999-03-26 11:49 ` Anton Moscal
1999-04-06 2:06 ` Alexey Nogin
1999-04-06 7:53 ` Xavier Leroy
1999-03-11 23:42 ` List.filter in Ocaml 2.02 Alexey Nogin
1999-03-12 10:10 ` Wolfram Kahl
1999-03-12 18:18 ` Alexey Nogin [this message]
1999-03-13 2:43 ` David Monniaux
1999-03-12 17:01 ` Jean-Francois Monin
1999-03-12 18:41 ` Alexey Nogin
[not found] ` <199903121011.LAA27611@lsun565.lannion.cnet.fr>
1999-03-12 18:37 ` Alexey Nogin
1999-03-15 9:06 ` Jean-Francois Monin
1999-03-06 0:27 Sort.array easily degenerates Markus Mottl
1999-03-09 10:44 ` Xavier Leroy
1999-03-09 23:03 ` doligez
1999-03-10 13:58 ` Xavier Leroy
1999-03-10 0:28 ` Markus Mottl
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=36E95A68.5DA46AA2@CS.Cornell.EDU \
--to=nogin@cs.cornell.edu \
--cc=caml-list@inria.fr \
--cc=kahl@diogenes.informatik.unibw-muenchen.de \
/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