Skip to content
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

Merged
merged 6 commits into from
Jan 23, 2024
Merged

Goのシリアライザの32bit対応 #77

merged 6 commits into from
Jan 23, 2024

Conversation

makiuchi-d
Copy link
Member

Goのシリアライザは64bit環境前提で作っていました(符号有/無の32bit intをどちらもintで扱っていた)
ところが、dashboardで使っているGopherjsが32bit環境をエミュレートしているため、桁溢れするときに正しく扱えていませんでした。

32bit環境でも変わらないよう、Int/UIntはint64で扱うようにして、テストも32bitで通るようにしました。

Unmarshalの結果がinterface{}で、一部型アサーションしてる部分があり、
この書きかえがコンパイル時チェックできないので、漏れがないか注意して見てほしいです。

i := int64(src[0]) << 24
i += int64(src[1]) << 16
i += int64(src[2]) << 8
i += int64(src[3])
Copy link
Member

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
}

https://github.com/golang/go/blob/2184a394777ccc9ce9625932b2ad773e6e626be0/src/encoding/binary/binary.go#L105-L109

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

別PRにするとコンフリクトするので、このPRでやってしまいます。

@makiuchi-d makiuchi-d requested a review from methane December 25, 2023 05:59
Copy link
Collaborator

@superkerokero superkerokero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご対応ありがとうございます 👍 👍 👍

Copy link
Member

@methane methane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

24bit以外のコードは encoding/binary を使えそうです。

Comment on lines 1209 to 1212
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)
Copy link
Member

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))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最適化を標準ライブラリに任せたほうがいいですねたしかに。

Comment on lines 1216 to 1219
i := int64(src[3])
i |= int64(src[2]) << 8
i |= int64(src[1]) << 16
i |= int64(src[0]) << 24
Copy link
Member

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))

@makiuchi-d makiuchi-d merged commit d6c8434 into main Jan 23, 2024
4 checks passed
@makiuchi-d makiuchi-d deleted the serializer-32 branch January 23, 2024 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants