Open johnswede opened 2 months ago
I'm fairly sure this is a buffered vs non-buffered IO issue. I ran both implementations and on my x86 Linux system the python version made 1654 syscalls (counted by strace piped into wc), the mojo binary (I compiled it beforehand) was at 1000549. If I switch to reading in 4096 bytes (python's default buffer size), Mojo pulls ahead by ~20%.
Does it make sense to switch the file type to buffered IO by default?
Also, @johnswede, consider using mmap + madvise for parsing large files, it saves a lot of implementation work and the extra information is often easier for the kernel to work with.
Bug description
I was attempting to parse a large file in Mojo when I discovered it's 10x slower than using Python.
These were the speed differences when reading 4-byte chunks 1 million times.
The culprit appears to be the external calls in
read
andread_bytes
:https://github.com/modularml/mojo/blob/b5391bae169c63d36e75f2ffa33c6dc6d8242f99/stdlib/src/builtin/file.mojo#L339-L345
Do you have any idea why the external calls are so slow?
Steps to reproduce
Profiling code
Python:
Mojo:
System information