From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id DCF268239C for ; Mon, 15 Jan 2018 11:52:08 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=leandro@ostera.io; spf=None smtp.mailfrom=leandro@ostera.io; spf=None smtp.helo=postmaster@mail-yw0-f200.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of leandro@ostera.io) identity=pra; client-ip=209.85.161.200; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="leandro@ostera.io"; x-sender="leandro@ostera.io"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of leandro@ostera.io) identity=mailfrom; client-ip=209.85.161.200; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="leandro@ostera.io"; x-sender="leandro@ostera.io"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-yw0-f200.google.com) identity=helo; client-ip=209.85.161.200; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="leandro@ostera.io"; x-sender="postmaster@mail-yw0-f200.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AMqSwexM3E0EtzFcfOm4l6mtUPXoX/o7sNwtQ0KIM?= =?us-ascii?q?zox0LfX4rarrMEGX3/hxlliBBdydt6odzbKO+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2aFLduGC94iAPERvjKwV1?= =?us-ascii?q?Ov71GonPhMiryuy+4ZLebxlViDanfb9+MAi9oBnMuMURnYZsMLs6xAHTontPde?= =?us-ascii?q?RWxGdoKkyWkh3h+Mq+/4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TibZM5mwGRd?= =?us-ascii?q?CeqRDrQ53BTrjzfUjqJ9J7ONqCoDqZ/53Z504L+LzExgxXlPF82Yllq1YSR0k2?= =?us-ascii?q?cPHWFk2al+pQls1Q/G3/Ig27pXEttc4/4PWQA/Z8bR?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0BbAQAYh1xahsihVdFdHAEBAQQBAQoBA?= =?us-ascii?q?YQndCeEE4E5l0iCAoJnlXwDXAojhRgChE1CFQEBAQEBAQEBAQESAQEBCAsLCCg?= =?us-ascii?q?vgjgMgmABAgIBIx0BASwLAQQLCQIEBxodAgIiEgEFAQoSBhMSigkDDQgQjWKRH?= =?us-ascii?q?UCLJW6CJ4MKAQEFhCUDgnoBAQEBAQUBAQEBAQEBARgIEoQqghWBV4Fpgy6DJAs?= =?us-ascii?q?EghyCaoJliGkMmnSIDI0/c4INkRCNPolTFCWBFw8mgXMyGggkDD0yBoFyCYI8H?= =?us-ascii?q?4FzQTcBjDoBAQE?= X-IPAS-Result: =?us-ascii?q?A0BbAQAYh1xahsihVdFdHAEBAQQBAQoBAYQndCeEE4E5l0i?= =?us-ascii?q?CAoJnlXwDXAojhRgChE1CFQEBAQEBAQEBAQESAQEBCAsLCCgvgjgMgmABAgIBI?= =?us-ascii?q?x0BASwLAQQLCQIEBxodAgIiEgEFAQoSBhMSigkDDQgQjWKRHUCLJW6CJ4MKAQE?= =?us-ascii?q?FhCUDgnoBAQEBAQUBAQEBAQEBARgIEoQqghWBV4Fpgy6DJAsEghyCaoJliGkMm?= =?us-ascii?q?nSIDI0/c4INkRCNPolTFCWBFw8mgXMyGggkDD0yBoFyCYI8H4FzQTcBjDoBAQE?= X-IronPort-AV: E=Sophos;i="5.46,363,1511823600"; d="scan'208,217";a="309080797" Received: from mail-yw0-f200.google.com ([209.85.161.200]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 15 Jan 2018 11:52:07 +0100 Received: by mail-yw0-f200.google.com with SMTP id n70so7548325ywd.20 for ; Mon, 15 Jan 2018 02:52:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ostera-io.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=lmDtqm7qngfAgVqzcQgXqiGLJfT7AG7Nn/y/Cb9e0Dg=; b=17vPYsztJwwUqrrEO8Fvdm0qP9yfFl7czhB1dIx/4Q+0kdhvmN9OPsIIxlBJKv9ACB x+izitbC+7zjj1ZR1ogqfx4RfnAOQiBOlILu39n8LdapnaeqHAJmDvOB4Ya8EuKDJD5s sPbZ9ChyKmi2aWfDseGaE+2ahFH3RFVLxxCYTVIFJk6VYje/UYp90byrzI8StoBKrp3t OHEg0HBSHQw88pd1ZzW/HFC4/LSJvufSYkAnVv7P1HFtx2P2oJhJPd/SFWzVth/7dx7d nBmxT39l3VvQGWs4KGzJ+MdfvrVnSUp6RfHL6+yztNGUR/DOq33C8erjXstTjp1qmgJQ duVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=lmDtqm7qngfAgVqzcQgXqiGLJfT7AG7Nn/y/Cb9e0Dg=; b=fmNZ0B+9nMD94eR9qEFP/DzvQ8dQkGIh2VmHT+yHNn+ie0ukf0D+YPHPrAHALHrqi2 ojw6Ze3zQDAE1A666Zo4nXEJTLCevXDggtej3VKysihj5fl7mD/qRQX/R2gzUDGKSHLn RRZv1UQCVNTquJpBs4FZ/9005rafFKJESFqXPAGVQPD8qogLBadUikiGwrZUSwl5D+96 d00rgBjCNy1zfNSsxbs+C16a9KCYQ47W+R4oHk4GOqCFtWLrkBeojoEWxOZn9z0BRggy tlagFX3XxYSAKjcjvyEKBQM81E+jVYqbg7g1y4fPnI6Zv6fdbJEunj+wcQVFYTdhzvEq 27qA== X-Gm-Message-State: AKwxyteS0/U5jOEsGCGgoyqFKV0KznC0ONr4skHaKg7okwJtbYRRgLy9 w9Vc3JWK54kLDBQos/36HMTr2DbHva/uas+QmkXmPQ== X-Google-Smtp-Source: ACJfBouBLogtvti7FxJU2m+h4gvQpRcaKqH0orKigkPGQzF9C0fgyf9jpMtsIng24XXuqbmblXTVlpwwadrqJG8bulw= X-Received: by 10.37.91.214 with SMTP id p205mr5703641ybb.386.1516013526158; Mon, 15 Jan 2018 02:52:06 -0800 (PST) MIME-Version: 1.0 References: <1515782196.604079108@f381.i.mail.ru> <937c674a-bf63-b2be-44eb-9ad331aa622f@ocamlpro.com> <1516012497.2168791.1235601456.5B7510E5@webmail.messagingengine.com> In-Reply-To: <1516012497.2168791.1235601456.5B7510E5@webmail.messagingengine.com> From: Leandro Ostera Date: Mon, 15 Jan 2018 10:51:55 +0000 Message-ID: To: Leo White Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary="001a1142622eb3b19f0562ce6736" Subject: Re: [Caml-list] flambda for bytecode backend --001a1142622eb3b19f0562ce6736 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable You could also use the binding mechanism that js functions have to swap the context they are ran in, although I=E2=80=99m not sure if it=E2=80=99s goin= g to be faster than the two options already provided above. function f_unbound(x) { return (x + this.y); } const env =3D { y: 1 }; const f =3D f_unbound.bind(env); f(1) // 2 m=C3=A5n 15 jan. 2018 kl. 11:35 skrev Leo White : > > The best we could do (without doing some magic) would look like: > > > > let f x =3D > > let y =3D x + 1 in > > let g z =3D z + y in > > g > > > > compiled to: > > > > function f_code(x, env) { > > let y =3D x + 1; > > function g_code(y, env) { > > return(z + env.y); > > } > > let g =3D { code : g_code, env : { y: y }; > > return(g); > > } > > > > let f =3D { code : f_code, env : { } } > > Pierre is right about the reason we don't use flambda with > bytecode, although I don't think that is the best JavaScript we > could produce. > > It would not be to difficult to undo closure conversion within > the function body itself. This still leaves references to the > closure outside of the function body. These aren't shown in > Pierre's example but they are produced during inlining and are > the main thing that flambda uses which bytecode doesn't > support. For these we can take advantage of the fact that > JavaScript functions are also ordinary objects, attaching the > environment to the function as fields: > > function f(x) { > let y =3D x + 1; > function g(z) { > return(z + y); > } > g.y =3D y; > return(g); > } > > Of course, this could still have an adverse effect on how the JIT > of different browsers deals with these functions, so some > benchmarking would be needed to confirm the viability of this > approach. > > Regards, > > Leo > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs --001a1142622eb3b19f0562ce6736 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable You could also use the binding mechanism that js functions have to swap the= context they are ran in, although I=E2=80=99m not sure if it=E2=80=99s goi= ng to be faster than the two options already provided above.

functio= n f_unbound(x) { return (x + this.y); }

const env =3D { y: 1 };
c= onst f =3D f_unbound.bind(env);
f(1) // 2
=
m=C3=A5n 15 jan. 2018 kl. 11:35 skrev Leo White <leo@lpw25.net>:
> The best we could do (without doing some magic) would l= ook like:
>
> let f x =3D
> =C2=A0 let y =3D x + 1 in
> =C2=A0 let g z =3D z + y in
> =C2=A0 g
>
> compiled to:
>
> function f_code(x, env) {
> =C2=A0 let y =3D x + 1;
> =C2=A0 function g_code(y, env) {
> =C2=A0=C2=A0=C2=A0 return(z + env.y);
> =C2=A0 }
> =C2=A0 let g =3D { code : g_code, env : { y: y };
> =C2=A0 return(g);
> }
>
> let f =3D { code : f_code, env : { } }

Pierre is right about the reason we don't use flambda with
bytecode, although I don't think that is the best JavaScript we
could produce.

It would not be to difficult to undo closure conversion within
the function body itself. This still leaves references to the
closure outside of the function body.=C2=A0 These aren't shown in
Pierre's example but they are produced during inlining and are
the main thing that flambda uses which bytecode doesn't
support. For these we can take advantage of the fact that
JavaScript functions are also ordinary objects, attaching the
environment to the function as fields:

=C2=A0 =C2=A0 function f(x) {
=C2=A0 =C2=A0 =C2=A0 let y =3D x + 1;
=C2=A0 =C2=A0 =C2=A0 function g(z) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return(z + y);
=C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 g.y =3D y;
=C2=A0 =C2=A0 =C2=A0 return(g);
=C2=A0 =C2=A0 }

Of course, this could still have an adverse effect on how the JIT
of different browsers deals with these functions, so some
benchmarking would be needed to confirm the viability of this
approach.

Regards,

Leo

--
Caml-list mailing list.=C2=A0 Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocam= l_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
--001a1142622eb3b19f0562ce6736--