From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!z41g2000yqz.googlegroups.com!not-for-mail From: Gautier write-only Newsgroups: comp.lang.ada Subject: Performance of the Streams 'Read and 'Write Date: Thu, 29 Oct 2009 16:29:47 -0700 (PDT) Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: 85.0.115.16 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1256858987 12777 127.0.0.1 (29 Oct 2009 23:29:47 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 29 Oct 2009 23:29:47 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: z41g2000yqz.googlegroups.com; posting-host=85.0.115.16; posting-account=gRqrnQkAAAAC_02ynnhqGk1VRQlve6ZG User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.27 Safari/532.0,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8865 Date: 2009-10-29T16:29:47-07:00 List-Id: Hello. I got used to think that I/O was the last spot where our preferred language was condemned to slowness. Now consider this. Variant 1 of a buffered I/O: type Buffer is array(Natural range <>) of Unsigned_8; procedure Read( b: out Buffer ) is begin Buffer'Read(Stream(f_in), b); exception when Ada.Streams.Stream_IO.End_Error => null; -- Nothing bad, just some garbage in the buffer -- after end of compressed code end Read; procedure Write( b: in Buffer ) is begin Buffer'Write(Stream(f_out), b); end Write; Bad luck, it is as slow as doing I/O's with single bytes and Sequential_IO! But if it is slow by receiving/sending a whole buffer, how to make it faster ? Now someone (in a slightly different context) came with this (call it variant 2): procedure Read( b: out Buffer ) is use Ada.Streams; First : constant Stream_Element_Offset:= Stream_Element_Offset (b'First); Last : Stream_Element_Offset:= Stream_Element_Offset (b'Last); SE_Buffer : Stream_Element_Array (First..Last); begin Read(Stream(f_in).all, SE_Buffer, Last); for i in First..Last loop b(Natural(i)):= Unsigned_8(SE_Buffer(i)); end loop; end Read; procedure Write( b: in Buffer ) is use Ada.Streams; First : constant Stream_Element_Offset:= Stream_Element_Offset (b'First); Last : constant Stream_Element_Offset:= Stream_Element_Offset (b'Last); SE_Buffer : Stream_Element_Array (First..Last); begin for i in SE_Buffer'Range loop SE_Buffer(i):= Stream_Element(b(Natural(i))); end loop; Write(Stream(f_out).all, SE_Buffer); end Write; Naively, you would say it is even slower: you do even more by copying a buffer into another one, right ? Indeed, not at all, it is *lots* faster (on GNAT and ObjectAda)! To give an idea, the variant 1 applied to a bzip2 decompressor makes it 4x slower than the C version, and variant 2 makes it only 7% slower! With only I/O (like copying a file) you would get an even much larger difference. Now, it raises some questions: Is there maybe a reason in the RM why the 'Read and 'Write have to be that slow ? Or are these two compilers lazy when compiling these attributes ? Should I bug Adacore about that, then ? Do some other compilers do it better ? _________________________________________________________ Gautier's Ada programming -- http://sf.net/users/gdemont/ NB: For a direct answer, e-mail address on the Web site!