digitalmars.D - [your code here]
- Roumen Roupski (51/51) Jan 31 2013 // Checks if two files have equal content using memory mapped
- bearophile (8/22) Jan 31 2013 Making equals() private is not useful, but maybe it's possible to
- Peter Alexander (5/10) Jan 31 2013 D has a GC. No need for manual deletion.
- Namespace (6/16) Jan 31 2013 http://dlang.org/deprecate.html#delete
- FG (3/5) Jan 31 2013 And that's why delete is valuable (at least on 32-bit windows).
- simendsjo (2/8) Jan 31 2013 Run GC.collect() After destroy.
- Namespace (4/11) Jan 31 2013 collect? Maybe free, but collect seems a bit like to take a
- Vladimir Panteleev (11/17) Jan 31 2013 If we are talking about memory mapped files, then destroying the
- bearophile (5/6) Jan 31 2013 I have had problems comparing with this program a single pair of
- FG (3/6) Jan 31 2013 Strange. No problems here. Only had to switch from dmd32 to gdc64 with 1...
- bearophile (5/7) Jan 31 2013 How much memory is it using? What's the performance compared to
- FG (4/8) Jan 31 2013 Two identical files, 1069 MB each. Program compiled with GDC, 64-bit.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (13/17) Jan 31 2013 Throwable is a little too high in the exception hierarchy:
- Andrej Mitrovic (2/4) Jan 31 2013 Where are you extracting this information from?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/10) Jan 31 2013 I hope I haven't spread wrong information. I "learned" this from the
- Timon Gehr (3/15) Jan 31 2013 Destructors are not "guaranteed" to run. They actually do. I think this
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (27/38) Jan 31 2013 I tested this with dmd. struct destructors do get called during stack
- Maxim Fomin (18/30) Jan 31 2013 There is not much information about this topic, but I believe
// Checks if two files have equal content using memory mapped files import std.file; import std.mmfile; import std.stdio; // Compares the content of two files private bool equals(in string f1, in string f2) { if (getSize(f1) != getSize(f2)) return false; // different file sizes if (getSize(f1) == 0) return true; // zero-length files are equal MmFile m1, m2; try { m1 = new MmFile(f1); m2 = new MmFile(f2); return m1[] == m2[]; } catch (Throwable ex) { writefln("File read error: %s", ex.msg); return false; // cannot compare the files } finally { delete m1; delete m2; } } void main (string[] args) { enum NotFound = "Cannot open file: %s"; if (args.length == 3) { auto f1 = args[1], f2 = args[2]; if (!(exists(f1) && isFile(f1))) writefln(NotFound, f1); else if (!(exists(f2) && isFile(f2))) writefln(NotFound, f2); else if (equals(f1, f2)) writeln("Same files"); else writeln("Different files"); } else { writeln("Usage: filequals <file1> <file2>"); } }
Jan 31 2013
Roumen Roupski:private bool equals(in string f1, in string f2) { if (getSize(f1) != getSize(f2)) return false; // different file sizes if (getSize(f1) == 0) return true; // zero-length files are equalMaking equals() private is not useful, but maybe it's possible to make equals() nothrow.MmFile m1, m2; try { m1 = new MmFile(f1); m2 = new MmFile(f2); return m1[] == m2[]; }I have tried your little program on two files about 500 MBytes long, and the memory usage is strange. Is it doing the right thing? Bye, bearophile
Jan 31 2013
On Thursday, 31 January 2013 at 08:42:48 UTC, Roumen Roupski wrote:finally { delete m1; delete m2; }D has a GC. No need for manual deletion. (In fact, I think this is deprecated? Or scheduled for removal? Or maybe neither. Who knows).
Jan 31 2013
On Thursday, 31 January 2013 at 10:58:48 UTC, Peter Alexander wrote:On Thursday, 31 January 2013 at 08:42:48 UTC, Roumen Roupski wrote:http://dlang.org/deprecate.html#delete If you want to do something, then take destroy. AFAIK delete destroy _and_ release the memory immediately. 'destroy' doesn't.finally { delete m1; delete m2; }D has a GC. No need for manual deletion. (In fact, I think this is deprecated? Or scheduled for removal? Or maybe neither. Who knows).
Jan 31 2013
On 2013-01-31 12:38, Namespace wrote:If you want to do something, then take destroy. AFAIK delete destroy _and_ release the memory immediately. 'destroy' doesn't.And that's why delete is valuable (at least on 32-bit windows). Especially when you are comparing 500 MB files in a loop. :)
Jan 31 2013
On Thursday, 31 January 2013 at 12:28:43 UTC, FG wrote:On 2013-01-31 12:38, Namespace wrote:Run GC.collect() After destroy.If you want to do something, then take destroy. AFAIK delete destroy _and_ release the memory immediately. 'destroy' doesn't.And that's why delete is valuable (at least on 32-bit windows). Especially when you are comparing 500 MB files in a loop. :)
Jan 31 2013
On Thursday, 31 January 2013 at 12:28:43 UTC, FG wrote:On 2013-01-31 12:38, Namespace wrote:I like and use it also. ;)If you want to do something, then take destroy. AFAIK delete destroy _and_ release the memory immediately. 'destroy' doesn't.And that's why delete is valuable (at least on 32-bit windows). Especially when you are comparing 500 MB files in a loop. :)Run GC.collect() After destroy.collect? Maybe free, but collect seems a bit like to take a sledgehammer to crack a nut.
Jan 31 2013
On Thursday, 31 January 2013 at 12:28:43 UTC, FG wrote:On 2013-01-31 12:38, Namespace wrote:If we are talking about memory mapped files, then destroying the MmFile classes will close the mappings, thus freeing the virtual memory. Anyway, I'm really impressed by this example, as the kernel may be inclined to leave the pages we've already compared in RAM (might be fixed with madvise(MADV_SEQUENTIAL)), and it doesn't demonstrate many D strengths (memory-mapped files are an OS feature, for which D just provides a wrapper class). Maybe it would be more interesting if we use std.algorithm.zip or .lockstep or .equal with File.byChunk?If you want to do something, then take destroy. AFAIK delete destroy _and_ release the memory immediately. 'destroy' doesn't.And that's why delete is valuable (at least on 32-bit windows). Especially when you are comparing 500 MB files in a loop. :)
Jan 31 2013
FG:Especially when you are comparing 500 MB files in a loop. :)I have had problems comparing with this program a single pair of files that large... Bye, bearophile
Jan 31 2013
On 2013-01-31 14:21, bearophile wrote:Strange. No problems here. Only had to switch from dmd32 to gdc64 with 1GB or bigger files. Tested on win7-64.Especially when you are comparing 500 MB files in a loop. :)I have had problems comparing with this program a single pair of files that large...
Jan 31 2013
FG:Strange. No problems here. Only had to switch from dmd32 to gdc64 with 1GB or bigger files. Tested on win7-64.How much memory is it using? What's the performance compared to the diff tool? Bye, bearophile
Jan 31 2013
On 2013-01-31 15:17, bearophile wrote:FG:Two identical files, 1069 MB each. Program compiled with GDC, 64-bit. Used 6272 kB private mem / 2144 MB working set, and took 13.5 seconds. Cygwin's diff took only 1.85 s.Strange. No problems here. Only had to switch from dmd32 to gdc64 with 1GB or bigger files. Tested on win7-64.How much memory is it using? What's the performance compared to the diff tool?
Jan 31 2013
On 01/31/2013 12:42 AM, Roumen Roupski wrote:catch (Throwable ex) { writefln("File read error: %s", ex.msg); return false; // cannot compare the filesThrowable is a little too high in the exception hierarchy: Throwable / \ Error Exception / \ / \ A program should catch only Exception and its subtypes. The Error sub-hierarchy represents errors about the state of the program. The program state may be so invalid that it is not guaranteed that even writefln() or 'return' will work. For the same reason, if it is really an Error that has been thrown, even the destructors are not called during stack unwinding. Ali
Jan 31 2013
On 1/31/13, Ali =C7ehreli <acehreli yahoo.com> wrote:For the same reason, if it is really an Error that has been thrown, even the destructors are not called during stack unwinding.Where are you extracting this information from?
Jan 31 2013
On 01/31/2013 10:39 AM, Andrej Mitrovic wrote:On 1/31/13, Ali Çehreli<acehreli yahoo.com> wrote:I hope I haven't spread wrong information. I "learned" this from the discussions on this forum. Perhaps it was merely an idea and I remember it as truth. Others, is what I said correct? Why do I think that way? :) AliFor the same reason, if it is really an Error that has been thrown, even the destructors are not called during stack unwinding.Where are you extracting this information from?
Jan 31 2013
On 01/31/2013 09:43 PM, Ali Çehreli wrote:On 01/31/2013 10:39 AM, Andrej Mitrovic wrote: > On 1/31/13, Ali Çehreli<acehreli yahoo.com> wrote: >> For the same reason, if it is really an Error that has been thrown, even >> the destructors are not called during stack unwinding. > > Where are you extracting this information from? I hope I haven't spread wrong information. I "learned" this from the discussions on this forum. Perhaps it was merely an idea and I remember it as truth. Others, is what I said correct? Why do I think that way? :) AliDestructors are not "guaranteed" to run. They actually do. I think this is mostly to allow segmentation faults on Linux.
Jan 31 2013
On 01/31/2013 12:43 PM, Ali Çehreli wrote:On 01/31/2013 10:39 AM, Andrej Mitrovic wrote: > On 1/31/13, Ali Çehreli<acehreli yahoo.com> wrote: >> For the same reason, if it is really an Error that has been thrown, even >> the destructors are not called during stack unwinding. > > Where are you extracting this information from? I hope I haven't spread wrong information. I "learned" this from the discussions on this forum. Perhaps it was merely an idea and I remember it as truth. Others, is what I said correct? Why do I think that way? :)I tested this with dmd. struct destructors do get called during stack unwinding. However, a relevant quote: "In principle, only thrown objects derived from [Exception] are safe to catch inside a catch block. Thrown objects not derived from Exception represent runtime errors that should not be caught, as certain runtime guarantees may not hold, making it unsafe to continue program execution." TDPL talks about what happens (and does not happen) when a function in declared as nothrow. It also talks about why Throwable should not be caught. It doesn't say the same exact things about Error but the book draws a clear distinction between the Exception sub-hierarchy and the other exception classes. There is great information in Chapter 9 of TDPL but they are quite large to type here. Especially sections 9.2 and 9.4 are relevant. The following are my thoughts... Here is the logic behind why the destructors must not be executed when the thrown exception is an Error. AssertError is an Error, indicating that the program state is wrong. When the program state is wrong, there is no guarantee that any further operation in the program can safely be executed. Assume that the AssertError is coming from the invariant block of a struct (or assume that any other assert about the state of an object has failed). In that case the object is in a bad state. Can the destructor be called on that object? Should it be? What can we expect to happen? Ali
Jan 31 2013
On Thursday, 31 January 2013 at 20:43:37 UTC, Ali Çehreli wrote:On 01/31/2013 10:39 AM, Andrej Mitrovic wrote:There is not much information about this topic, but I believe there are two separate issues here (technical and practical): 1) Errors can behave not always like exceptions. For example, most errors (which are not thrown directly) are generated by D features: final switch throws SwitchError, notorious activity inside class dtors which calls GC causes InvalidMemoryOperationError, etc. These are typically called as OnXXError functions and are in druntime (https://github.com/D-Programming-Language/druntime/blob/master/sr /core/exception.d). Theoretically this functions may just terminate application without throwing exception, so point here is that even trying to catch Error can be useless. However if Error is thrown by D exception mechanism, I think you can handle it just like other Throwables. 2) Although you can (sometimes) catch Error, state of the program is in unpredictable condition. These conditions may depend on type of error and other factors.On 1/31/13, Ali Çehreli<acehreli yahoo.com> wrote:thrown, evenFor the same reason, if it is really an Error that has beenI hope I haven't spread wrong information. I "learned" this from the discussions on this forum. Perhaps it was merely an idea and I remember it as truth. Others, is what I said correct? Why do I think that way? :) Alithe destructors are not called during stack unwinding.Where are you extracting this information from?
Jan 31 2013