Skip to content
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

ObjectCache Performance Consideration #78

Open
xLEGiON opened this issue Jan 6, 2020 · 0 comments
Open

ObjectCache Performance Consideration #78

xLEGiON opened this issue Jan 6, 2020 · 0 comments
Assignees

Comments

@xLEGiON
Copy link
Contributor

xLEGiON commented Jan 6, 2020

In ObjectCache, ClearSerializationCache() uses Clear() on the serialization cache Dictionary. This is an O(n) operation, where n is the capacity of the Dictionary. ClearSerializationCache() is called near the end of Serialize().

Using a CerasSerializer instance to serialize an object with many references may cause the capacity of the Dictionary to increase significantly. If we then use the same CerasSerializer instance to serialize objects with very few references, the serialization processing time can be significantly slower than expected due to the larger capacity of the Dictionary affecting Clear().

Simply replacing Clear() with creating a new Dictionary instance has a negative impact on the serialization processing time for repeatedly serializing objects with very few references. However, using Clear() when the Count of the Dictionary is less than or equal to the initial capacity of the Dictionary and creating a new Dictionary instance when the Count is greater seems to work well. This has the effect of keeping a consistent Dictionary capacity between calls to Serialize().

Edit: I've run a few tests. Here, "Current" refers to the current method of using Clear() and "Proposed" refers to the method described above. The source code has been attached.

Test 1: 1,000,000 calls to Serialize() for an object with few references.
Current: 4763ms
Proposed: 4793ms

Test 2: 1,000 calls to Serialize() for an object with many references.
Current: 5812ms
Proposed: 6785ms

Test 3: 1,000,000 calls to Serialize() for an object with few references after a call to Serialize() for an object with many references.
Current: 13952ms
Proposed: 4778ms

The proposed method does fix the issue but has a negative impact on the serialization processing time for repeatedly serializing objects with many references. A configuration option to preserve the object cache capacity between calls to Serialize() by always using Clear() would provide a way of optimizing for this use case.

Form1.vb.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants