From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id F3F02BC69 for ; Wed, 15 Aug 2007 17:29:57 +0200 (CEST) Received: from ptb-relay01.plus.net (ptb-relay01.plus.net [212.159.14.212]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l7FFTv5U019212 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Wed, 15 Aug 2007 17:29:57 +0200 Received: from [80.229.56.224] (helo=beast.local) by ptb-relay01.plus.net with esmtp (Exim) id 1ILKoj-0001CS-M6; Wed, 15 Aug 2007 16:29:54 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: Joel Reymont Subject: Re: [Caml-list] High-performance bytecode interpreter in OCaml Date: Wed, 15 Aug 2007 16:18:37 +0100 User-Agent: KMail/1.9.7 References: <2B9157A5-ECA0-46EE-B628-840689224ACC@gmail.com> <200708151401.41091.jon@ffconsultancy.com> In-Reply-To: Cc: caml-list@yquem.inria.fr MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200708151618.38165.jon@ffconsultancy.com> X-Miltered: at discorde with ID 46C31BF5.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; bytecode:01 ocaml:01 bytecode:01 compiler:01 ocaml:01 translating:01 byte:01 workload:01 lowercase:01 lowercase:01 frog:98 closures:01 wrote:01 rec:01 rec:01 On Wednesday 15 August 2007 14:20:34 Joel wrote: > I have an existing bytecode file that I need to execute. The bytecode > is produced by some other compiler. Does this change anything? Yes, that rules out most of the solutions I proposed. :-) > I'm not sure if a term-level interpreter or rewriter applies in this > scenario. Indeed. If string operations turn out to be slow in OCaml and the CPS way of doing things is too obfuscated, you might try translating the byte array into an instruction array with a more OCaml-friendly notion of instruction (i.e. a variant type!). This would be particularly preferable if you think there are any simplifying transformations that you can do to reduce your workload. > As for CPS, what I meant is implementing each bytecode instruction as > a function that takes a continuation (next instruction?). That should certainly work. Make sure you hoist out as much computation as possible from the continuation closures that you generate. For example, make sure you change this: let rec compile bytecode = match bytecode with | 0x83::x::t -> (fun k -> print_string (String.lowercase (string_of_int x)); k(compile t)) into this: let rec compile bytecode = match bytecode with | 0x83::x::t -> let x = String.lowercase (string_of_int x) in let t = compile t in (fun k -> print_string x; k t) This allows you to precompute as much as possible while you build up the continuation that will evaluate the program. In particular, make sure you hoist out any lookups, such as jump labels. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. OCaml for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists/?e