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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id E3F0E7EE51 for ; Thu, 9 May 2013 16:30:24 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of tom.j.ridge@googlemail.com) identity=pra; client-ip=209.85.192.171; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="tom.j.ridge@googlemail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of tom.j.ridge@googlemail.com designates 209.85.192.171 as permitted sender) identity=mailfrom; client-ip=209.85.192.171; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="tom.j.ridge@googlemail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-pd0-f171.google.com) identity=helo; client-ip=209.85.192.171; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="postmaster@mail-pd0-f171.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArQCAEiyi1HRVcCrlGdsb2JhbABSgz6tbpMKCBYOAQEBAQcLCwkSKoIfAQEFJxkBASwLAQ8BCgsNDSEiEgEFAQoSBgESEgmHXgEDDwygSop8hD8BBYRTChknAwqIFAaMUoYyly+BJo4qFimBXYJZOw X-IPAS-Result: ArQCAEiyi1HRVcCrlGdsb2JhbABSgz6tbpMKCBYOAQEBAQcLCwkSKoIfAQEFJxkBASwLAQ8BCgsNDSEiEgEFAQoSBgESEgmHXgEDDwygSop8hD8BBYRTChknAwqIFAaMUoYyly+BJo4qFimBXYJZOw X-IronPort-AV: E=Sophos;i="4.87,641,1363129200"; d="scan'208";a="13716101" Received: from mail-pd0-f171.google.com ([209.85.192.171]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 09 May 2013 16:30:23 +0200 Received: by mail-pd0-f171.google.com with SMTP id r11so2061320pdi.2 for ; Thu, 09 May 2013 07:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=x-received:mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=qRnHwQbRbfUMEYvDb1hqCigoVtU6JeS6EzUt8QP9Lmw=; b=SeqrE5FbnWJlj6qLs2teFDgVI7Hzox5vXmstg+rWxPSy6IL6YybfgEKwUb/j6wmn8t 8iscHe8cjJSMomEo6XWq79bsbG0jVvl595J/CBukkHMlIahKQww4N6cMgo12oDz1qD1b Yld3eCSTqqQw0PY37aIhBgDPOPQSuNBDS/6wnw6iYgJo+9LzQ1X9H1qT8aOdCpAFXNcf TWNgp3D2QyGtovw3dUKZrl2K4BjuAu9IkDmpgq+m/xWjWEU89W+AZR/nJWbpVs650/Ow LFE0ZBE43L4PKxTWGWiOp68nNrcc9uT7RpfBOcOntYgz7FU6M3elu4bc3PqKO8YjPFwb FT4Q== X-Received: by 10.66.216.136 with SMTP id oq8mr13135667pac.161.1368109821949; Thu, 09 May 2013 07:30:21 -0700 (PDT) MIME-Version: 1.0 Sender: tom.j.ridge@googlemail.com Received: by 10.70.52.66 with HTTP; Thu, 9 May 2013 07:30:00 -0700 (PDT) In-Reply-To: References: From: Tom Ridge Date: Thu, 9 May 2013 15:30:00 +0100 X-Google-Sender-Auth: XXFx_WkfDHfqL5GsKsARMXLu_BQ Message-ID: To: Anil Madhavapeddy , markus.mottl@gmail.com Cc: caml-list Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] String, Array, Bigarray.char Ah! These are probably solutions to my problem. I shall investigate further. Thanks On 9 May 2013 15:21, Anil Madhavapeddy wrote: > You should look at either Lwt or Core/Async, which both provide wrappers > over Bigarray, and asynchronous alternatives to using Unix and threads. > > Lwt_bytes: http://ocsigen.org/lwt/api/Lwt_bytes > Bigstring: https://ocaml.janestreet.com/ocaml-core/latest/doc/core/Bigstring.html > > The choice between them depends on your situation. Lwt is an add-on > library that interops with existing code well, whereas Core is a more > complete stdlib replacement (with vastly more features/datastructures). > > -anil > > On 9 May 2013, at 10:14, Tom Ridge wrote: > >> Although I see that this won't be so easy because various functions >> such as Unix.write have the buffer argument being of type string :( >> >> So at various points I seem to be forced to use strings. I suppose one >> alternative is to reimplement the functions I use (such as Unix.write) >> to work with arrays. Does anyone know if this has been done elsewhere? >> >> >> On 9 May 2013 15:07, Tom Ridge wrote: >>> Thanks for this information. >>> >>> I guess I will probably end up using arrays as much as possible. In >>> various places I have used strings as though they were immutable >>> arrays of byte. I guess the advantage of this approach is that strings >>> seem more familiar than arrays (especially Bigarrays). But it is >>> probably not much of a big deal to move to using arrays everywhere. >>> >>> Thanks once again >>> >>> Tom >>> >>> >>> On 9 May 2013 14:44, Anil Madhavapeddy wrote: >>>> On 9 May 2013, at 09:32, Tom Ridge wrote: >>>>> >>>>> Quick question: I am working a lot with arrays of byte, which at >>>>> various points I want to view as strings, and at various points I want >>>>> to view as arrays. The exact types involved should be discernible from >>>>> the code below. >>>>> >>>>> I have some conversion functions e.g.: >>>>> >>>>> type myfusebuffer = (char, Bigarray.int8_unsigned_elt, >>>>> Bigarray.c_layout) Bigarray.Array1.t >>>>> >>>>> module A = Bigarray.Array1 >>>>> >>>>> (* convenience only; don't use in production code *) >>>>> let array_of_string bs = ( >>>>> let arr = (Array.init (String.length bs) (String.get bs)) in >>>>> let contents = A.of_array Bigarray.char Bigarray.c_layout arr in >>>>> contents) >>>>> let (_:string -> myfusebuffer) = array_of_string >>>>> >>>>> This presumably takes O(n) time (where n is the length of the string >>>>> bs). My question is: is there functionality to move values between >>>>> these types at cost O(1)? Basically, I'm hoping that String is >>>>> implemented as A.of_array Bigarray.char Bigarray.c_layout or >>>>> similar... >>>> >>>> Strings are represented as normal OCaml values within the OCaml heap, >>>> whereas Bigarrays are simply pointers to externally allocated memory >>>> (via malloc). You do therefore need to copy across them in most cases. >>>> One quick solution is to define a subset of the String module that uses >>>> the Bigarray accessor functions, but this isn't ideal (especially when >>>> external libraries that use strings are involved). >>>> >>>> Your fusebuffer type probably means that you're working with filesystem >>>> data. Can you just use Bigarrays for everything, with copies to strings >>>> only when you absolutely need to? We haven't released this out of beta >>>> yet, but the cstruct camlp4 extension helps map C structures to OCaml: >>>> https://github.com/mirage/ocaml-cstruct >>>> >>>> -anil >>>> >>>> >>>> >> >> -- >> 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 >> >