From: Junio C Hamano Date: Mon, 09 Apr 2012 23:03:38 GMT Subject: Re: [PATCH v2] xdiff: load full words in the inner loop of xdl_hash_record Message-ID: <7viph8tsmt.fsf@alter.siamese.dyndns.org> In-Reply-To: Thomas Rast writes: > +unsigned long xdl_hash_record(char const **data, char const *top, long flags) { > + unsigned long hash = 5381; > + unsigned long a = 0, mask = 0; > + char const *ptr = *data; > + char const *end = top-sizeof(unsigned long)+1; > + > + if (flags & XDF_WHITESPACE_FLAGS) > + return xdl_hash_record_with_whitespace(data, top, flags); > + > + ptr -= sizeof(unsigned long); > + do { > + hash += hash << 5; > + hash ^= a; > + ptr += sizeof(unsigned long); > + if (ptr >= end) > + break; > + a = *(unsigned long *)ptr; > + /* Do we have any '\n' bytes in this word? */ > + mask = has_zero(a ^ NEWLINEBYTES); > + } while (!mask); > + > + if (ptr >= end) { > + /* > + * There is only a partial word left at the end of the > + * buffer. Because we may work with a memory mapping, > + * we have to grab the rest byte by byte instead of > + * blindly reading it. Ok, because end is sufficiently below "top" and because you know "top-1" should be a mapped byte, the "ptr >= end" check in the above loop ensures that reading ul at ptr will stay within the range of mapped pages. Cute but ugly ;-)