-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Goのシリアライザの32bit対応 #77
Conversation
server/binary/marshal.go
Outdated
i := int64(src[0]) << 24 | ||
i += int64(src[1]) << 16 | ||
i += int64(src[2]) << 8 | ||
i += int64(src[3]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(今回の変更点ではない部分へのコメント。修正するかは任せます)
get32()の定義の上に空行が入ってなくてもフォーマッターに怒られないのに驚きました。
でも、put24/get24の間には空行が入っているので、ここにも空行を挟んだ方がいい気がします。
+= よりも |= の方が最適化がかかる可能性が高い(コンパイラがシャッフル系の命令を使える時に使ってもらいやすい)のと、 src[3]
から順にアクセスする方が bounds check が一回で済むので微妙に速く小さくなります。
参考に、似た処理をしている標準ライブラリのコードを貼っておきます。
func (littleEndian) Uint64(b []byte) uint64 {
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
別PRにするとコンフリクトするので、このPRでやってしまいます。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご対応ありがとうございます 👍 👍 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
24bit以外のコードは encoding/binary を使えそうです。
server/binary/marshal.go
Outdated
dst[0] = byte((val & 0xff000000) >> 24) | ||
dst[1] = byte((val & 0xff0000) >> 16) | ||
dst[2] = byte((val & 0xff00) >> 8) | ||
dst[3] = byte(val & 0xff) | ||
dst[2] = byte((val & 0xff00) >> 8) | ||
dst[1] = byte((val & 0xff0000) >> 16) | ||
dst[0] = byte((val & 0xff000000) >> 24) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
binary.BigEndian.PutUint32(dst, uint32(val))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最適化を標準ライブラリに任せたほうがいいですねたしかに。
server/binary/marshal.go
Outdated
i := int64(src[3]) | ||
i |= int64(src[2]) << 8 | ||
i |= int64(src[1]) << 16 | ||
i |= int64(src[0]) << 24 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return int64(binary.BigEndian.Uint32(src))
Goのシリアライザは64bit環境前提で作っていました(符号有/無の32bit intをどちらも
int
で扱っていた)ところが、dashboardで使っているGopherjsが32bit環境をエミュレートしているため、桁溢れするときに正しく扱えていませんでした。
32bit環境でも変わらないよう、Int/UIntはint64で扱うようにして、テストも32bitで通るようにしました。
Unmarshalの結果が
interface{}
で、一部型アサーションしてる部分があり、この書きかえがコンパイル時チェックできないので、漏れがないか注意して見てほしいです。