Jump to content
h2ner

Improving Script Filter performance

Recommended Posts

Hello,

 

Now that my workflow overall is pretty fast, I'm starting to more notice things that could perhaps one day could be faster. It's possible such things may result in a difference in response time of less than a few dozen milliseconds, perhaps less than 20, unsure, but such is perceivable. It seems that for a Script Filter, Alfred will for each filter result, create all resources such as the icon before rendering. In contrast, Finder Spotlight results will display the list w/o icons and then later fill them in. The difference when filter results could be 100+ sort of at the moment as they say bugs me. Not necessarily a negative though it has me wondering how much more responsive it'd be as one enters a query in a script filter and having the results update. 

 

Also curious if someday workflow feedback might have an additional format that can be created, transmitted, received, and parsed faster than JSON. Perhaps some have been investigated, are under such, others may not yet be ready, and few yet have the needed language support.

Share this post


Link to post
Share on other sites
6 hours ago, h2ner said:

Alfred will for each filter result, create all resources such as the icon before rendering.

 

It doesn't. Alfred loads icons lazily after it's already displaying results.

 

6 hours ago, h2ner said:

Also curious if someday workflow feedback might have an additional format that can be created, transmitted, received, and parsed faster than JSON.

 

Doubtful. What's faster?

 

In any case, Alfred is plenty fast enough unless you're sending many, many thousands of results.

Share this post


Link to post
Share on other sites
8 hours ago, deanishe said:

Alfred is plenty fast enough unless you're sending many, many thousands of results.

 

With CardSearch I’m sending almost twenty thousand (and that number will ever only grow) results with modifiers, via an almost 9MB JSON file, and I find it pretty fast. It’s definitely faster than my previous method, where I was making API calls.


Prior to that prior option, I tried building the results from a non-Alfred-ready JSON, meaning I was remaking the results on every call. That solution was indeed somewhat slow.

Share this post


Link to post
Share on other sites

Yup. When Andrew added List Filters, he had them running buttery smooth filtering fifty thousand items.


Alfred is extremely fast.


I have no idea why your workflow might be slow. I get the same Expected end of line but found identifier. (-2741) error with both versions :( 

 

The code looks perfectly legit, even if I can't run it. FWIW, you don't need to manually mess around with aw.Cache and json here. There's already an initialised cache at wf.Cache and there's the Cache.StoreJSON method, so that whole function can be reduced to a single call:

 

func cacheLastQuery(queryString string) {
    if err := wf.Cache.StoreJSON("LastQuery", queryString); err != nil {
        panic(err)
    }
}

Same goes for getLastQuery() mutatis mutandis.

 

Share this post


Link to post
Share on other sites
Posted (edited)
7 hours ago, nikivi said:

How did you learn so many latin sayings?

 

Dunno. There just are loads of Latin sayings in English. I guess that one came from a contract or some other legal text?

Edited by deanishe

Share this post


Link to post
Share on other sites
Posted (edited)
22 hours ago, deanishe said:


I have no idea why your workflow might be slow. I get the same Expected end of line but found identifier. (-2741) error with both versions :( 

 

The general question of an alternative to JSON is just a question. Doesn't hurt to ask if someday it could even be better.

 

As for the error, do you think it occurs during the script filter? I was thinking calibre might have changed their preference file format and it was once not JSON. Perhaps there's an issue with reading calibre preferences to find the current EPUB. PDFs work ok?

 

As for the slowness, it's not that slow but it seems that the filter that filters bookmarks for the current book is slightly slower than searching all bookmarks. In that case, the query is slightly more complex and there is the variable getting. I''ll look into the query. 

Edited by h2ner

Share this post


Link to post
Share on other sites
Posted (edited)
2 hours ago, h2ner said:

The general question of an alternative to JSON is just a question. Doesn't hurt to ask if someday it could even be better.

 

There are plenty of faster alternatives. But there's about zero chance of Alfred supporting anything but JSON because JSON is so ubiquitous.

 

OTOH, there are plenty of JSON libraries for Go that are way faster than the built-in one (IIRC, Python's standard JSON library is—or at least was—faster than Go's).

 

2 hours ago, h2ner said:

As for the error, do you think it occurs during the script filter?

 

Presumably. It's an AppleScript error, after all. I haven't bothered trying to track it down because I hate AppleScript and I don't actually need the workflow.

 

2 hours ago, h2ner said:

it seems that the filter that filters bookmarks for the current book is slightly slower than searching all bookmarks

 

If you're running it via a Hotkey, it would be. You're wasting ~0.2s by saving a variable you don't actually want to save and then another ~0.2s deleting it again later:

tell application "Alfred 3"
        set configuration "JNANA_FILE" to value (quoted form of theFile) in workflow system attribute "alfred_workflow_bundleid"
    end tell

Calling Alfred's set configuration method is not the right way to set a workflow variable, and as I said in the other thread, tell application "..." is slow as hell.

 

This is why I said you need to benchmark code before you start optimising it. If you'd properly benchmarked the workflow's code, you'd know that the workflow is wasting a few tenths of a second with tell application "Alfred 3" calls it shouldn't be making.

 

I'd put money on those two unneeded tell application ... calls taking more time to run than you've saved by rewriting in Go :( 

 

EDIT: Here's the section on setting variables. You've followed the section on saving variables, which is not what you want.


If you only need to set one variable via AppleScript, you can return the value and use an Args & Vars to set a variable. If you need more than one, I'd recommend trying to rewrite the script in JXA. It's just as crazy as AppleScript in its own way, but it at least has perfect support for JSON.

Edited by deanishe

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...