Commit 6941ba7
committed
[STJ] Only compute PopCount once when topologically sorting Enums
Packs the calculated (once per entry) PopCount into the high 32 bits of the long and the original index into the low 32 bits. Then that can just be sorted using the heavily optimized Array.Sort() method.
After sorting just extract the low 32 bits as the original array index. As before, we negate the actual _PopCount_ to ensure that `Key`s with more on-bits (e.g. more flags represented) will sort **first**.
This trades 2 x O(N log N) [average case] to 2 x O(N^2) [worst case] calls to the `popcount` instruction (or the emulation if NET is not defined) for N **shift-left-32** and **or** + N x **truncate to 32 bits**. It _also_ eliminates the overhead of the `CompareTo` method as it's now a direct `long` low-level compare.1 parent 2fe0457 commit 6941ba7
File tree
1 file changed
+22
-23
lines changed- src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value
1 file changed
+22
-23
lines changedLines changed: 22 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
676 | 676 | | |
677 | 677 | | |
678 | 678 | | |
679 | | - | |
| 679 | + | |
680 | 680 | | |
681 | 681 | | |
682 | | - | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
683 | 687 | | |
684 | 688 | | |
685 | | - | |
| 689 | + | |
686 | 690 | | |
687 | 691 | | |
688 | 692 | | |
689 | 693 | | |
690 | | - | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
691 | 698 | | |
692 | 699 | | |
693 | 700 | | |
| 701 | + | |
694 | 702 | | |
695 | | - | |
696 | | - | |
697 | | - | |
698 | | - | |
699 | | - | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
| 703 | + | |
| 704 | + | |
705 | 705 | | |
706 | | - | |
| 706 | + | |
707 | 707 | | |
708 | | - | |
709 | | - | |
710 | | - | |
711 | | - | |
712 | | - | |
713 | | - | |
714 | | - | |
715 | | - | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
716 | 713 | | |
| 714 | + | |
| 715 | + | |
717 | 716 | | |
718 | 717 | | |
719 | 718 | | |
| |||
0 commit comments