Skip to content

Using const fn constructor instead of struct expression with large number of constants causes compilation to fail because of large memory allocation. #68957

Closed
@CDirkx

Description

@CDirkx

I have:

  • A struct CodePoint with a const fn from(...) -> Self constructor
  • A large number of CodePoint constants (42.000+) using the constructor
#[repr(transparent)]
pub struct CodePoint {
    raw: u32
}

impl CodePoint {
    pub const fn from(raw: u32) -> Self {
        // debug_assert!(raw <= 0x10FFFF);
        CodePoint { raw }
    }
}

// include!(...)
// 42508 constants, 3.28MB file
impl CodePoint {
    pub const SPACE : CodePoint = CodePoint::from(32u32);
    ...
}

Building this leads to:

  • Compiler busy for 2min+
  • Compiler error trying to allocate 1.2GB:
    memory allocation of 1207959552 bytes failed
    even though I have 12GB of unused RAM

Using struct expressions instead of a const fn constructor:

impl CodePoint {
    pub const SPACE : CodePoint = CodePoint { raw: 32u32 };
    ...
}
  • Compiler finishes within 5s.

Tested on nightly and stable, with include!(...) and copy-pasting: no differences.

Source file to reproduce: reproduce.zip

Metadata

Metadata

Labels

A-associated-itemsArea: Associated items (types, constants & functions)I-compilememIssue: Problems and improvements with respect to memory usage during compilation.I-compiletimeIssue: Problems and improvements with respect to compile times.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions