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 8767103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:43:26+00:00 2026-06-13T16:43:26+00:00

If objects are immutable it’s very easy and efficient to make a deep copy

  • 0

If objects are immutable it’s very easy and efficient to make a deep copy of object – just copy memory pointer of that object.

It’s also very easy and efficient to do deep equality check – just compare the pointers.

But what happens if data comes from the outer world and we need to check its identity?

Consider following example:

  • Application query data for a Post from DataBase, deserialize it into the immutable Post Object (Model) and cache it in memory.
  • After a some time Application query the same data again, and also deserialize it into the immutable Post Object.
  • Now, how we can check if Post has been changed? We can’t just compare references of immutable objects to check identity. References will be different (because we deserialized the data twice) but the data itself still may be the same.

How to handle such situations?

  • 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-06-13T16:43:28+00:00Added an answer on June 13, 2026 at 4:43 pm

    Two approaches that may be workable, or may give you more ideas for future approaches:

    1. Keep one or more dictionaries of known immutable instances, and look up instances you construct to see if they are in the dictionary yet; if so, substitute the dictionary instance for the newly read-one. Note that unless the dictionary is used every time an object is constructed, reference *equality* can be used to expedite comparisons, but reference *inequality* cannot. It may be useful, however, to have objects cache their hash code since unequal items will *usually* have different hashes. Note as well that unless you use a “WeakDictionary” of some sort, you’ll have to make sure to periodically clean out the dictionary (in practice, this means that unless you have a WeakDictionary, it will be hard to clean out unused items without at least occasionally cleaning out used ones).
    2. It may be possible to give each object a creation-time indicator (perhaps a static `Interlocked.Increment` counter) as well as a link to the oldest (first created) object which is known to be equal. When comparing objects, follow the chains of “older known-equal” objects. If the chains reach the same object, the original objects are equal. Otherwise, if the objects’ hash codes match, test them for value equality. If they’re equal, update the link on the newer to point to the older. If either starting object had a chain of more than one link, `Interlocked.CompareExchange` its link to point directly to the oldest known-equal object. Using this approach, comparisons between value-equal objects will cause them to form cliques; comparison between value-equal objects within a clique will be fast, and comparison between value-equal objects in different cliques will join the two cliques into one. Note also that it may be desirable to have consumers of the nodes hold wrapper objects rather than direct links to the nodes themselves; if that is done, the wrappers for all the nodes in a clique could share a reference to the same internal data item.

    Approach #1 is probably a good one if a suitable dictionary type is available, but approach #2 could have some considerable advantages as well. The biggest annoyance with #2 is that it adds an extra layer of indirection to object accesses. Still, being able to have objects consolidate themselves quickly into cliques could be a major plus.

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

Sidebar

Related Questions

I want to make sure that a given group of objects is immutable. I
I want to make sure that I correctly understand the 'Effectively Immutable Objects' behavior
I know that immutable objects always have the same state, the state in which
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
Why are copy constructors unnecessary for immutable objects? Please explain this for me.
I'm building an entire application out of immutable objects so that multi-threading and undo
I recently wrote some data access methods (plain old Java) that use immutable objects
I have an object factory that hands out instances of a few constant, immutable
When creating classes for immutable objects immutable meaning that state of instances can not
Object-oriented design encourages the use of immutable objects to improve thread-safety and performance. I'm

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.