nim-lang / bigints

BigInts for Nim
MIT License
124 stars 32 forks source link

Consider extracting the first limb into a separate field #43

Open konsumlamm opened 2 years ago

konsumlamm commented 2 years ago

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:

Disadvantages:

pietroppeter commented 2 years ago

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

narimiran commented 2 years ago

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.