-
Notifications
You must be signed in to change notification settings - Fork 913
Fast React ID Generator #528
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright (c) 2016-Present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
|
|
||
| namespace React | ||
| { | ||
| /// <summary> | ||
| /// Extension methods relating to GUIDs. | ||
| /// </summary> | ||
| public static class ReactIdGenerator | ||
| { | ||
| private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; | ||
|
|
||
| private static long _random = DateTime.UtcNow.Ticks; | ||
|
|
||
| private static readonly char[] reactPrefix = "react_".ToCharArray(); | ||
|
|
||
| [ThreadStatic] | ||
| private static char[] _chars; | ||
|
|
||
| /// <summary> | ||
| /// Returns a short react identifier starts with "react_". | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public static string Generate() | ||
| { | ||
| var chars = _chars; | ||
| if (chars == null) | ||
| { | ||
| _chars = chars = new char[19]; | ||
|
||
| Array.Copy(reactPrefix, 0, chars, 0, reactPrefix.Length); | ||
| } | ||
|
|
||
| var id = Interlocked.Increment(ref _random); | ||
|
|
||
| //reactPrefix.Length == 6 | ||
| chars[6] = _encode32Chars[(int)(id >> 60) & 31]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| chars[7] = _encode32Chars[(int)(id >> 55) & 31]; | ||
| chars[8] = _encode32Chars[(int)(id >> 50) & 31]; | ||
| chars[9] = _encode32Chars[(int)(id >> 45) & 31]; | ||
| chars[10] = _encode32Chars[(int)(id >> 40) & 31]; | ||
| chars[11] = _encode32Chars[(int)(id >> 35) & 31]; | ||
| chars[12] = _encode32Chars[(int)(id >> 30) & 31]; | ||
| chars[13] = _encode32Chars[(int)(id >> 25) & 31]; | ||
| chars[14] = _encode32Chars[(int)(id >> 20) & 31]; | ||
| chars[15] = _encode32Chars[(int)(id >> 15) & 31]; | ||
| chars[16] = _encode32Chars[(int)(id >> 10) & 31]; | ||
| chars[17] = _encode32Chars[(int)(id >> 5) & 31]; | ||
| chars[18] = _encode32Chars[(int)id & 31]; | ||
|
|
||
| return new string(chars, 0, 19); | ||
|
||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright (c) 2016-Present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| */ | ||
|
|
||
| using System; | ||
| using Xunit; | ||
|
|
||
| namespace React.Tests.Core | ||
| { | ||
| public class ReactIdGeneratorTests | ||
| { | ||
| [Fact] | ||
| public void GuidShouldHaveFixedRange() | ||
| { | ||
| var shortGuid = ReactIdGenerator.Generate(); | ||
|
|
||
| Assert.Equal(19, shortGuid.Length); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldStartsWithReact() | ||
| { | ||
| var shortGuid = ReactIdGenerator.Generate(); | ||
|
|
||
| Assert.StartsWith("react_", shortGuid); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TwoGuidsShouldNotEqual() | ||
| { | ||
| var shortGuid1 = ReactIdGenerator.Generate(); | ||
| var shortGuid2 = ReactIdGenerator.Generate(); | ||
|
|
||
| Assert.NotEqual(shortGuid1, shortGuid2); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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())Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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(...)"