Browse Source

codec: add bitset32|256.check so we don't double-compare

Previously, we would check if != 0 in isset, and then
call !isset in calling code.

Now, we just check if == 0 instead directly in calling code.
Ugorji Nwoke 6 years ago
parent
commit
4751785f62
2 changed files with 18 additions and 5 deletions
  1. 12 2
      codec/helper.go
  2. 6 3
      codec/reader.go

+ 12 - 2
codec/helper.go

@@ -2581,8 +2581,13 @@ func (s *set) remove(v interface{}) (exists bool) {
 
 type bitset256 [32]byte
 
+func (x *bitset256) check(pos byte) uint8 {
+	return x[pos>>3] & (1 << (pos & 7))
+}
+
 func (x *bitset256) isset(pos byte) bool {
-	return x[pos>>3]&(1<<(pos&7)) != 0
+	return x.check(pos) != 0
+	// return x[pos>>3]&(1<<(pos&7)) != 0
 }
 
 // func (x *bitset256) issetv(pos byte) byte {
@@ -2598,8 +2603,13 @@ type bitset32 uint32
 func (x bitset32) set(pos byte) bitset32 {
 	return x | (1 << pos)
 }
+
+func (x bitset32) check(pos byte) uint32 {
+	return uint32(x) & (1 << pos)
+}
 func (x bitset32) isset(pos byte) bool {
-	return x&(1<<pos) != 0
+	return x.check(pos) != 0
+	// return x&(1<<pos) != 0
 }
 
 // func (x *bitset256) unset(pos byte) {

+ 6 - 3
codec/reader.go

@@ -555,7 +555,8 @@ func (z *bufioDecReader) skipFill(accept *bitset256) (token byte) {
 		}
 		z.buf = z.buf[:n2]
 		for i, token = range z.buf {
-			if !accept.isset(token) {
+			// if !accept.isset(token) {
+			if accept.check(token) == 0 {
 				z.n += (uint(i) - z.c) - 1
 				z.loopFn(uint(i + 1))
 				return
@@ -606,7 +607,8 @@ func (z *bufioDecReader) readTo(accept *bitset256) (out []byte) {
 	i := z.c
 LOOP:
 	if i < uint(len(z.buf)) {
-		if !accept.isset(z.buf[i]) {
+		// if !accept.isset(z.buf[i]) {
+		if accept.check(z.buf[i]) == 0 {
 			// return z.readToLoopFn(i, nil)
 			// inline readToLoopFn here (for performance)
 			z.n += (i - z.c) - 1
@@ -644,7 +646,8 @@ func (z *bufioDecReader) readToFill(accept *bitset256) []byte {
 		}
 		z.buf = z.buf[:n2]
 		for i, token := range z.buf {
-			if !accept.isset(token) {
+			// if !accept.isset(token) {
+			if accept.check(token) == 0 {
 				z.n += (uint(i) - z.c) - 1
 				z.bufr = append(z.bufr, z.buf[z.c:i]...)
 				z.loopFn(uint(i))