Reduce memory usage #1258
Replies: 4 comments 10 replies
-
Hey |
Beta Was this translation helpful? Give feedback.
-
Normally when one presents data on the memory use of an algorithm an effort is made to minimize the contributions to the data due to factors unrelated to the algorithm. It takes five minutes to realize that in this case the structure is performing unnecessary calculations. The expected end is calculated to be While it it true that in most environments creating a zero sized array is considered an error, this check is not necessary for pl. You can create zero sized arrays freely, pl just creates nothing but that is exactly what is being asked to do. . I tested the code you posted above using the file you also posted and the average run used 2 Gib.s of ram and took about 20 secs on my aging pc. This is to be expected if the code needed to include those extra calculations and conditional checks. The following code produces the exact same number of real variables (excluding the padding) with the same names at the same positions but in a fraction of the time and using less memory than the input file size itself. #pragma endian little
#include <std/mem.pat>
struct ChunkPreamble {
u32 tag;
u32 subtag;
u32 size;
padding[4];
u8 unknownData[size] [[sealed]];
padding[-$ & 7]; // -offset & 7 is the number of bytes to next 8 byte aligned address
};
//u64 fileSize = std::mem::size();
//ChunkPreamble chunks[100] @ 0x00;
// This line might use 6GB memory.
ChunkPreamble allChunks[while(!std::mem::eof())] @ 0x00; My same aging pc used less than 4 Mib.s of ram and ran for about 1 sec. to produce the same pattern. The relation may still not be linear for non optimized arrays, but that doesn't mean that the code is not fast or efficient. The information comes from then constants that go with the order estimates a c in C*O(n). |
Beta Was this translation helpful? Give feedback.
-
I might have found the root issue, but don't know how to fix it because I'm a novice in pattern language. Let's simplify the pattern, it will parse first 512KB of any test file: // #pragma debug
struct SimpleChunk {
u8 dataAxBy[1024] [[sealed]];
u32 locafe = 0xcafe;
};
SimpleChunk sc[512] @ 0x00; When processing This piece of code:
When assigning When assigning Running the pattern code with local variable will use (1 + 2 + ... + 512) * 1024B = 131328KB (plus variable itself and some overheaed) more memory than the code without local variable, which is exactly what I have observed (152MB vs 18MB). I add |
Beta Was this translation helpful? Give feedback.
-
That makes sense and explains the observed behavior that adding a 4 bytes local variable increases the pattern size from mb to gbs as we both reported 4 days ago. I should reran the tests where the two sizes were being reported similar but the case with non local variable was much smaller and see why that was happening. |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm writing a pattern to parse trace v3 files, but my code uses way too much memory (6 GB memory to parse a 10 MB file).
The issue seems to be the
unknownData
. I triedpadding unknownData[expectedEnd - $]
but the memory usage is as high asu8
. Is there a proper way to mark/skip these bytes without using huge amount of memory? Thanks in advance.Here is my pattern code:
and here is the test file: test.tracev3.zip
Beta Was this translation helpful? Give feedback.
All reactions