-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: matchSet.addX - add length check and option for "update-in-place"
- addX: adding length check to avoid copying the map if nothing was changed. - Introducing addXSingleThreaded which allows update-in-place in single-threaded usages. ``` name \ time/op benchs/1_baseline benchs/2_len_check benchs/3_thread_safe_add benchs/4_thread_safe_len_check _Quamina-10 48.0µs ± 0% 33.9µs ± 0% 6.6µs ± 0% 6.8µs ± 0% name \ alloc/op benchs/1_baseline benchs/2_len_check benchs/3_thread_safe_add benchs/4_thread_safe_len_check _Quamina-10 41.0kB ± 0% 30.4kB ± 0% 8.9kB ± 0% 8.9kB ± 0% name \ allocs/op benchs/1_baseline benchs/2_len_check benchs/3_thread_safe_add benchs/4_thread_safe_len_check _Quamina-10 161 ± 0% 129 ± 0% 28 ± 0% 28 ± 0% ``` * `benchs/1_baseline` - baseline * `benchs/2_len_check` - adding early returrn with length check to `addX` (still being used in matching) * `benchs/3_thread_safe_add` - using `addXSingleThreaded` in matching * `benchs/4_thread_safe_len_check` - adding early return with length check to `addXSingleThreaded` .. didn't improved the situation, which makes sense in this case.
- Loading branch information
Showing
3 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package quamina | ||
|
||
import "testing" | ||
|
||
func TestAddX(t *testing.T) { | ||
set := newMatchSet() | ||
|
||
// empty exes | ||
set = set.addX() | ||
if !isSameMatches(set) { | ||
t.Errorf("Expected matches to be empty: %+v", set.matches()) | ||
} | ||
|
||
newSet := set.addX(1) | ||
// existing set should be empty. | ||
if len(set.matches()) > 0 { | ||
t.Errorf("Expected matches to be empty: %+v", set.matches()) | ||
} | ||
if !isSameMatches(newSet, 1) { | ||
t.Errorf("Expected matches to be [1]: %+v", set.matches()) | ||
} | ||
|
||
// add another two values | ||
newSet = newSet.addX(2) | ||
newSet = newSet.addX(3) | ||
if !isSameMatches(newSet, 1, 2, 3) { | ||
t.Errorf("Expected matches to be [1, 2, 3]: %+v", set.matches()) | ||
} | ||
} | ||
|
||
func TestAddXSingleThreaded(t *testing.T) { | ||
set := newMatchSet() | ||
|
||
// empty exes | ||
set.addXSingleThreaded() | ||
if !isSameMatches(set) { | ||
t.Errorf("Expected matches to be empty: %+v", set.matches()) | ||
} | ||
|
||
set.addXSingleThreaded(1) | ||
// existing set should be empty. | ||
if !isSameMatches(set, 1) { | ||
t.Errorf("Expected matches to be [1]: %+v", set.matches()) | ||
} | ||
|
||
// add another two values | ||
set.addXSingleThreaded(2) | ||
set.addXSingleThreaded(3) | ||
if !isSameMatches(set, 1, 2, 3) { | ||
t.Errorf("Expected matches to be [1, 2, 3]: %+v", set.matches()) | ||
} | ||
} | ||
|
||
func isSameMatches(matchSet *matchSet, exes ...X) bool { | ||
if len(exes) == 0 && len(matchSet.matches()) == 0 { | ||
return true | ||
} | ||
|
||
if len(exes) != len(matchSet.matches()) { | ||
return false | ||
} | ||
|
||
for _, x := range exes { | ||
if !matchSet.contains(x) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |