Skip to content

Commit

Permalink
cty: Optimize equality check for numbers of differing signs
Browse files Browse the repository at this point in the history
This improves performance when comparing large numbers to zero, along
with comparing large numbers of different signs. Without this special case,
the numbers are both converted to text, and string compared, which is
costly if the numbers are large.

This provides a cheap early-exit path if the signs don't match,
speeding up the cost of comparison to zero. As part of this, the .Modulo
function calling .RawEquals should also have significantly improved
performance (which was otherwise an expensive check for large numbers).
  • Loading branch information
jedevc committed Jun 22, 2022
1 parent b66d00a commit 3c2818f
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cty/primitive_type.go
Expand Up @@ -74,6 +74,8 @@ func rawNumberEqual(a, b *big.Float) bool {
return false
case a == nil: // b == nil too then, due to previous case
return true
case a.Sign() != b.Sign():
return false
default:
// This format and precision matches that used by cty/json.Marshal,
// and thus achieves our definition of "two numbers are equal if
Expand Down

0 comments on commit 3c2818f

Please sign in to comment.