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

Circular references #9

Closed
majg0 opened this issue Nov 8, 2021 · 9 comments
Closed

Circular references #9

majg0 opened this issue Nov 8, 2021 · 9 comments

Comments

@majg0
Copy link

majg0 commented Nov 8, 2021

I don't think this should be supported, because of the extra overhead either in performance or in API complexity, but I think the documentation should state clearly that it's not handled. I mean, reading the code this is obvious, but it would still be a good service :)

P.s. do you align with the handmade manifesto? If you haven't heard of it, do check it out, because it seems you do! 😊🙌

Cheers

@infostreams
Copy link

Don't be silly, the overhead in both complexity, memory use, and code size is negligible (see for yourself 3b6ab8f), and it serves the important purpose of not creating situations where using this library would potentially crash the process or exhaust your server's memory. For such a small addition (less than 2 lines of code) that's a huge win.

@majg0
Copy link
Author

majg0 commented Nov 8, 2021

That totally depends on the size of the objects. If the user knows that there are no circular references, why have the runtime check it at all?

For big JSON blobs with many objects, this will severely impact performance by having to

  1. reallocate the array
  2. linearly search for references for every addition

Sure, it is assumed that it is small, but performance almost doesn't matter in the small anyway, so why micro-optimize then?

@infostreams
Copy link

Benchmark it.

@majg0
Copy link
Author

majg0 commented Nov 8, 2021

I don't have the incentive to put that amount of time in, I don't even use the lib. Just wanted to put this issue out there.

@AsyncBanana
Copy link
Owner

I will try to benchmark this later. If the results show that cyclical reference checking slows nested objects down enough, I will add an option to disable the checking.

@AsyncBanana
Copy link
Owner

For most objects with low depth, as expected, there is little to no difference. However, with highly nested objects, I noticed a difference of around 30%. Should I add an option to disable it? Having the choice would increase code size slightly and reduce performance slightly, but it would overall be faster if you disable checking, and you can still keep checking if you want.

@majg0
Copy link
Author

majg0 commented Nov 10, 2021

Great work! 🎉🙌

One thing we could try before using a flag is to use a Set instead of an Array. How does that affect performance?

I think a 3% pessimization or so is where I'd feel alright about it personally, because if one only diffs large JSON blobs which just arrived serialized over the network, there is no need to check for circular dependencies, so it would feel silly to miss out on something like every 20th diff (in the case of 5% e.g.)

@AsyncBanana
Copy link
Owner

I decided to add an option in v1.2.0, so by default, cycles are checked, but you can disable it as specified in the README and release notes.

@majg0
Copy link
Author

majg0 commented Nov 13, 2021

Nicely done!

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

3 participants