BitIntegers.jl icon indicating copy to clipboard operation
BitIntegers.jl copied to clipboard

How to create bit integers in a loop? How to avoid additional allocations?

Open lampretl opened this issue 1 year ago • 1 comments

I'd like to create all BitIntegers of byte length up to 1024. I don't want to write 1024 lines of code for that, so I'm trying to use a loop. Running the following code gives an error:

for k=8:8:1024  k∈(8,16,32,64,128) && continue;  
    @eval BitIntegers.@define_integers $k Int$k UInt$k end

ERROR: syntax: invalid type signature around /home/user/.julia/packages/BitIntegers/VJRl1/src/BitIntegers.jl:62
Stacktrace:
 [1] top-level scope
   @ none:1
 [2] eval(m::Module, e::Any)
   @ Core ./boot.jl:370
 [3] top-level scope
   @ REPL[11]:2

Running the code below works but does not create my UInt's in the global scope:

for k=8:8:1024  k∈(8,16,32,64,128) && continue; 
    eval("BitIntegers.@define_integers $k Int$k UInt$k;") end

julia> UInt24
ERROR: UndefVarError: `UInt24` not defined

Is there a workaround for doing this?

Also, an unrelated question: If I create Matrix{Int128} and do row and column operations on it (like Smith normal form), that will cause no additional allocations. However, on Matrix{Int1024} there are many additional allocations. Is there a way to avoid this? Are your BitIntegers still converting internally to BigInts? If so, then what is the purpose of BitIntegers, what advantage do they offer compared to just using BigInts from the start?

Theoretically, would it be possible to refactor this package in a way that BitIntegers would behave more like Int64 and Int128 with regard to allocations? Or is the inherent nature of CPUs and RAMs such that this is impossible? (I noticed there are no integers of custom byte sizes in rust, go, ..., so I was wondering...)

lampretl avatar Jun 16 '24 09:06 lampretl

I already got an answer here: https://discourse.julialang.org/t/bitintegers-creation-in-loop-how-to-avoid-allocations/115935/2

lampretl avatar Jun 20 '24 18:06 lampretl