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

New DeepClone code snippet #847

Merged
merged 4 commits into from
Oct 7, 2024
Merged

New DeepClone code snippet #847

merged 4 commits into from
Oct 7, 2024

Conversation

EnumEnv
Copy link
Contributor

@EnumEnv EnumEnv commented Sep 28, 2024

Changes

I've made a bit more documented of a code snippet to help scripters navigate their way through it, considering not everyone viewing would be experienced. This take on the deep clone snippet also gives a faster result, meaning the code is ran faster and you get your value returned faster.

OLD: 0.0000051 (os.clock)
NEW: 0.0000034 (os.clock)

The difference may be small but definitely in some cases noticeable and useful. This is due to the usage of the generalized iterator. Also simplifying the code with ternary operators. But, I believe my take on this new snippet would definitely be faster, and also educate other scripters better, even spiking their curiosity in code simplification if not familiar with ternary operators. All in short, I believe It's a great take.

https://create.roblox.com/docs/luau/tables#deep-clones
https://devforum.roblox.com/t/ternary-like-operation-in-lua/834254

Checks

By submitting your pull request for review, you agree to the following:

  • This contribution was created in whole or in part by me, and I have the right to submit it under the terms of this repository's open source licenses.
  • I understand and agree that this contribution and a record of it are public, maintained indefinitely, and may be redistributed under the terms of this repository's open source licenses.
  • To the best of my knowledge, all proposed changes are accurate.

I've made a bit more documented of a code snippet to help scripters navigate their way through it, considering not everyone viewing would be experienced. This take on the deep clone snippet also gives a faster result, meaning the code is ran faster and you get your value returned faster.

OLD: **0.0000051**
NEW: **0.0000034**

The difference may be small but definitely in some cases noticeable and useful. This is due to the usage of the generalized iterator. Also simplifying the code with ternary operators. But, I believe my take on this new snippet would definitely be faster, and also educate other scripters better, even spiking their curiosity in code simplification if not familiar with ternary operators. All in short, I believe It's a great take.
@EnumEnv EnumEnv requested a review from a team as a code owner September 28, 2024 20:10
@github-actions github-actions bot added the engine guides Changes the Engine guides label Sep 28, 2024
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @EnumEnv, thanks so much for helping improve the Roblox creator documentation! Our technical writing team will review your pull request soon. In the meantime, please ensure you've read through the README.md, contribution guidelines, and style recommendations.

@metatablecat
Copy link
Contributor

metatablecat commented Sep 30, 2024

table.clone would like to have a word with you about performance (unsure if its faster since you still have to iterate the table for tables)

@IgnisRBX
Copy link
Contributor

IgnisRBX commented Oct 1, 2024

Hi @EnumEnv , in your version, where does MY_deepCopy come into play?

@EnumEnv
Copy link
Contributor Author

EnumEnv commented Oct 2, 2024

Hi @EnumEnv , in your version, where does MY_deepCopy come into play?
@IgnisRBX
Oh, my bad, that's actually a test thing I had set up before for recursion, was using 2 functions for comparing and well forgot to rename. I'll just change it back to the function's name "deepCopy". That's my bad

@metatablecat
Copy link
Contributor

metatablecat commented Oct 3, 2024

Using generalised iteration (for k, v in o do) and shorthand if statements is faster, my table.clone idea is a lot slower lol.

image

The code is provided within the image. Test sample is a table with 10,000 entries with empty tables randomly intersperced.

@EnumEnv
Copy link
Contributor Author

EnumEnv commented Oct 5, 2024

@metatablecat The reason the 'your solution' one is slower is because you used "pairs", which I initially didn't use at all, I used the generalized iteration. So, that's the reason of it slowing down. Generally it should be faster.

@EnumEnv
Copy link
Contributor Author

EnumEnv commented Oct 5, 2024

Waiting on another review. Tested everything and re-benchmarked.

@IgnisRBX
Copy link
Contributor

IgnisRBX commented Oct 7, 2024

@EnumEnv looks good now! I just did minor edits to the comment lines (no ending periods as per our typical style, etc.)

@IgnisRBX IgnisRBX merged commit 3ff4d1d into Roblox:main Oct 7, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine guides Changes the Engine guides
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants