c++ - Efficent way of swapping bytes in a memory-mapped file -
i managed parse large binary file (~8gb) reading blocks of data memory , swapping big-endian integers using functions showed below. however, trying gain more performance using boost memory-mapped files not able use endian_swap functions because file opened read-only mode. there efficient way swap bytes without writing original file? if not, performance affected i/o overhead?
inline void endian_swap(unsigned short int& x) { x = (x>>8) | (x<<8); } inline void endian_swap(unsigned int& x) { x = (x>>24) | ((x<<8) & 0x00ff0000) | ((x>>8) & 0x0000ff00) | (x<<24); } inline void endian_swap(unsigned long long int& x) { x = (((unsigned long long int)(x) << 56) | \ (((unsigned long long int)(x) << 40) & 0xff000000000000ull) | \ (((unsigned long long int)(x) << 24) & 0xff0000000000ull) | \ (((unsigned long long int)(x) << 8) & 0xff00000000ull) | \ (((unsigned long long int)(x) >> 8) & 0xff000000ull) | \ (((unsigned long long int)(x) >> 24) & 0xff0000ull) | \ (((unsigned long long int)(x) >> 40) & 0xff00ull) | \ ((unsigned long long int)(x) >> 56)); } the code found on article. thank time
at least underlying operating system supports desired behavior:
map_private create private copy-on-write mapping. updates mapping not visible other processes mapping same file, , not carried through underlying file. unspecified whether changes made file after mmap() call visible in mapped region. the priv flag appears translate map_private:
void* data = ::boost_iostreams_fd_mmap( const_cast<char*>(p.hint), size_, readonly ? prot_read : (prot_read | prot_write), priv ? map_private : map_shared, handle_, p.offset ); if (data == map_failed) cleanup_and_throw("failed mapping file");
Comments
Post a Comment