Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exploit pointer equality in Eq (memcmp does not seem to do this) #73

Closed
andreasabel opened this issue Mar 29, 2014 · 2 comments
Closed

Comments

@andreasabel
Copy link
Member

Eq for Data.Text is implemented in terms of Data.Text.Array.equal, which call the C memcmp function.

int _hs_text_memcmp(const void *a, size_t aoff, const void *b, size_t boff,
size_t n)
{
  return memcmp(a + (aoff<<1), b + (boff<<1), n<<1);
}

However, the C implementation does not seem to check for pointer equality first. Would it not make sense to check equality of a + (aoff<<1) and b + (boff<<1) first? That would bring the equality test down from O(n) to O(1) for identical texts!

@bos
Copy link
Contributor

bos commented Dec 12, 2014

You are correct that this is possible, but I would be surprised if it's common enough to offset the small performance hit of the added compare-and-branch. I am open to data proving otherwise, of course.

@bos bos closed this as completed Dec 12, 2014
@andreasabel
Copy link
Member Author

Fair enough. At least I could not notice a different on my (large) test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants