From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA26831; Tue, 4 Mar 2003 10:20:54 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA26343 for ; Tue, 4 Mar 2003 10:20:51 +0100 (MET) Received: from lachesis.inria.fr (lachesis.inria.fr [128.93.52.5]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h249KnT28067; Tue, 4 Mar 2003 10:20:49 +0100 (MET) Received: (from lefessan@localhost) by lachesis.inria.fr (8.12.5/8.12.5/Submit) id h248TZBn005329; Tue, 4 Mar 2003 09:29:35 +0100 From: Fabrice Le Fessant MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <15972.25582.259905.6239@lachesis.inria.fr> Date: Tue, 4 Mar 2003 09:29:34 +0100 To: "Eric C. Cooper" Cc: "caml-list@inria.fr" Subject: Re: [Caml-list] Strings as arrays or lists... References: <20030227223155.GA488@first.in-berlin.de> <20030302193437.A6487@pauillac.inria.fr> <20030304024920.GA24512@stratocaster.home> X-Mailer: VM 7.07 under Emacs 21.2.1 Reply-To: fabrice@lefessant.net X-Spam: no; 0.00; caml-list:01 char:01 datatype:01 checksum:01 marshaling:01 buffer:01 allocates:01 arrays:01 byte:01 marshal:01 bytes:02 string:03 wrote:03 recursive:03 data:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > I recently wrote some code that made use of char lists, and explode > and implode came in handy. I needed to marshal a recursive datatype > into a packet to be sent over a communication channel according to a > protocol that imposed a specific format, including a length byte at > the beginning and a checksum byte at the end. > > I could have made one pass over the data to compute the packet length, > then a second pass marshaling it into a buffer. But it was very > natural to just build up a list of bytes in a single traversal of the > datatype. Then the length and checksum could easily be added to the > beginning and the end, and the result written out. I had exactly the same code to write one year ago, and I simply built the packet inside a buffer, with a 0 length-field, changed it to a string, and filled the length-field just before sending, by changing the chars in place in the string. It only allocates the buffer once, and then the final string at the end, I cannot believe that using a list of chars can be more efficient, but sometimes strange things happen... - Fabrice ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners