mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
This implements new version 2 filters which have 4 changes as compared to version 1 filters: - Support for independently specifying the false positive rate and Golomb coding bin size which allows minimizing the filter size - A faster (incompatible with version 1) reduction function - A more compact serialization for the number of members in the set - Deduplication of all hash collisions prior to reducing and serializing the deltas In addition, it adds a full set of tests and updates the benchmarks to use the new version 2 filters. The primary motivating factor for these changes is the ability to minimize the size of the filters, however, the following is a before and after comparison of version 1 and 2 filters in terms of performance and allocations. It is interesting to note the results for attempting to match a single item is not very representative due to the fact the actual hash value itself dominates to the point it can significantly vary due to the very low ns timings involved. Those differences average out when matching multiple items, which is the much more realistic scenario, and the performance increase is in line with the expected values. It is also worth nothing that filter construction now takes a bit longer due to the additional deduplication step. While the performance numbers for filter construction are about 25% larger in relative terms, it is only a few ms difference in practice and therefore is an acceptable trade off for the size savings provided. benchmark old ns/op new ns/op delta ----------------------------------------------------------------- BenchmarkFilterBuild50000 16194920 20279043 +25.22% BenchmarkFilterBuild100000 32609930 41629998 +27.66% BenchmarkFilterMatch 620 593 -4.35% BenchmarkFilterMatchAny 2687 2302 -14.33% benchmark old allocs new allocs delta ----------------------------------------------------------------- BenchmarkFilterBuild50000 6 17 +183.33% BenchmarkFilterBuild100000 6 18 +200.00% BenchmarkFilterMatch 0 0 +0.00% BenchmarkFilterMatchAny 0 0 +0.00% benchmark old bytes new bytes delta ----------------------------------------------------------------- BenchmarkFilterBuild50000 688366 2074653 +201.39% BenchmarkFilterBuild100000 1360064 4132627 +203.86% BenchmarkFilterMatch 0 0 +0.00% BenchmarkFilterMatchAny 0 0 +0.00%
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
// Copyright (c) 2019 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gcs
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// ErrorCode identifies a kind of error.
|
|
type ErrorCode int
|
|
|
|
// These constants are used to identify a specific RuleError.
|
|
const (
|
|
// ErrNTooBig signifies that the filter can't handle N items.
|
|
ErrNTooBig ErrorCode = iota
|
|
|
|
// ErrPTooBig signifies that the filter can't handle `1/2**P`
|
|
// collision probability.
|
|
ErrPTooBig
|
|
|
|
// ErrBTooBig signifies that the provided Golomb coding bin size is larger
|
|
// than the maximum allowed value.
|
|
ErrBTooBig
|
|
|
|
// ErrMisserialized signifies a filter was misserialized and is missing the
|
|
// N and/or P parameters of a serialized filter.
|
|
ErrMisserialized
|
|
|
|
// numErrorCodes is the maximum error code number used in tests.
|
|
numErrorCodes
|
|
)
|
|
|
|
// Map of ErrorCode values back to their constant names for pretty printing.
|
|
var errorCodeStrings = map[ErrorCode]string{
|
|
ErrNTooBig: "ErrNTooBig",
|
|
ErrPTooBig: "ErrPTooBig",
|
|
ErrBTooBig: "ErrBTooBig",
|
|
ErrMisserialized: "ErrMisserialized",
|
|
}
|
|
|
|
// String returns the ErrorCode as a human-readable name.
|
|
func (e ErrorCode) String() string {
|
|
if s := errorCodeStrings[e]; s != "" {
|
|
return s
|
|
}
|
|
return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
|
|
}
|
|
|
|
// Error identifies a filter-related error. The caller can use type assertions
|
|
// to access the ErrorCode field to ascertain the specific reason for the
|
|
// failure.
|
|
type Error struct {
|
|
ErrorCode ErrorCode // Describes the kind of error
|
|
Description string // Human readable description of the issue
|
|
}
|
|
|
|
// Error satisfies the error interface and prints human-readable errors.
|
|
func (e Error) Error() string {
|
|
return e.Description
|
|
}
|
|
|
|
// makeError creates an Error given a set of arguments. The error code must
|
|
// be one of the error codes provided by this package.
|
|
func makeError(c ErrorCode, desc string) Error {
|
|
return Error{ErrorCode: c, Description: desc}
|
|
}
|
|
|
|
// IsError returns whether err is an Error with a matching error code.
|
|
func IsErrorCode(err error, c ErrorCode) bool {
|
|
e, ok := err.(Error)
|
|
return ok && e.ErrorCode == c
|
|
}
|