6
$\begingroup$

As my this previous post.I get a custom function which can help me got some function contain a certain option.And I have put it in my "init.m" file.

LookupOptionFunction[option_] := 
 Select[ToExpression[
   Complement[
    Select[Names["System`*"], 
     StringFreeQ["$"]], {"AllowTransliteration", "MyFind"}]], 
  KeyMemberQ[Options[#1], option] &]

Usage:

LookupOptionFunction[SelfLoopStyle]

{GraphPlot, GraphPlot3D, LayeredGraphPlot, TreeForm, TreePlot}

But there are some problem

enter image description here

As the @Bob Hanlon's answer,the speed up due to the caching produced in first time to run it.


My Question

How to save the caching to help me speed up to run it even we restart the Mathematica?


New progress:

I exclude the Keys@SystemOptions[] one by one and narrow it down from 90 to 17,I think following system options must can implement this demand:

{"CacheOptions","CatchMachineUnderflow","DataOptions","DefinitionsReordering","DifferentiationOptions","DynamicLibraryOptions","EnforceCallPacket","FileBackedCachingOptions","GlobFileNames","HolonomicOptions","LegacyFrontEnd","LegacyNewlineParsingInStrings","NeedNotReevaluateOptions","PostScriptBufferSize","RestorePackageDependencies","SymbolicProductThreshold","SymbolicSumThreshold"}

But I don't familar they by name.Maybe we just have a little bit of a difference

$\endgroup$
4
+100
$\begingroup$

The behavior you are encountering is the time taken by Mathematica evaluate all the Symbols in the System context, including definitions (and Options) that are only loaded on first use. (For one of my own encounters with this delayed loading please see Why do I have to evaluate this twice?)

In a fresh Kernel observe that GraphPlot has no Options:

Quit[]  (* quit the Kernel, then separately evaluate the line below *)

Options @ Unevaluated @ GraphPlot
{}

But when it is evaluated its definitions are loaded and it then has Options:

GraphPlot;  (* seemingly inert command that pre-loads GraphPlot definitions *)

Options @ Unevaluated @ GraphPlot
{AlignmentPoint -> Center, AspectRatio -> Automatic, Axes -> False, . . .

If we prevent all pre-loading and other evaluation by using Unevaluated as I did in the first example the initial search is quite fast:

Quit[] (* quit the kernel first *)

nopreloadLookupOptionFunction[option_] := 
  Complement @@ Names /@ {"System`*", "System`\\$*"} // ToHeldExpression // 
    Cases[ Hold[s_Symbol] :> s /; Quiet @ Options[Unevaluated @ s, option] =!= {} ]

nopreloadLookupOptionFunction[SelfLoopStyle] // AbsoluteTiming
{0.132444, {TreeForm}}

However we no longer have Symbols like GraphPlot etc. in the list because at the time of evaluation these had no Options. We therefore need to know which Symbols to preload, or which to not evaluate (because they are slow). I believe we can use the OwnValues of a Symbol to determine if it needs preloading by looking for the appearance of System`Dump`AutoLoad. Implementing this idea we have my

Proposed solution

SetAttributes[preload, HoldFirst]

preload[sym_Symbol] := 
  If[! FreeQ[Quiet @ OwnValues @ Unevaluated @ sym, System`Dump`AutoLoad], sym;]

fastLookupOptionFunction[option_] :=
  Names["System`*"] // ToHeldExpression //
    Cases[
     Hold[s_Symbol] :>
      s /; (preload @ s; Quiet @ Options[Unevaluated @ s, option]) =!= {}
    ]

Which in a fresh kernel yields:

fastLookupOptionFunction[SelfLoopStyle] // AbsoluteTiming
{1.47235, {GraphPlot, GraphPlot3D, LayeredGraphPlot, TreeForm, TreePlot}}

If this is still not fast enough for you or misses Options that are loaded in another way besides System`Dump`AutoLoad I suggest that you construct a separate database of the Symbol and Option names that you can search independent of evaluation.

$\endgroup$
  • $\begingroup$ Wonderful solution.. $\endgroup$ – yode Jul 7 '16 at 22:47
  • $\begingroup$ @yode Thank you :-) $\endgroup$ – Mr.Wizard Jul 7 '16 at 23:06
6
+50
$\begingroup$

You may make use of the built-in index caching for Entity objects; "WolframLanguageSymbol".

lookupOptionFunction[optionName_String] :=
 ToExpression /@
  EntityValue[EntityList[
    Entity["WolframLanguageSymbol", 
        {EntityProperty["WolframLanguageSymbol", "OptionNames"] -> optionName}]], 
   "Name"]]

First run after creating lookupOptionFunction.

lookupOptionFunction["Alignment"] // AbsoluteTiming
(* 26.2167, {ActionMenu, <<>>, Trigger}}

After quitting Mathematica, restarting, and recreating lookupOptionFunction in the notebook.

lookupOptionFunction["Alignment"] // AbsoluteTiming
(* 2.25438, {ActionMenu, <<>>, Trigger}}

Hope this helps.

$\endgroup$
  • $\begingroup$ Thanks for your help,upvote this method.I simplify it to be lookupOptionFunction[optionName_]:=CanonicalName[EntityList[Entity["WolframLanguageSymbol",{"OptionNames"->ToString[optionName]}]]].But I found when I change the option every time,it will give another data-downloading.like this. $\endgroup$ – yode Jun 1 '16 at 0:26
  • $\begingroup$ @yode Execute WolframLanguageData[]; to bring down the entire index. $\endgroup$ – Edmund Jun 1 '16 at 0:28
  • $\begingroup$ Yes,there is sure a course for this. $\endgroup$ – yode Jun 1 '16 at 0:30
  • $\begingroup$ But seem to cannot install the data of WolframLanguageData. $\endgroup$ – yode Jun 1 '16 at 0:37
  • $\begingroup$ @yode I think that method of installing the paclet data is no longer supported. Or, at least, it has stopped working. $\endgroup$ – Edmund Jun 1 '16 at 0:39

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.