mobx icon indicating copy to clipboard operation
mobx copied to clipboard

descriptorCache is not necessary

Open Jlg1128 opened this issue 1 month ago • 4 comments

Intended outcome: DescriptorCache is somewhat useful for performance optimization.

Actual outcome: I did some performance test, found that performance was better without using DescriptorCache and lower memory usage, even with a 50% duplicate rate.

How to reproduce the issue: https://codesandbox.io/p/sandbox/ntrp45

Versions

Jlg1128 avatar Nov 11 '25 03:11 Jlg1128

I don't remember which one, but there was a test showing significant regression without a cache. IIRC before v6 it used to cache whole descriptor, but we've found out the important bit was to reuse the accessors functions. I no longer remember, but maybe it wasn't (just) about creation or memory savings: If you have a class with X instances, then accessing the same property on any of the instance points to the same piece of code in memory, so I can imagine it's easier for JS engine/CPU to optimize. Maybe an engine can already tell these functions look the same and optimize them, dunno.

urugator avatar Nov 12 '25 10:11 urugator

Is it the "${version} - sort" in packages/mobx/tests/perf/perf.js? I've run other perf test cases, and whether caching is used doesn't show a significant difference except for this "${version} - sort". This particular test case has 400,000 keys, among which only four are unique, while the remaining 399,996 keys are all the letter "a".

I've made some further adjustments to my demo. Now there are a total of 400,000 keys, with 200,000 of them being duplicates (though such a high duplication rate would be unlikely in real scenarios). I then performed 20 read and write operations, the results still show that not using descriptorCache is better. I'm wondering if this aligns with what you had in mind.

reproduce: https://codesandbox.io/p/sandbox/ntrp45

Would it be possible to add a configuration option, for example disableDescriptorCache, to disable the descriptor cache?

Jlg1128 avatar Nov 12 '25 14:11 Jlg1128

Please note that per documentation https://mobx.js.org/observable-state.html, observable objects should not be used for dynamically keyed collections, use maps instead. MobX performance trade-offs are made such that (largely) statically shaped datastructures are stored using objects, and dynamically keyed objects as Maps. Generating random keys and using those as field names, is in that sense expected to degrade performance

Image

mweststrate avatar Nov 13 '25 19:11 mweststrate

Thank you for your reply. I've come to realize that the descriptorCache is indeed useful. My current project is an SSR application, where the descriptorCache proves valuable on the server side because it can be reused with each request. On the client side, however, the role of the descriptorCache might be less significant. The project involves a large number of keys (approximately 260,000), but they are rarely repeated.

Jlg1128 avatar Nov 16 '25 03:11 Jlg1128