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

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

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

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

  • 1
    $\begingroup$ This might possibly be fixed in the development version. $\endgroup$ – Daniel Lichtblau Jul 30 '19 at 20:44
  • $\begingroup$ The issue seems fixed in v12.1 on Linux. $\endgroup$ – 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 Internal`Bag doesn't like this!

If all Internal`Bags are the same, then

ConstantArray[Internal`Bag[0], 250]

seems to work.

Otherwise, temporarily changing the TableCompileLength system option works:

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

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.