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

Consider extracting the first limb into a separate field #43

Open
konsumlamm opened this issue Nov 25, 2021 · 2 comments
Open

Consider extracting the first limb into a separate field #43

konsumlamm opened this issue Nov 25, 2021 · 2 comments

Comments

@konsumlamm
Copy link
Contributor

konsumlamm commented Nov 25, 2021

Currently, BigInt is defined as follows:

type
  BigInt* = object
    limbs: seq[uint32]
    isNegative: bool

It has the invariant that limbs.len >= 1.

It might be worth it changing that to

type
  BigInt* = object
    limbs: seq[uint32]
    first: uint32
    isNegative: bool

This has several advantages:

  • The invariant is now always satisfied (the first limb always exists).
  • Small BigInts require no allocation & pointer indirection.
  • It would avoid special casing 0 (see Empty limbs when uninitialized #26).

Disadvantages:

  • A bit more special handling is needed.
  • The first limb not being next to the other limbs in memory may make a difference (but I doubt it would be noticable).
@pietroppeter
Copy link
Contributor

Another option would be to consider Zero the one with empty limbs (which is what https://github.com/SciNim/megalo does)

@narimiran
Copy link
Member

I did try some "small BigInt optimization", but when I benchmarked it, I haven't noticed any measurable improvements. But there's a possibility that my tests weren't great and/or my implementation wasn't good enough.

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

No branches or pull requests

3 participants