Summary: Mostly syscalls and mmap do the same things just substituting a page fault for a syscall to get to kernel mode, but… In user space her code is using AVX optimized memory copy instructions which are not accessible in kernel mode yielding a significant speed up.
Bonus summary: She didn’t use the mmapped data in place in order to make a more apples-to-apples comparison. If you can use the data in place then you will get even better performance.
Sasha is universally applied to both males and females, although to be fair, in Russian, it's culturally much acceptable to call Alexander Sasha in any context whatsoever, whereas Sasha as–in female Alexandra is reserved for informal communication.
Sasha is derived from Alexander via its dimunitive, but obsolete form - Aleksashka - shortned to Sashka, further simplified to Sasha as per established name format of Masha (Maria), Dasha (Daria), Pasha (Pavel, Paul), Glasha (Glafira), Natasha (Natalia), etc.
The bonus is what makes this writeup silly. I've always seen mmap presented as a way to reduce copies. If you're not using it for that, then yes, it's not a given it will be faster.
Yes, the test is sort of a strange one because the read test is reading directly in to the final destination wheras mmap is not and doing a redundant copy instead. So it's not your typical "eliminate a copy and go faster" usage of mmap.
What it actually seems to be comparing is whether its faster if the copy happens in the kernel, in the read() system call, or whether it's faster in userspace.
The results are puzzling because I would expect the kernel to be faster since it would incur no page-faults to access the pages from page cache since those ought to fall under the page-global mappings, or, at least, be in the kernels linear ram map which use 1GB pages (edit: in either case, not incur faults for demand map/demand load). But maybe that's no longer the case in a post-spectre world?
So I think actually it's a test of "does AVX go enough-faster to outweigh the costs of the extra-pagefaults incurred by copying from a user-space mapping which has not been prefaulted"
Sounds like the answer is yes (which still surprises me). And I expect if they MAP_POPULATE it would pull even further ahead...
Yes, 1,167 pagefaults for the read() version and 66,704 pagefaults for the mmap() version. With MAP_POPULATE it's back down to 1,167 faults and it goes a bit faster.
My libc is using rep/mov, same as the kernel, however. So I can't conclude that AVX memcpy is the win.
From the profile of readsyscall version I see this:
42.12% fa [kernel.kallsyms] [k] copy_user_enhanced_fast_string
14.45% fa [kernel.kallsyms] [k] syscall_exit_to_user_mode
11.00% fa [kernel.kallsyms] [k] find_get_entry
6.28% fa [kernel.kallsyms] [k] syscall_return_via_sysret
5.14% fa [kernel.kallsyms] [k] entry_SYSCALL_64
nearly 15% of the time is spent on a single verw instruction. Spectre mitigations.
When I reboot with mitigations=off, sure enough, the difference goes away and read() and mmap() perform identically.
What OP has discovered is that spectre mitigations make syscalls enough slower than pagefaults that what used to be a slower way to do things is now faster.
If you want the mmap version to perform faster when spectre mitigations are off, you need to use MAP_POPULATE.
The kernel does have access to these instructions. It is a deliberate choice by kernel developers not to use them in the case discussed here. In other cases the kernel does use such instructions.
Stupid question: Does the entire AVX state need to be saved and restored for a memcpy routine? It seems like it would be sufficient to save at most a couple of registers for a tight copy loop...
Thanks. Curse this language. I just want to refer to people! It’s simple encapsulation and abstraction. I shouldn’t have to care about implementation details irrelevant to the context.
Curiously, the page you've linked explains that it isn't normal to use it in this case: "can only be used with a morphologically and syntactically singular antecedent when what it refers to is semantically collective and/or generic and/or indefinite and/or unknown".
> According to the third edition, The New Fowler's Modern English Usage (edited by Burchfield and published in 1996) singular they has not only been widely used by good writers for centuries, but is now generally accepted, except by some conservative grammarians, including the Fowler of 1926, who, it is argued, ignored the evidence:
Anyone arguing against singular 'they' may as well be arguing for a gender neutral 'he' - an argument lost over a century ago by virtually any modern English standard.
Absolutely true. At least one whole generation grew up taught in school that singular "they" isn't a thing. I was on the tail end of that and read a lot of books written by the previous generation, so it never stuck strongly in my head, but that's what GP is referring to.
I don't think that's the case. I think many people (myself included) were taught only that "they" is plural, not that it explicitly cannot be singular. The distinction there may be too subtle to matter for some people, though.
Either way, clinging to a frozen-in-time interpretation of a living, changing language is foolish for anyone who would like to easily understand and be easily understood. And not waste time in our too-short lives on pointless language nitpickery like we're doing here.
I suppose I should have said it's doubtful that you lived at a time when the argument against singular they was legitimate. I'm sure many teachers are teaching students incorrect things all the time.
But by modern English standards, which I would define through works like Fowlers', the legitimate argument against singular they is long over.
> Anyone arguing against singular 'they' may as well be arguing for a gender neutral 'he'
That seems like a flawed analogy; many people abuse the traditional generic usage of 'they' but I've seen few people abuse the traditional generic usage of 'he'.
Mostly they're bad ones, you can read about them in the linked articles, specifically what's noted in Fowler's 1926 is probably the best place to start.
But the usage of they to refer a single person is older than anyone alive if that makes you feel better about sticking it to your picky grade school teachers.
Don't worry about it. Some people lose their marbles because they think females get erased when male language is used. Just erase both genders and you'll be fine. Use singular they.
Bonus summary: She didn’t use the mmapped data in place in order to make a more apples-to-apples comparison. If you can use the data in place then you will get even better performance.