Skip to content

Fast React ID Generator#528

Merged
dustinsoftware merged 3 commits into
reactjs:masterfrom
DaniilSokolyuk:fast-id-generator
Apr 12, 2018
Merged

Fast React ID Generator#528
dustinsoftware merged 3 commits into
reactjs:masterfrom
DaniilSokolyuk:fast-id-generator

Conversation

@DaniilSokolyuk

Copy link
Copy Markdown
Contributor

Fast React ID Generator

@Daniel15 Daniel15 left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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.

Comment thread src/React.Core/ReactIdGenerator.cs Outdated
/// <summary>
/// Extension methods relating to GUIDs.
/// </summary>
public static class ReactIdGenerator

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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())

@DaniilSokolyuk DaniilSokolyuk Apr 9, 2018

Copy link
Copy Markdown
Contributor Author

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(...)"

Comment thread src/React.Core/ReactIdGenerator.cs Outdated
var chars = _chars;
if (chars == null)
{
_chars = chars = new char[19];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why 19?

Comment thread src/React.Core/ReactIdGenerator.cs Outdated
chars[17] = _encode32Chars[(int)(id >> 5) & 31];
chars[18] = _encode32Chars[(int)id & 31];

return new string(chars, 0, 19);

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

Daniel15 commented Apr 9, 2018 via email

Copy link
Copy Markdown
Contributor

Daniil Sokolyuk added 2 commits April 9, 2018 20:05
@DaniilSokolyuk

Copy link
Copy Markdown
Contributor Author

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

@dustinsoftware dustinsoftware left a comment

Copy link
Copy Markdown
Contributor

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
namespace React
{
public class GuidExtensionsTests
public interface IReactIdGenerator

Copy link
Copy Markdown
Contributor

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.

3 participants