Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[#51] Fix performance issue with Object Store #52

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

Merged
jacksondunstan merged 7 commits into jacksondunstan:master from AustinSmith13:master
Jan 19, 2020

Conversation

@AustinSmith13
Copy link
Contributor

@AustinSmith13 AustinSmith13 commented Jan 14, 2020

When BaseMaxSimultaneous was set to a large number the object-store was
spending to much time finding the handle. This was fixed using a
dictionary where the keys have the objects full hash.

= added 2 commits January 13, 2020 22:01
When BaseMaxSimultaneous was set to a large number the object-store was
spending to much time finding the handle. This was fixed using a
dictionary where the keys have the objects full hash.
Copy link
Contributor Author

After looking at this in the morning I realized I'm keying with hashcodes which is not guaranteed to be unique.

Copy link
Owner

@AustinSmith13 let me know if you fix the uniqueness problem and I'll review. Thanks for the PR! 👍

Copy link
Contributor Author

@jacksondunstan After a little bit of research object.GetHashCode() returns a unique index assigned to the object by the CLR when the object is created and released from garbage collection.

So I don't think its likely to have a hash-code duplicate in the same app-domain but maybe after collection which I don't think is a problem.

I did implement a version with hash collision handling but it less nice looking and is probably slower. (but not by much)

I am thinking about keeping it the way it is, but microsoft advises against it here https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netframework-4.8

public static int GetHandle(object obj)
{
	// Null is always zero
	if (object.ReferenceEquals(obj, null))
	{
		return 0;
	}
				
	lock (objects)
	{
		// Look up the handle in the hash table
		uint hash = (uint)obj.GetHashCode();
		List<int> handleBucket = null;
		if (handleBucketByHash.TryGetValue(hash, out handleBucket))
		{
			for (int i = 0; i < handleBucket.Count; i++)
			{
				int handleInBucket = handleBucket[i];
				object objectInBucket = objects[handleInBucket];
							
				if (object.ReferenceEquals(objectInBucket, obj))
				{
					return handleInBucket;
				}
			}
		}
	}
				
	// Object not found
	return Store(obj);
}

I've tried to cause hash collisions by creating and destroying thousands of objects per frame. No collisions.

If you are fine with whats there I would like to keep it the way it is without any collision handling for slight performance benefit.

Copy link
Contributor Author

Sorry about constantly changing this I can't help but try to make things perfect. I realize a better way of doing this would be to use a single dictionary Dictionary<object, int>. Dictionaries already consider hash collisions for there keys. I'll have this change ready for review later today.

...objects
Dictionary<object, int> will take care of hashing/collisions for us.
Copy link
Contributor Author

@jacksondunstan This is ready for review.

After looking into how dictionaries work, I realized they already do everything I was previously trying to do. Internally they call GetHashCode and check equality for collisions.

Copy link
Owner

@jacksondunstan jacksondunstan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @AustinSmith13, thanks for the continued development on this PR. I just had a few comments and two of them are pretty minor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jacksondunstan jacksondunstan jacksondunstan requested changes

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /