-
Notifications
You must be signed in to change notification settings - Fork 107
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
Using ManagedBufferPointer
instead of Array
as a storage
#97
Comments
As for any regressions: |
This sounds great. Did you already benchmark both approaches? |
This is a little bit more complicated. There is no silver bullet and there are multiple ways in which you can implement a Before I implement this change I want to close the #98 Using tests from “Violet - Python VM written in Swift”. The improvements (if any) would be only in some specific scenarios, definitely not in the most common case then the test looks like this: let a: BigInt = …
let b: BigInt = …
do something with them, maybe even I a loop…
In addition, things work well in Violet because we only have 2 representations:
In 99% of the cases we are Anyway, let's finish the #98 first and then (maybe) go back to this issue. |
Hi,
Recently I had to write my own
BigInt
implementation for Violet - Python VM written in Swift.Internally I decided to use ManagedBufferPointer instead of Swift
Array
. The whole design in one sentence would be: union (via tagged pointer) ofInt32
(calledSmi
, after V8) and a heap allocation (magnitude + sign representation) with ARC for garbage collection. The detailed explanation is available in our documentation.Naturally I'm quite curious why most of the
BigInt
libraries (including this one) useArray
. The current implementation gives you (2014 rMBP with Intel x64):Going with
ManagedBufferPointer
would give us much smaller numbers:I believe that this approach would have following advantages:
better usage of CPU cache - in the current design
BigInt
has size 33 and stride 40. WithManagedBufferPointer
we have size 18 and stride 24. This does not matter for aBigInt
as a type, but it may matter in real-life scenarios, for example when it has to be stored in astruct
on anArray
. (Just a reminder: intel cpus have 64 bytes cache line and M1 128 bytes, though I do not own the M1 device to check this).BigIntStorage
is specialized for storingWord
which means that it can do some things in a more efficient way thanSwift.Array
.potential further optimizations - I believe that you could bring the stride to 16, but then: inline value would be a single
Word
(instead of 2Words
) and the slicefrom/to
would have to beInt32
(instead ofInt
) + some minor rearrangement of how things are stored internally. It may not be worth it though.The downside is that you would have to implement your own heap storage based on
ManagedBufferPointer
, but this is not that difficult.The text was updated successfully, but these errors were encountered: