Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:36:03+00:00 2026-05-26T23:36:03+00:00

I have some model objects that I save in a DB serialized with protobuf.

  • 0

I have some model objects that I save in a DB serialized with protobuf. I want to compare the version I will save to the existing one, to avoid to add two times the same version.

Ideally I should

byte[] existingBlob = GetFromDBExistingModelObject();
ModelType existingModel = existingBlob.Deserialize();
if (!model.Equals(existingModel))
{
    byte[] serializedModel = model.Serialize();
    Save(serializedModel); //Save in DB the new blob
}

However I will have to implement .Equals on every model object and this be quite painful. I would like to do

byte[] existingBlob = GetFromDBExistingModelObject();
byte[] serializedModel = model.Serialize();
if (!compareBlob(existingBlob, serializedModel)
{
    Save(serializedModel);
}

private bool compareBlob(byte[] existingBlob, byte[] serializedModel)
{
    if (serializedModel.Length != existingBlob.Length)
    {
        return false;
    }

    return !serializedModel.Where((t, i) => t != existingBlob[i]).Any();
}

I also do that for performance, because I don’t deserialize the existingBlob

What do you think of this implementation ? Do you think I can rely on this comparison ? I use protobuf for serialization.

Thanks for your comment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:36:03+00:00Added an answer on May 26, 2026 at 11:36 pm

    protobuf-net will produce a predictable output, but strictly speaking that is not guaranteed by the spec; – there are 2 edge cases (field-order, and sub-normal forms† for varint encoding) that technically could produce different output with the same meaning, but protobuf-net will always produce the same output currently.

    I am toying with adding an option to deliberately use sub-normal varint forms to avoid some memory shuffling, but that would be opt-in only.

    So; as long as you aren’t building your binary files by appending (protobuf is an appendable format, but obviously all bets are off if you are appending in arbitrary orders), then yes: the data on the wire should be predictable, and you can compare the byte sequence to test for equality.

    As a minor note, I would recommend a regular for loop here, for efficiency:

    if (serializedModel.Length != existingBlob.Length)
    {
        return false;
    }
    for(int i = 0 ; i < serializedModel.Length ; i++)
        if(serializedModel[i] != existingBlob[i]) return false;
    return true;
    

    (if you are particularly speed-crazy, you could even use unsafe code and compare it as a int* or long* instead (taking 1/4 or 1/8 of the tests), and just check the last few bytes manually)

    You might also consider comparing a hash (sha1 etc) instead of byte-by-byte; this would be especially useful for large models, especially if you can store the hashed value along with the original (so you never have to fetch the original existing BLOB – just the existing hash).


    † : specifically, the bit-sequence 10000000 or 00000000 at the “big end” of a varint just means “and more zeros at the big end” (with or without more data to follow), so has no impact on the number; hence any (reasonable) number of 0x80 0x80 0x00 on the end of a varint does not change the result; there is a use-case where-by this could be used to avoid having to move data around, by deliberately using an oversized varint as a length-prefix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some constants that represent the valid options in one of my model's
I have some model objects I'm using in my Java client application. Later these
I have some configuration data that I'd like to model in code as so:
I have a class that contains data from some model. This class has metadata
I have model Article it has field title with some text that may contain
I have a Project model and it has some text attributes, one is summary.
I have a managed Object Model that contains 2 Entities. One of the entities
I have a repository class that defines some basic Get/Save/Delete methods. Inside these, I
I'm confuse about objects that I can't save, simplified model is class Subscription <
I have a model that I overrode the save method for, so that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.