# Can't make more than 249 InternalBags in a Table

I need a lot of InternalBags so I made them with Table. It works fine for 249 or fewer Bags but goes horribly wrong for 250 or more:

Table[InternalBag[0], {i, 249}]
(* {InternalBag["<" 1 ">"], ... } *)

Table[InternalBag[0], {i, 250}]
(* {0, 1, 2, ..., 249} *)


What's causing this weird behavior and how can I avoid it?

• This might possibly be fixed in the development version. – Daniel Lichtblau Jul 30 '19 at 20:44
• The issue seems fixed in v12.1 on Linux. – Oscillon Jun 8 at 19:42

Seems to be the same underlying issue as here: by default, Table compiles its argument when the number of values is 250 or more. Evidently InternalBag doesn't like this!

If all InternalBags are the same, then

ConstantArray[InternalBag[0], 250]


seems to work.

Otherwise, temporarily changing the TableCompileLength system option works:

tcl = SystemOptions["CompileOptions" -> "TableCompileLength"];
SetSystemOptions[
"CompileOptions" -> {"TableCompileLength" -> \[Infinity]}];
Table[InternalBag[0], {i, 250}]
SetSystemOptions[tcl];


Perhaps, a general heuristic: if anything starts breaking or slowing down at 250, suspect behind-the-scenes Compilation. See here, here, here, and here for more examples.

• The default return value for InternalBag from Compile is the bag ID or something That would be my assumption for what's breaking. Better than the return value from NIntegrateHeap (which it can't return even though it can use in compiled code). – b3m2a1 Jul 28 '19 at 17:08
• As you are temporarily(!) modifying system settings, I would recommend wrapping your business in InternalWithLocalSettings[], as was done here. – J. M.'s discontentment Jul 29 '19 at 3:19