On 1 June 2010 16:37, Mike Caron <[email protected]> wrote: > On 01/06/2010 12:25 AM, Ralph Versteegen wrote: >> >> On 1 June 2010 16:15, Mike Caron<[email protected]> wrote: >>> >>> On 01/06/2010 12:10 AM, Ralph Versteegen wrote: >>>> >>>> On 1 June 2010 15:57, Mike Caron<[email protected]> wrote: >>>>> >>>>> On 31/05/2010 11:44 PM, Ralph Versteegen wrote: >>>>>> >>>>>> On 31 May 2010 11:58, Ralph Versteegen<[email protected]> wrote: >>>>>>> >>>>>>> On 31 May 2010 10:20, Mike Caron<[email protected]> wrote: >>>>>>>> >>>>>>>> On 30/05/2010 6:01 PM, Ralph Versteegen wrote: >>>>>>>>> >>>>>>>>> On 31 May 2010 08:57, Mike Caron<[email protected]> >>>>>>>>> wrote: >>>>>>>>>> >>>>>>>>>> D:\ohrrpgce>dir gentest.xml >>>>>>>>>> 30/05/2010 04:52 AM 237,804,960 gentest.xml >>>>>>>>>> >>>>>>>>>> D:\ohrrpgce>xml2reload gentest.xml gentest.rld >>>>>>>>>> Loaded XML document in 20857 ms >>>>>>>>>> Memory usage: 301 >>>>>>>>>> Parsed XML document in 17049 ms >>>>>>>>>> Memory usage: 573142499 >>>>>>>>>> Freed XML document in 35378 ms >>>>>>>>>> Optimised document in 31149 ms >>>>>>>>>> Memory usage: 428328275 >>>>>>>>>> Serialized document in 8557 ms >>>>>>>>>> Tore down memory in 173 ms >>>>>>>>>> Finished in 116510 ms >>>>>>>>>> >>>>>>>>>> D:\ohrrpgce>dir gentest.rld >>>>>>>>>> 30/05/2010 04:31 PM 203,271,865 gentest.rld >>>>>>>>>> >>>>>>>>>> And, here's a profile of CPU and Memory usage while doing the >>>>>>>>>> above: >>>>>>>>>> >>>>>>>>>> http://taleotc.com/Reloadprofile.png >>>>>>>>>> >>>>>>>>>> Note that on the CPU graph, Green is Usermode, while Red is Kernel >>>>>>>>>> mode. >>>>>>>>>> And >>>>>>>>>> that my CPU thinks it's dual core, so you should consider the 50% >>>>>>>>>> mark >>>>>>>>>> to >>>>>>>>>> be >>>>>>>>>> "max". >>>>>>>>> >>>>>>>>> Hyperthreading? >>>>>>>> >>>>>>>> Aka, "Make applications think the CPU can multiple things at once, >>>>>>>> when >>>>>>>> in >>>>>>>> reality it can't!". I guess "Hyperthreading" is more marketable, >>>>>>>> though. >>>>>>>> :) >>>>>>>> >>>>>>>>>> Further, you should care more about the "Commit" memory, rather >>>>>>>>>> than >>>>>>>>>> th >>>>>>>>>> "Physical" memory. >>>>>>>>>> -- >>>>>>>>>> Mike >>>>>>>>> >>>>>>>>> Yikes! Nearly twice as long to free as to load the XML document. >>>>>>>>> Looks >>>>>>>>> like the memory allocator thrashed a lot while freeing memory, >>>>>>>>> which >>>>>>>>> is odd because I would have expected xml nodes to be deleted in >>>>>>>>> about >>>>>>>>> the same order they were created. Maybe libxml deletes attribute >>>>>>>>> nodes >>>>>>>>> after deleting children. (Yes! I checked, and my hunch is correct!) >>>>>>>>> >>>>>>>>> I assume that 3.0GB marks the total virtual memory committed on the >>>>>>>>> machine, and xml2relaod used roughly 1.5GB? >>>>>>>> >>>>>>>> Yes, that's correct. >>>>>>>> >>>>>>>>> What does that xml file look like? It's interesting that the >>>>>>>>> resulting >>>>>>>>> .reld is nearly the same size as the original! That 64MB test case >>>>>>>>> compressed to half the size. >>>>>>>> >>>>>>>> I used this program to generate a document: >>>>>>>> >>>>>>>> http://www.xml-benchmark.org/downloads.html >>>>>>>> >>>>>>>> It seems to have a lot of content in the form of text nodes. Alas, I >>>>>>>> can't >>>>>>>> find the place I got the original document, since it was basically >>>>>>>> more >>>>>>>> of >>>>>>>> the same. >>>>>>>> >>>>>>>> I suppose we could just cat the 64-meg document to itself, and do a >>>>>>>> tiny >>>>>>>> bit >>>>>>>> of editing :D >>>>>>>> >>>>>>>>> Serialisation could be a bit faster (you can always increase the >>>>>>>>> buffer size to speed it up, going from 32kb to 64kb caused about a >>>>>>>>> 20% >>>>>>>>> improvement), but mostly I'm happy with that. >>>>>>>> >>>>>>>> Honestly, that's the least of my concerns. That's always going to >>>>>>>> scale >>>>>>>> linearly with the resulting document size. >>>>>>>> >>>>>>>> I'm more concerned, now, with the thing we haven't really been >>>>>>>> profiling: >>>>>>>> Loading these stupidly large documents again. I modified reloadtest >>>>>>>> to >>>>>>>> load >>>>>>>> it, and it ain't pretty: >>>>>>> >>>>>>> Well, I told you to profile them, and have been doing so myself; who >>>>>>> cares how long it takes to convert an XML? >>>>>>> >>>>>>> And speaking of profiling, I tried profiling but got totally useless >>>>>>> results missing nearly all running time. >>>>>>> >>>>>>>> Test #19: loadHugeDocument... Pass >>>>>>>> Took 50 s >>>>>>>> Test #20: freeHugeDocument... Pass >>>>>>>> Took 297 ms >>>>>>>> >>>>>>>> And, just for completion's sake, here's the 64 Meg-document's >>>>>>>> RELOAD: >>>>>>>> >>>>>>>> Test #19: loadHugeDocument... Pass >>>>>>>> Took 19 s >>>>>>>> Test #20: freeHugeDocument... Pass >>>>>>>> Took 53 ms >>>>>>>> >>>>>>>> Ouch. >>>>>>> >>>>>>> What the devil? Here are my times, after warming up of course, on >>>>>>> windows XP this time, with a private heap: >>>>>>> >>>>>>>> reload2reload medline.reld medline2.reld >>>>>>> >>>>>>> Loaded document in 12743 ms >>>>>>> Serialized document in 1062 ms >>>>>>> Tore down memory in 17 ms >>>>>>> Finished in 13824 ms >>>>>>> >>>>>>> But!!!!! here's what it was in linux (back then the buffer was >>>>>>> smaller, I think now the serialization time is about 2.5s): >>>>>>> >>>>>>> bash-3.1$ time reload2reload medline2.reld medline2_.reld >>>>>>> Loaded document in 3724 ms >>>>>>> Serialized document in 3506 ms >>>>>>> Tore down memory in 597 ms >>>>>>> Finished in 7838 ms >>>>>>> >>>>>>> real 0m7.892s >>>>>>> user 0m5.276s >>>>>>> sys 0m0.440s >>>>>>> >>>>>>> So, it looks like Linux is being way too paranoid about flushing the >>>>>>> file to disk as its written, or something like that (there are so >>>>>>> many >>>>>>> options in the kernel for tuning file systems; looks like they're not >>>>>>> set for performance in this case on my machine), and conversely >>>>>>> Windows is doing something REALLY stupid when loading it in. I mean, >>>>>>> the reload file is read in a linear manner, what on earth could the >>>>>>> problem be? Argh! We may have to write a file buffering layer for >>>>>>> reading files as well! Or maybe the problem is in allocating nodes? >>>>>>> We could test this by loading the file into memory before attempting >>>>>>> to load it. >>>>>> >>>>>> OK, I tested and confirmed that file reads are the problem. I loaded >>>>>> the whole file into memory and then read from that ("inmem"). Then I >>>>>> also added an arena allocator, which allocates 2MB at a time and doles >>>>>> it out, and used it to allocate memory for new Nodes ("arena"). >>>>>> Finally I tried allocating strings for string nodes with the arena as >>>>>> well ("strings"), which we couldn't do in practice: >>>>>> >>>>>> medline.reld load time (each warmed up with several runs): >>>>>> >>>>>> Windows/NTFS, private heap: >>>>>> Initial (current SVN): 12743 ms >>>>>> inmem: 2658ms >>>>>> inmem+arena: 1422ms >>>>>> >>>>>> Linux/ext3: >>>>>> Initial (current SVN): 3603 ms >>>>>> arena: 3018ms >>>>>> inmem: 2263ms >>>>>> inmem+arena: 1588ms >>>>>> inmem+arena+strings: 1431ms >>>>>> >>>>>> Time to delete on Linux: 480ms without and 250ms with the arena; >>>>>> negligible on Windows. >>>>>> >>>>>> Serialisation time (near-equal in all of above): 1200ms >>>>>> >>>>>> So to solve the load time problem on windows I could write a buffer >>>>>> binary file input abstraction as well. But what's the point? Why not >>>>>> just memmap the whole file in when loading? The in-memory size of the >>>>>> RELOAD tree is going to be much larger than the file, so you're not >>>>>> reducing memory requirements by much by messing around with a fixed >>>>>> size buffer. On the other hand, if you're not loading in the whole >>>>>> file (delayed loading), that's probably going to demand a different >>>>>> buffering design, so once again, we shouldn't waste time writing it >>>>>> right now. >>>>> >>>>> The thing about delay loading is that it might be, say, twice as slow, >>>>> but >>>>> that will be acceptable because it's only doing a tiny bit of work at a >>>>> time. >>>>> >>>>> That said, in cases where delay loading would be unwanted (save games, >>>>> etc), >>>>> we should still use the memory mapped file approach. The nice thing >>>>> about >>>>> memory mapped files is that even if you map a 500 meg file, it won't >>>>> load >>>>> it >>>>> until you actually ask for the pages. >>>> >>>> Which may mean memmapping is fine when delay loading too. Might be OS >>>> dependent. >>> >>> Why would it be OS dependant? I should hope that the Linux kernel uses a >>> similar scheme to Windows. >> >> Who said anything about Linux? Can I really trust the Haiku kernel to >> behave the same? Minix? AmigaOS??? > > When FreeBASIC runs on those platforms, I'm sure I'll care. > > Seriously, though, if those kernels support memory mapped files, then I bet > they'll behave similarly to the platforms we're familiar with.
Well, no. Kernels are enormously complicated under the hood, there are a multitude of different strategies that an OS can use to deliver the bytes that you ask for. Continued below... >> But I meant, I don't know how much difference there will be on >> operating system X between memmapping a file and reading a few bytes, >> and reading those same file positions using plain file operations. > > As I alluded before, the kernel loads memory mapped files in pages, the same > way it loads binaries. Since the page size is 4kb on the x86 platform, then > it would be similar to reading with a 4kb buffer, except: > > 1. The buffer is guaranteed to be page aligned, since it IS a page, and > 2. The overhead is slightly less, since there are no function calls involved > (the kernel loads it in a page fault) > > Actually, I'm not sure what the implications of #2 are. I'm guessing that > overall system performance is marginally reduced, since while the kernel is > loading memory, other processes cannot do the same. However, I am not an > expert. > > So, let's check Wikipedia! > > http://en.wikipedia.org/wiki/Memory-mapped_file > >> The major reason to choose memory mapped file I/O is for performance. One >> should nevertheless keep in mind the tradeoff that is being made. The >> standard I/O approach is costly due to system call overhead and memory >> copying. The memory mapped approach has its cost in minor page faults - when >> a block of data is loaded in page cache, but not yet mapped in to the >> process's virtual memory space. Depending on the circumstances, memory >> mapped file I/O can actually be substantially slower than standard file I/O. > > So, I wasn't too far off. > > (Incidentally, if you weren't talking about performance, then please > disregard this entire message) I've been reading a thread on a FreeBSD mailing list linked to from that article (someone complaining about bad mmap performance). It's hard to draw bitesized conclusions, but here are some enlightening quotes: >I've heard people repeat the same notion, that is to say "that mmap()ing a >file is supposed to be faster than read()ing it" [1], but the two operations >are not quite the same thing, and there is more work being done to mmap a file >(and thus gain random access to any byte of the file by dereferencing memory), > than to read and process small blocks of data at a time. > For linear processing of large data sets you almost universally want > to use a read() loop. There's no good reason to use mmap(). >The amount of "work" for the kernel to do a read() and a high-speed >memory copy is much less than the cost of taking a page fault, running >a whole bunch of really really nasty code in the vm system, repairing >the damage from the page fault, updating the process paging state and >restarting the instruction. >mmap is more valuable as a programmer convenience these days. My conclusion: if you have a clue what your usage pattern will be and buffer intelligently, you should beat mmap which is equivalent to asking for random access and placing the burden on the kernel. >>>>> Actually, thinking about this, I wish I'd thought about this earlier, >>>>> since >>>>> it would make a lot of the loading code easier. >>>> >>>> So, who's going to implement it? >>> >>> NOTITCALLEDIT! >> >> :) >> >>>>>>> Also, on Linux it took less than half as long to delete the document >>>>>>> as it does in Windows without a private heap. >>>>>>> >>>>>>>> One thing I want to implement is allocating memory for multiple >>>>>>>> nodes >>>>>>>> at >>>>>>>> once. This will be tricky, though, since deleting nodes is a >>>>>>>> problem. >>>>>>>> Hmm... >>>>>>>> I will give this some thought... >>>>>>> >>>>>>> I don't think that's really necessary (unless the memory allocation >>>>>>> really is the bottleneck) and would be pretty damn messy. >>>>>> >>>>>> I misunderstood you; thought you were talking about that rtlArray >>>>>> stuff. Allocating multiple nodes at once is not very complex or messy. >>>>>> And I think not being able to free blocks of memory as nodes are >>>>>> deleted would not be too large a problem: in which circumstances would >>>>>> you actually be doing a lot of deleting? I can't think of many aside >>>>>> from the optimise step in xml2reload, and the others would only occur >>>>>> in Custom, not Game, where I think it won't be a problem. >>>>>> >>>>>> However, by looking at the numbers above, it looks like it would >>>>>> reduce load time by 25% at most, not worth it. >>>>> >>>>> Well, my idea would only be useful when loading a tree from disk, not >>>>> building it in RAM (how does it know how many nodes you're going to add >>>>> here?). >>>>> >>>>> Last night, I tried implementing it with a "read only" option, so it >>>>> would >>>>> disallow modifying the (very fragile) tree, but it ended up getting so >>>>> complicated that I'd have to rewrite quite a bit of stuff, which I >>>>> didn't >>>>> feel like doing. >>>>> >>>>> I think we should just forget this particular idea and move on. >>>> >>>> Oh, I guess I didn't misunderstand, seems you were thinking about >>>> rltArray. I meant writing a simple memory allocator which allocates >>>> space for multiple Nodes at a time (say 64, which is 4kB) but also >>>> allows freeing Nodes: it keeps a free list and deletes chunks if they >>>> become totally empty. I've written exactly this before. Anyway. >>> >>> rltArray was similar, but a different idea. The idea was that a node >>> would >>> contain X "slots" of homogenous data, i.e. an array, but all in one node. >> >> Kind of like your awful* Frame arrays hack! >> >> * which turned out to be just fine aside from being very bug-prone and >> hard to maintain; but I will probably keep it intact in SpriteSet > > I don't recall this one. Frame was Simon's idea, and if I was going to > extend it, an array of Frames would seem to be the way to go. I see. Write convoluted code, dump it on the next guy, and then forget about its existence :P >>> Your comments sprinkled about in regards to rltArray are, in fact, in >>> accurate, as they never implied multiple nodes. That said, I am not sure >>> how >>> I was going to implement it, so I should just strip it out... >> >> That would be nice. >> >>>>> -- >>>>> Mike >>> >>> >>> -- >>> Mike > > > -- > Mike > _______________________________________________ > Ohrrpgce mailing list > [email protected] > http://lists.motherhamster.org/listinfo.cgi/ohrrpgce-motherhamster.org > _______________________________________________ Ohrrpgce mailing list [email protected] http://lists.motherhamster.org/listinfo.cgi/ohrrpgce-motherhamster.org
