3 ms·
If your allocator is fast, the IO-list approach used by Erlang is a lot faster at copying and concatenating strings than anything that involves copying the char
by kragen 10y ago
If your allocator is fast, the IO-list approach used by Erlang is a lot faster at copying and concatenating strings than anything that involves copying the characters around. I used this to good effect in Ur-Scheme. But then processing the contents of the strings becomes potentially expensive, and you may have aliasing bugs if your strings are mutable.
Python 2.7.3 concatenates strings in string_concat using Py_MEMCPY, which is a macro defined at Include/pyport.h:292. That invokes memcpy, except for very short strings, where it just uses a loop, because on some platforms memcpying three bytes is a lot slower than just copying them. In http://canonical.org/~kragen/sw/dev3/propfont.c http://canonical.org/~kragen/sw/dev3/propfont.c I got a substantial speedup from writing a short_memcpy function that does this kind of nonsense:
if (nbytes == 4) {
memcpy(dest, src, 4);
} else if (nbytes < 4) {
if (nbytes == 2) {
memcpy(dest, src, 2);
} else if (nbytes < 2) {
The main case in eglibc 2.13 memcpy, which is what Python is invoking on my machine, is as follows:
/* Copy just a few bytes to make DSTP aligned. */
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
/* Copy whole pages from SRCP to DSTP by virtual address manipulation,
as much as possible. */
PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
/* Copy from SRCP to DSTP taking advantage of the known alignment of
DSTP. Number of bytes remaining is put in the third argument,
i.e. in LEN. This number may vary from machine to machine. */
WORD_COPY_FWD (dstp, srcp, len, len);
/* Fall out and copy the tail. */
}
/* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_FWD (dstp, srcp, len);
In sysdeps/i386/i586/memcopy.h, WORD_COPY_FWD uses inline assembly to copy 32 bytes per loop iteration, but using %eax and %edx, not using SIMD instructions. It explains:
/* Written like this, the Pentium pipeline can execute the loop at a
sustained rate of 2 instructions/clock, or asymptotically 480
Mbytes/second at 60Mhz. */
This is presumably what Jeff's code was a replacement for. Too bad he didn't contribute it to glibc, but he presumably wrote it at a time in Google's lifetime where the Google paranoia was at its absolute peak.
PAGE_COPY_FWD sounds awesome but it's only defined on Mach. Elsewhere PAGE_COPY_FWD_MAYBE just invokes WORD_COPY_FWD.
My tentative conclusion is that Ulrich scared everyone else away from wanting to work on memcpy so effectively that it's been unmaintained since sometime in the previous millennium.
- nostrademons 10y agoInteresting - the last time I looked seriously at Erlang (~2005), its string handling was a mess. Strings were lists of characters, which ate 8 bytes of memory per character, couldn't be addressed in O(1), and were often slow to traverse because of cache misses. IOLists seem to be a big improvement on that. They're functionally equivalent to a Rope, right? Ropes are used all over the place at Google (where they're called Cords); I worked on a templating engine while I was there that would just assemble pieces into one for later flushing out to the network.
- kragen 10y agoErlang has always used IO lists for I/O, and they can include both strings and "binaries", i.e. byte arrays. IO lists are very similar to ropes, but because they don't even store the length in each node, they're even faster than ropes to concatenate, but linear-time to index into.