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

バイト列の自動拡張 #6

Closed
kawasin73 opened this issue Nov 8, 2018 · 4 comments
Closed

バイト列の自動拡張 #6

kawasin73 opened this issue Nov 8, 2018 · 4 comments
Assignees

Comments

@kawasin73
Copy link
Owner

kawasin73 commented Nov 8, 2018

参考実装となる https://github.com/willf/bitset では、ビットベクトル作成時に全体のサイズを指定するものの、サイズを超過する値が設定されたときにスライス([]uint64)を自動拡張する。

bitset での自動拡張を行うかどうかを検討する

@kawasin73 kawasin73 mentioned this issue Nov 8, 2018
15 tasks
@kawasin73 kawasin73 self-assigned this Nov 8, 2018
@kawasin73
Copy link
Owner Author

ビットベクトルの拡張には対応しない。

zcbit は syscall.Mmap() でファイルにマッピングされたバイト列に対するビットベクトルの操作を考慮して作られている。
スライスの自動拡張は、スライスのキャパシティ(cap())が拡張後のサイズよりも小さい場合に、倍のサイズのメモリを新しく確保し、古いメモリのデータをコピーしてバイト列の参照を切り替えるという処理を行う。
mmap されたメモリから別のメモリに参照先が切り替えるとビットベクトルの操作による変更内容がファイルに反映されないバグにつながる。

そのためスライスサイズの拡張はユーザーが行い、拡張を行う場合は再度 zcbit.BitVec を作成し直すことで対応し、zcbit 単体ではスライスの自動拡張には対応しない。

@kawasin73
Copy link
Owner Author

バイト列の拡張に対応したい。

ただし、拡張してはいけない場合もあるので引数で拡張可能であるかどうかを渡す

@kawasin73 kawasin73 reopened this Dec 19, 2018
This was referenced Dec 19, 2018
@kawasin73
Copy link
Owner Author

Go のスライスは 1024 要素までは倍のサイズに拡張し、それ以上の場合は、1.25 倍に拡張する。

https://golang.org/src/runtime/slice.go#L121

If the slice does not have sufficient capacity, append will need to allocate new memory and copy the old one over. For slices with <1024 elements, it will double the capacity, for slices with >1024 elements it will increase it by factor 1.25.
https://stackoverflow.com/questions/17332227/big-o-of-append-in-golang

Go のスライスの実装に習い、1024 個より小さい要素の場合は倍のサイズにして、それ以上の場合は 1.25 倍のサイズにする

また、新しいサイズがオーバーフローする場合はエラーとする

@kawasin73
Copy link
Owner Author

対応した。

f69014b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant