r/learnpython • u/lehmagavan • 17d ago
Parsing very large string that is already in memory
Suppose I have a very large string in memory containing entries separated by \n's.
I need to yield the lines until I find a match.
What's the most efficient way in standard Python in terms of both memory usage and execution time?
From what I've read, io.StringIO IS NOT guaranteed to not copy the entire string internally, which is absurd to me. Suppose I need that guarantee.
4
u/Discordbothost 17d ago
once its already a giant str youve paid the memory cost. dont bother with StringIO — just walk it with find so you never build a full list of lines:
i = 0 while True: j = s.find("\n", i) line = s[i:] if j < 0 else s[i:j] if match(line): ... if j < 0: break i = j + 1
slices still allocate the line, but you stop at the first hit instead of splitting everything up front.
0
u/lehmagavan 17d ago
Yeah but can someone explain why can't the StringIO guarantee a zero-copy parsing? It makes no sense to me why it would ever need to cpy the string internally.
1
u/Discordbothost 17d ago
it's because StringIO is a read/write file object, not a view. you can seek and write into it, and a python str is immutable, so it has to own its own buffer. in cpython it also converts your str into a fixed-width internal buffer so seek/tell by character index is cheap, which means a copy (and sometimes widening it to 4 bytes per char).
BytesIO is the odd one out, it does share the original bytes until you write to it. but for a str you already have in memory, just walk it with find():
pos = 0 while (nl := s.find('\n', pos)) != -1: line = s[pos:nl] pos = nl + 1each slice is still a small copy but you never duplicate the whole thing
1
2
u/Rubix321 17d ago
Did this string come from a file?
1
u/lehmagavan 17d ago
No, it's guaranteed to come from memory and be passed via the function call.
2
u/Rubix321 17d ago edited 17d ago
How does the string initially get pulled into memory?
-1
u/lehmagavan 17d ago
I don't know, all I know is that I need to craft a function that solves the problem as efficiently as possible.
4
u/Rubix321 17d ago
Describe how you are wanting to "match" something.
Particular word? Pattern? All in one line?
Do you only need to find the first time in happens in the string?
1
u/Rubix321 17d ago
Both regex and string.find() supposedly don't need to make a copy of the string it's searching. So it really depends on the complexity of match algorithm you need to do.
2
u/MezzoScettico 17d ago
What about string.find() to locate the next \n? From what I read, a lot of string operations including that one, are optimized in C.
-1
3
u/QuinlanResistance 17d ago
.split() then iterate?
1
u/lehmagavan 17d ago
That would allocate a list of substrings.
1
u/Refwah 16d ago
What do you think yield is going to do
1
u/pachura3 16d ago
The whole point of using
yieldis not to allocate everything at the same time (for which you don't have enough memory), but to stream stuff.1
u/Refwah 16d ago
But it is duplicating the substring in memory which is what the OP is explicitly attempting to avoid
the entire iterator already exists in memory, the benefit of using yield here is minimal at best, and just adds some bizarre convolution to the code rather than just accepting that the entire array exists in memory already
The entire array existing in memory already is the entire 'need; for this code to be optimised, and so the solution is to work within that existing framework (it exists in memory, why would we yield) or that overarching component is rewritten to stream the contents into memory as needed, which would then lead you to the yield path
1
u/pachura3 16d ago edited 16d ago
But it is duplicating the substring in memory which is what the OP is explicitly attempting to avoid
No, OP wants to avoid duplicating the whole original string in memory. It is rather inevitable to temporarily duplicate separate lines (but they will be swept out by the garbage collector when they accumulate).
This is of course if you do not intend to implement regular expressions engine that works directly on
memoryviewsinstead ofstrs. (By the way, string slices in the default Python implementation do copy data).The whole use of
yieldis to create a pseudo-view on the big string (a streamed tokenizer) so it could be processed like a collection of separate lines. Does it introduce some overhead? Perhaps yes, to be benchmarked. Does it simplify the code by introducing an abstraction? Definitively.0
u/lehmagavan 16d ago
Also, using a generator would be func call overhead. I think the best solution is to yield manually using .find() and moving the cursor in each iteration.
1
u/Refwah 16d ago
How do you know the caller of your function is not just adding the responses to a list
0
u/lehmagavan 16d ago
I don't control neither the caller nor anything other than my function (+ global cache). All I know is that automated tests will measure mem/CPU usage and the execution time. Now you get it? ;)
1
u/pachura3 16d ago
I'd propose to do simple benchmarking by yourself as well, rather than guessing which method works better.
11
u/EntrepreneurHuge5008 17d ago
Rather than having the string in memory, put it in a .txt file and just read it line by line until you find whatever you wanted matched, as opposed to loading the whole thing into memory.
3
u/latkde 17d ago
Python strings don't really support zero-copy parsing. The standard approach is to bite the bullet and keep using smaller strings, which generally will produce copies.
There are some options though:
memoryviewsupports bytes-like zero-copy views, which can sometimes be useful. However, I don't think this will help with your parsing problems.Regex functions and some string functions can be anchored to a certain offset, rather than to the start. E.g. you can search for the next occurrence of a regex pattern or a constant string starting at some offset. This is the solution I generally prefer. Once there's a match, I can extract the relevant slice, which at least reduces the amount of copies.
If performance really matters , you'll have to switch to native code, e.g. Rust. While you can do zero-copy parsing in any language that supports pointers, Rust is uniquely designed to support safe sharing of data, e.g. multiple slices into the same string buffer. Rust also has a mature ecosystem of parser combinator libraries, e.g.
nomorwinnow. Thanks to the PyO3 interface, integrating Rust code into Python is relatively straightforward.
4
u/KronktheKronk Github username 17d ago
Consume the string like a list, element by element. Put elements into another string variable. When you get to newline, yield the string
2
u/fasta_guy88 17d ago
for match in re.finditer(r'[^\n]\n',long_string):
if re.search(r'thing_I_am_looking for',match.group()):
print("Found it: %s"%(match.group()))1
u/pachura3 17d ago
after_last_eol = 0
while after_last_eol < len(big_in_mem_string):
next_eol = big_in_mem_string.find("\n", after_last_eol)
if next_eol == -1: # end of file
next_eol = len(big_in_mem_string)
if next_eol > after_last_eol: # skip empty lines (multiple \n in row)
ln = big_in_mem_string[after_last_eol:next_eol] # create string slice
if matches(ln): # your matching logic
yield ln
after_last_eol = next_eol + 11
u/Altruistic_Sky1866 17d ago
Did you think regular expression and split the string? I am not sure how efficient it is or if it applicable in your situation though
23
u/Refwah 17d ago
Holy xy problem Batman