| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Metadata |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a UTF-8 encoded JSON property name and its associated <see cref="JsonPropertyInfo"/>, if available. |
| | | 12 | | /// PropertyRefs use byte sequence equality, so equal JSON strings with alternate encodings or casings are not equal |
| | | 13 | | /// Used as a first-level cache for property lookups before falling back to UTF decoding and string comparison. |
| | | 14 | | /// </summary> |
| | | 15 | | internal readonly struct PropertyRef(ulong key, JsonPropertyInfo? info, byte[] utf8PropertyName) : IEquatable<Proper |
| | | 16 | | { |
| | | 17 | | // The length of the property name embedded in the key (in bytes). |
| | | 18 | | // The key is a ulong (8 bytes) containing the first 7 bytes of the property name |
| | | 19 | | // followed by a byte representing the length. |
| | | 20 | | private const int PropertyNameKeyLength = 7; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// A custom hashcode produced from the UTF-8 encoded property name. |
| | | 24 | | /// </summary> |
| | 0 | 25 | | public readonly ulong Key = key; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The <see cref="JsonPropertyInfo"/> associated with the property name, if available. |
| | | 29 | | /// </summary> |
| | 0 | 30 | | public readonly JsonPropertyInfo? Info = info; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Caches a heap allocated copy of the UTF-8 encoded property name. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | public readonly byte[] Utf8PropertyName = utf8PropertyName; |
| | | 36 | | |
| | 0 | 37 | | public bool Equals(PropertyRef other) => Equals(other.Utf8PropertyName, other.Key); |
| | 0 | 38 | | public override bool Equals(object? obj) => obj is PropertyRef other && Equals(other); |
| | 0 | 39 | | public override int GetHashCode() => Key.GetHashCode(); |
| | | 40 | | |
| | | 41 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 42 | | public bool Equals(ReadOnlySpan<byte> propertyName, ulong key) |
| | 0 | 43 | | { |
| | | 44 | | // If the property name is less than 8 bytes, it is embedded in the key so no further comparison is necessar |
| | 0 | 45 | | return key == Key && (propertyName.Length <= PropertyNameKeyLength || propertyName.SequenceEqual(Utf8Propert |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Get a key from the property name. |
| | | 50 | | /// The key consists of the first 7 bytes of the property name and then the least significant bits of the length |
| | | 51 | | /// </summary> |
| | | 52 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 53 | | public static ulong GetKey(ReadOnlySpan<byte> name) |
| | 0 | 54 | | { |
| | 0 | 55 | | int length = name.Length; |
| | 0 | 56 | | ulong key = (ulong)(byte)length << 56; |
| | 0 | 57 | | key |= length switch |
| | 0 | 58 | | { |
| | 0 | 59 | | 0 => 0, |
| | 0 | 60 | | 1 => name[0], |
| | 0 | 61 | | 2 => MemoryMarshal.Read<ushort>(name), |
| | 0 | 62 | | 3 => MemoryMarshal.Read<ushort>(name) | ((ulong)name[2] << 16), |
| | 0 | 63 | | 4 => MemoryMarshal.Read<uint>(name), |
| | 0 | 64 | | 5 => MemoryMarshal.Read<uint>(name) | ((ulong)name[4] << 32), |
| | 0 | 65 | | 6 => MemoryMarshal.Read<uint>(name) | ((ulong)MemoryMarshal.Read<ushort>(name.Slice(4, 2)) << 32), |
| | 0 | 66 | | 7 => MemoryMarshal.Read<uint>(name) | ((ulong)MemoryMarshal.Read<ushort>(name.Slice(4, 2)) << 32) | ((ul |
| | 0 | 67 | | _ => MemoryMarshal.Read<ulong>(name) & 0x00ffffffffffffffUL |
| | 0 | 68 | | }; |
| | | 69 | | #if DEBUG |
| | | 70 | | // Verify key contains the embedded bytes as expected. |
| | | 71 | | // Note: the expected properties do not hold true on big-endian platforms |
| | 0 | 72 | | if (BitConverter.IsLittleEndian) |
| | 0 | 73 | | { |
| | | 74 | | const int BitsInByte = 8; |
| | 0 | 75 | | Debug.Assert( |
| | 0 | 76 | | // Verify embedded property name. |
| | 0 | 77 | | (name.Length < 1 || name[0] == ((key & ((ulong)0xFF << BitsInByte * 0)) >> BitsInByte * 0)) && |
| | 0 | 78 | | (name.Length < 2 || name[1] == ((key & ((ulong)0xFF << BitsInByte * 1)) >> BitsInByte * 1)) && |
| | 0 | 79 | | (name.Length < 3 || name[2] == ((key & ((ulong)0xFF << BitsInByte * 2)) >> BitsInByte * 2)) && |
| | 0 | 80 | | (name.Length < 4 || name[3] == ((key & ((ulong)0xFF << BitsInByte * 3)) >> BitsInByte * 3)) && |
| | 0 | 81 | | (name.Length < 5 || name[4] == ((key & ((ulong)0xFF << BitsInByte * 4)) >> BitsInByte * 4)) && |
| | 0 | 82 | | (name.Length < 6 || name[5] == ((key & ((ulong)0xFF << BitsInByte * 5)) >> BitsInByte * 5)) && |
| | 0 | 83 | | (name.Length < 7 || name[6] == ((key & ((ulong)0xFF << BitsInByte * 6)) >> BitsInByte * 6)) && |
| | 0 | 84 | | // Verify embedded length. |
| | 0 | 85 | | (key & ((ulong)0xFF << BitsInByte * 7)) >> BitsInByte * 7 == (byte)name.Length, |
| | 0 | 86 | | "Embedded bytes not as expected"); |
| | 0 | 87 | | } |
| | | 88 | | #endif |
| | 0 | 89 | | return key; |
| | 0 | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |