Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 53 additions & 50 deletions src/Npgsql/NpgsqlTypes/NpgsqlTsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,27 @@ public static NpgsqlTsQuery Parse(string value)
if (pos >= value.Length)
goto Finish;
ch = value[pos];
if (ch == '*')
switch (ch)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this hot enough to

Suggested change
switch (ch)
switch (ch | 0x20)

so to have the cases only for lowercase chars, thus saving some branches and code-size?
See sharplab

Copy link
Member Author

Choose a reason for hiding this comment

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

I definitely don't think this code is hot enough for micro-optimization.. In fact, we should consider removing the parsing logic altogether, as this PR does for the geometry types.

{
case '*':
((NpgsqlTsQueryLexeme)valStack.Peek()).IsPrefixSearch = true;
else if (ch == 'a' || ch == 'A')
break;
case 'a' or 'A':
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.A;
else if (ch == 'b' || ch == 'B')
break;
case 'b' or 'B':
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.B;
else if (ch == 'c' || ch == 'C')
break;
case 'c' or 'C':
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.C;
else if (ch == 'd' || ch == 'D')
break;
case 'd' or 'D':
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.D;
else
break;
default:
goto PushedVal;
}

pos++;
goto InWeightInfo;

Expand Down Expand Up @@ -338,12 +347,12 @@ public static NpgsqlTsQuery Parse(string value)
}

/// <inheritdoc/>
public override int GetHashCode() =>
throw new NotImplementedException();
public override int GetHashCode()
=> throw new NotSupportedException("Must be overridden");

/// <inheritdoc/>
public override bool Equals(object? obj) =>
obj is NpgsqlTsQuery query && query.Equals(this);
public override bool Equals(object? obj)
=> obj is NpgsqlTsQuery query && query.Equals(this);

/// <summary>
/// Returns a value indicating whether this instance and a specified <see cref="NpgsqlTsQuery"/> object represent the same value.
Expand All @@ -358,18 +367,17 @@ public override bool Equals(object? obj) =>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> and <paramref name="right"/> are equal; otherwise, <see langword="false"/>.</returns>
public static bool operator ==(NpgsqlTsQuery? left, NpgsqlTsQuery? right) =>
left is null ? right is null : left.Equals(right);

public static bool operator ==(NpgsqlTsQuery? left, NpgsqlTsQuery? right)
=> left is null ? right is null : left.Equals(right);

/// <summary>
/// Indicates whether the values of two specified <see cref="NpgsqlTsQuery"/> objects are not equal.
/// </summary>
/// <param name="left">The first object to compare.</param>
/// <param name="right">The second object to compare.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> and <paramref name="right"/> are not equal; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(NpgsqlTsQuery? left, NpgsqlTsQuery? right) =>
left is null ? right is not null : !left.Equals(right);
public static bool operator !=(NpgsqlTsQuery? left, NpgsqlTsQuery? right)
=> left is null ? right is not null : !left.Equals(right);
}

readonly struct NpgsqlTsQueryOperator
Expand Down Expand Up @@ -506,15 +514,15 @@ internal override void WriteCore(StringBuilder sb, bool first = false)
}

/// <inheritdoc/>
public override bool Equals(NpgsqlTsQuery? other) =>
other is NpgsqlTsQueryLexeme lexeme &&
lexeme.Text == Text &&
lexeme.Weights == Weights &&
lexeme.IsPrefixSearch == IsPrefixSearch;
public override bool Equals(NpgsqlTsQuery? other)
=> other is NpgsqlTsQueryLexeme lexeme &&
lexeme.Text == Text &&
lexeme.Weights == Weights &&
lexeme.IsPrefixSearch == IsPrefixSearch;

/// <inheritdoc/>
public override int GetHashCode() =>
HashCode.Combine(Text, Weights, IsPrefixSearch);
public override int GetHashCode()
=> HashCode.Combine(Text, Weights, IsPrefixSearch);
}

/// <summary>
Expand Down Expand Up @@ -555,13 +563,12 @@ internal override void WriteCore(StringBuilder sb, bool first = false)
}

/// <inheritdoc/>
public override bool Equals(NpgsqlTsQuery? other) =>
other is NpgsqlTsQueryNot not &&
not.Child == Child;
public override bool Equals(NpgsqlTsQuery? other)
=> other is NpgsqlTsQueryNot not && not.Child == Child;

/// <inheritdoc/>
public override int GetHashCode() =>
Child?.GetHashCode() ?? 0;
public override int GetHashCode()
=> Child?.GetHashCode() ?? 0;
}

/// <summary>
Expand Down Expand Up @@ -611,14 +618,12 @@ internal override void WriteCore(StringBuilder sb, bool first = false)
}

/// <inheritdoc/>
public override bool Equals(NpgsqlTsQuery? other) =>
other is NpgsqlTsQueryAnd and &&
and.Left == Left &&
and.Right == Right;
public override bool Equals(NpgsqlTsQuery? other)
=> other is NpgsqlTsQueryAnd and && and.Left == Left && and.Right == Right;

/// <inheritdoc/>
public override int GetHashCode() =>
HashCode.Combine(Left, Right);
public override int GetHashCode()
=> HashCode.Combine(Left, Right);
}

/// <summary>
Expand Down Expand Up @@ -649,14 +654,12 @@ internal override void WriteCore(StringBuilder sb, bool first = false)
}

/// <inheritdoc/>
public override bool Equals(NpgsqlTsQuery? other) =>
other is NpgsqlTsQueryOr or &&
or.Left == Left &&
or.Right == Right;
public override bool Equals(NpgsqlTsQuery? other)
=> other is NpgsqlTsQueryOr or && or.Left == Left && or.Right == Right;

/// <inheritdoc/>
public override int GetHashCode() =>
HashCode.Combine(Left, Right);
public override int GetHashCode()
=> HashCode.Combine(Left, Right);
}

/// <summary>
Expand Down Expand Up @@ -708,15 +711,15 @@ internal override void WriteCore(StringBuilder sb, bool first = false)
}

/// <inheritdoc/>
public override bool Equals(NpgsqlTsQuery? other) =>
other is NpgsqlTsQueryFollowedBy followedBy &&
followedBy.Left == Left &&
followedBy.Right == Right &&
followedBy.Distance == Distance;
public override bool Equals(NpgsqlTsQuery? other)
=> other is NpgsqlTsQueryFollowedBy followedBy &&
followedBy.Left == Left &&
followedBy.Right == Right &&
followedBy.Distance == Distance;

/// <inheritdoc/>
public override int GetHashCode() =>
HashCode.Combine(Left, Right, Distance);
public override int GetHashCode()
=> HashCode.Combine(Left, Right, Distance);
}

/// <summary>
Expand All @@ -732,10 +735,10 @@ public NpgsqlTsQueryEmpty() : base(NodeKind.Empty) {}
internal override void WriteCore(StringBuilder sb, bool first = false) { }

/// <inheritdoc/>
public override bool Equals(NpgsqlTsQuery? other) =>
other is NpgsqlTsQueryEmpty;
public override bool Equals(NpgsqlTsQuery? other)
=> other is NpgsqlTsQueryEmpty;

/// <inheritdoc/>
public override int GetHashCode() =>
Kind.GetHashCode();
public override int GetHashCode()
=> Kind.GetHashCode();
}
Loading