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

julia types corresponding to flint types are unncessarily large

Open thofma opened this issue 10 months ago • 0 comments

The main offender is fq_default (flint) and FqFieldElem (Nemo). This is is a union with the largest size being 48 bytes (and the smallest being 8 byte, a single ulong). Of course, the point of this whole endeavor was to have a single type on the julia side, so using the official C-interface this must be

mutable struct fq_default
  data::NTuple{48, Int8}

  fq_default(ctx) = begin; z = new(); ccall((:fq_default_init, libflint), Cvoid, (Ref{fq_default}, Ref{fq_default_ctx}), z, ctx); z end
end

This makes every element of e.g. GF(2) quite large (8 times larger than it needs to be). I think any project/goal/plan of rewriting/recreating the wrappers for flint should try to fix this problem.

I discussed this often with @fieker, and we saw two possible solutions:

There is a relatively easy way to do this (which we already used at some other places). Let flint do the allocation, which will never be too large.

mutable struct fq_default
  ptr::Ptr{Nothing}

  fq_default() = ccall((:fq_default_alloc, libflint), Ptr{Nothing}, (Ref{fq_default_ctx}, ), ctx)

where fq_default_alloc needs to be added/patched into flint and which would look in principle as follows:

fq_default * fq_default_alloc(... ctx)
{
    fq_default * x;
    fq_default_init(x, ctx);
    return x;
}

Maybe there is another way?

thofma avatar Feb 01 '25 11:02 thofma