Skip to content

sizeof() sometimes used as if it would return size in bits and not bytes #1226

Closed

Description

@deng0

sizeof() is sometimes used as if it would return the size in bits instead of bytes. It seems these cases are related to GetHashCode, so it's not a big issue, but still it should probably be fixed.

One example of this would in the PGUtil class:
public static int RotateShift(int val, int shift){ return (val << shift) | (val >> (sizeof (int) - shift)); }

Activity

  1. roji commented on Aug 1, 2016

    @roji
    Member

    Thanks for raising this, I noticed this a year ago but forgot about it...

    We should probably do a general pass on hash code generation in Npgsql...

  2. added this to the 3.2 milestone on Aug 1, 2016
  3. roji commented on Aug 1, 2016

    @roji
    Member

    BTW you're welcome to submit a PR if want to.

  4. deng0 commented on Aug 1, 2016

    @deng0
    Author

    I've done a quick pass fixing the sizeof issue (no PR yet).
    But a more detailed pass on the hash code generation would probably be better.
    This is also related to the other issue #1145

    Let me know if I should make the PR anyway.

  5. deng0 commented on Aug 1, 2016

    @deng0
    Author

    If I might ask:
    Is there a specific reason why most Npgsql Types override ==, != and GetHashCode?
    Meaning is it needed/essential for something within Npgsql, Entity Framework or NHibernate or is it more or less just a service for the normal user of the library?

  6. roji commented on Aug 1, 2016

    @roji
    Member

    Regarding a PR, it's really up to you when you want to submit... If you think you're going to be doing more work in this area then it's better to wait and submit a single PR. However, please separate different changes in different PRs (or at least commits): fixing GetHashCode() is one thing, modifying mutability of Postgis types etc. is another.

    Regarding the overriding, it's been there since before I got involved (which is a long time now :)), I 've been wanting to look at this too. None of these types are actually used within Npgsql (and probably aren't really used in EF/NHibernate either, although I'm not sure). So it's a matter of providing high-quality types to users.

    Overriding == and != is a good idea when the type is immutable, and a bad idea otherwise (https://msdn.microsoft.com/en-us/library/ms173147(v=vs.90).aspx). It seems that overriding GetHashCode() in general is a good idea rather than leaving the default behavior, especially in structs where the default is reflection based and therefore very slow (the same goes for Equals).

    A cleanup here may be in order too for 3.2...

  7. roji commented on Sep 15, 2016

    @roji
    Member

    @deng0 just wanted to know whether you think you'll be working on this at some point...

  8. deng0 commented on Sep 16, 2016

    @deng0
    Author

    @roji sorry, I'm rather busy atm. I don't think I will work on this anytime soon.
    Thanks again for your insights.

  9. removed this from the 3.2 milestone on Sep 25, 2016
  10. added this to the milestone on Mar 9, 2018
  11. roji commented on Mar 9, 2018

    @roji
    Member

    Fixed via 361fbd0 (PR #1741), thanks @YohDeadfall.

  12. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions