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

Fast React ID Generator #528

Merged
merged 3 commits into from
Apr 12, 2018

Conversation

DaniilSokolyuk
Copy link
Contributor

Fast React ID Generator

Copy link
Member

@Daniel15 Daniel15 left a comment

Choose a reason for hiding this comment

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

This seems OK to me. @dustinsoftware what do you think?

var id = Interlocked.Increment(ref _random);

//reactPrefix.Length == 6
chars[6] = _encode32Chars[(int)(id >> 60) & 31];
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot of magic numbers in this code. It'd be good to pull these out as constants and add comments explaining what they're doing.

/// <summary>
/// Extension methods relating to GUIDs.
/// </summary>
public static class ReactIdGenerator
Copy link
Member

Choose a reason for hiding this comment

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

Would be good to add an interface for this, and use the dependency injection framework to inject it into ReactComponent. That way, anyone can swap out the implementation if they want to (eg. if they want the old one back).

Copy link
Member

Choose a reason for hiding this comment

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

(see AssemblyRegistration.cs, use .AsSingleton())

Copy link
Contributor Author

@DaniilSokolyuk DaniilSokolyuk Apr 9, 2018

Choose a reason for hiding this comment

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

Nice idea, i can inject it to ReactEnvironment and create property IReactIdGenerator (like IBabel) and call it from constructor in ReactComponent like "environment.ReactIdGenerator.Generate(...)"

var chars = _chars;
if (chars == null)
{
_chars = chars = new char[19];
Copy link
Member

Choose a reason for hiding this comment

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

Why 19?

chars[17] = _encode32Chars[(int)(id >> 5) & 31];
chars[18] = _encode32Chars[(int)id & 31];

return new string(chars, 0, 19);
Copy link
Member

Choose a reason for hiding this comment

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

Pull out 19 as a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok!, const will be inlined without overhead 👍

@Daniel15
Copy link
Member

Daniel15 commented Apr 9, 2018 via email

@DaniilSokolyuk
Copy link
Contributor Author

Completed.
IReactIdGenerator injected to Environment and passed to Component.
GeneratesContainerIdIfNotProvided test deleted because UsesProvidedContainerId is very similar to him

Copy link
Member

@dustinsoftware dustinsoftware left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good.

@dustinsoftware dustinsoftware merged commit d49bb11 into reactjs:master Apr 12, 2018
{
public class GuidExtensionsTests
public interface IReactIdGenerator
Copy link
Member

Choose a reason for hiding this comment

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

Ugh, sorry. Missed this one. There is no XML comment for this type which is generating a warning at build time.

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

Successfully merging this pull request may close these issues.

None yet

3 participants