Currently, I am immersed in an enlightening article about monomorphism. Within this article, a fascinating code snippet caught my attention:
function ff(b, o) {
if (b) {
return o.x
} else {
return o.x
}
}
ff(true, { x: 1 })
ff(false, { x: 2, y: 0 })
ff(true, { x: 1 })
ff(false, { x: 2, y: 0 })
The burning question posed is: how many property access inline caches reside within the function ff? And what state are they currently in? The answer lies in the fact that there are 2 caches, both embracing monomorphic characteristics as they solely interact with objects of one shape.
Intriguingly, I initially anticipated encountering a polymorphic cache due to an earlier example provided by the author:
f({ x: 4, y: 1 }) // polymorphic, degree 2
f({ x: 5, z: 1 }) // polymorphic, degree 3
f({ x: 6, a: 1 }) // polymorphic, degree 4
f({ x: 7, b: 1 }) // megamorphic
It appears that in this scenario, the function receives objects of varying structures which inevitably transform the monomorphic cache into a polymorphic one. This has left me pondering over why the situation contrasts significantly with the example discussed in the aforementioned question.