microsoft/FASTER · error · ArgumentException

Size is not a power of 2

Error message

Size {0} is not a power of 2

What it means

FASTERBase.Initialize validates that the size parameter (e.g., hash index or overflow-bucket allocation size) is a power of two and fits in 32 bits. The power-of-2 check uses ArgumentException with message 'Size {0} is not a power of 2' when Utility.IsPowerOfTwo(size) is false. The {0} is never substituted, so the raw message appears.

Solutions

  1. Round the size up to the next power of 2 before calling Initialize
  2. Use Utility.NextPowerOf2(size) (or equivalent bit trick) to normalize user input
  3. Document/validate the config so only power-of-2 sizes are accepted

Example fix

// before
fasterBase.Initialize(1_000_000, sectorSize: 512); // throws
// after
long size = 1_000_000;
fasterBase.Initialize(Utility.NextPowerOf2(size), sectorSize: 512);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsPowerOfTwo(long x) => x > 0 && (x & (x - 1)) == 0;
if (!IsPowerOfTwo(size))
    size = Utility.NextPowerOf2(size);

Try / catch

try { fasterBase.Initialize(size, sectorSize); }
catch (ArgumentException ex) when (ex.Message.Contains("not a power of 2"))
{
    fasterBase.Initialize(Utility.NextPowerOf2(size), sectorSize);
}

Prevention

When it happens

Trigger: Calling Initialize(size, sector_size) with a size that is not a power of 2, e.g., 1_000_000 instead of 1<<20, or a config-computed size like (long)(n * 1.5).

Common situations: User-configured index sizes that ignore the power-of-2 requirement; computing sizes from entry counts without rounding up; porting configs from other systems that allow arbitrary sizes.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of microsoft/FASTER@321d872eab (2026-09-15). Data as JSON: /api/errors/06fd2b9f19171885. Report an issue: GitHub.

Appendix: source

Thrown at cs/src/core/Index/FASTER/FASTERBase.cs:400

        private void Free(int version)
        {
#if !NET5_0_OR_GREATER
            if (state[version].tableHandle.IsAllocated)
                state[version].tableHandle.Free();
#endif
        }

        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="size"></param>
        /// <param name="sector_size"></param>
        public void Initialize(long size, int sector_size)
        {
            if (!Utility.IsPowerOfTwo(size))
            {
                throw new ArgumentException("Size {0} is not a power of 2");
            }
            if (!Utility.Is32Bit(size))
            {
                throw new ArgumentException("Size {0} is not 32-bit");
            }

            minTableSize = size;
            resizeInfo = default;
            resizeInfo.status = ResizeOperationStatus.DONE;
            resizeInfo.version = 0;
            Initialize(resizeInfo.version, size, sector_size);
        }

        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="version"></param>
        /// <param name="size"></param>

View on GitHub (pinned to 321d872eab)