Coder Social home page Coder Social logo

Comments (7)

pauldemarco avatar pauldemarco commented on July 23, 2024

hashCode is also overridden, and forwards to _calcHashCode, which uses a ListEquality to compare byte for byte.

Do you have a specific test case that is showing a false positive?

from flutter_blue.

pauldemarco avatar pauldemarco commented on July 23, 2024

Closing for now. We can reopen if needed.

from flutter_blue.

djziegler avatar djziegler commented on July 23, 2024

I really looks like ListEquality.hash does a conventional hash rather than returning an int value that is a unique representation of the input value (Furthermore, ListEquality.hash is returning a 63 bit value, which is way fewer bits than in a GUID (see the & _HASH_MASK))

Source:

https://github.com/dart-lang/collection/blob/master/lib/src/equality.dart

class ListEquality implements Equality<List> {
// ....

int hash(List list) {
if (list == null) return null.hashCode;
// Jenkins's one-at-a-time hash function.
// This code is almost identical to the one in IterableEquality, except
// that it uses indexing instead of iterating to get the elements.
int hash = 0;
for (int i = 0; i < list.length; i++) {
int c = _elementEquality.hash(list[i]);
hash = (hash + c) & _HASH_MASK;
hash = (hash + (hash << 10)) & _HASH_MASK;
hash ^= (hash >> 6);
}
hash = (hash + (hash << 3)) & _HASH_MASK;
hash ^= (hash >> 11);
hash = (hash + (hash << 15)) & _HASH_MASK;
return hash;
}
}

from flutter_blue.

pauldemarco avatar pauldemarco commented on July 23, 2024

Interesting, I wonder what the collision rate would be, and if it's something to be concerned with. I'll reopen for now.

Consider looking into what the UUID lib does for this.

Related to #10

from flutter_blue.

djziegler avatar djziegler commented on July 23, 2024

Given that GUIDs have 128 bits and the hash codes you are using have 63 bits, each GUID will collide on average with 2^65 other GUIDs (36893488147419103232 other GUIDs).

As a general 'Globally Unique ID' class, having an '==' like this would kind of wreck the 'Unique ID' property.

However, given, that this is a flutter blue specific class, and that each program would only know about relatively few UUIDs (devices it has seen, service ids, characteristic ids etc), actual collisions will probably be pretty small.

However, if they do happen, it will be a really weird and hard to reproduce bug (for example a user will setup a fitness application with the UUID of the heart rate monitor. But it will just happen to collide with the UUID of a wireless camera - and you will have a really weird, hard to track down problem only effecting one user. Personally, I find these kinds of situations much more terrifying than a common bug - which one finds in testing and fixes.

For my purposes, I am just comparing the .toString () values of GUIDs - so it is not actually a problem for me.

On the other hand adding a check if ListEquality.equals () after the hash checks will add essentially no overhead.

Thank you for writing flutter blue - it is a massive relief to be free of Android SDK programming (which I am not a fan of), and I couldn't make the move until you wrote flutter blue.

from flutter_blue.

espresso3389 avatar espresso3389 commented on July 23, 2024

I'm also afraid certain 'potential' false-positives on the current implementation.
Apparently, if equals(a, b) then hash(a) == hash(b) is true as mentioned on ListEquality hash method but the reverse is not always true.
The only and correct way is to compare all the bytes one by one though it is assumed to be slower than just comparing hashes.

from flutter_blue.

espresso3389 avatar espresso3389 commented on July 23, 2024

I think, the operator == should be like this:

  operator ==(other) =>
    other is Guid && this.hashCode == other.hashCode && (const ListEquality<int>()).equals(_bytes, other._bytes);

from flutter_blue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.