replicache icon indicating copy to clipboard operation
replicache copied to clipboard

Test performance benefit of removing copies in read transactions

Open aboodman opened this issue 2 years ago • 4 comments

Currently Replicache returns deep clones in all read interfaces for safety. This was introduced in rocicorp/replicache#479 and the rationale is there.

However, this must be really expensive, both in cpu time and gc pressure, and it seems like it would be hard to see in the profile because the gc's will tend to be decoupled from cause.

I would like to test out the idea of skipping the clone for read transactions and see what it does for our benchmarks. If we ship this, then we would also want to do rocicorp/replicache-internal#56 to prevent typescript users from modifying this data.

aboodman avatar Mar 20 '22 16:03 aboodman

Currently Replicache returns deep clones in all read interfaces for safety. This was introduced in rocicorp/replicache#479 and the rationale is there.

We do not do that. ReadTransaction get returns a ReadonlyJSONValue.

We only clone for the WriteTransaction which returns a JSONValue.

arv avatar Mar 21 '22 15:03 arv

Oh. I see. I misread the code. 😳. I wonder what @tmcw was seeing then with object identities changing. That seems like it would happen only when a key/value pair changes, which is expected.

aboodman avatar Mar 21 '22 16:03 aboodman

We clone in put so what you put in is not going to be the same object as what you get out.

I still think there is room for improvement even if we do clone...

But I would be happier if we got rid of all the clones and told people not to do that or things will break!

arv avatar Mar 22 '22 08:03 arv

Another option is to conditionally clone/freeze. React uses process.env.NODE_ENV as a signal for doing expensive correctness checks. We could do the same.

arv avatar Mar 22 '22 09:03 arv

The current plan is to deep freeze in debug mode and do nothing in release mode (with the risk of things not working if people only test in release mode).

arv avatar Oct 20 '22 20:10 arv