Bug 473311

Summary: API Krita.instance().resources("preset") is really slow
Product: [Applications] krita Reporter: grum999
Component: ScriptingAssignee: Krita Bugs <krita-bugs-null>
Status: REPORTED ---    
Severity: normal CC: halla
Priority: NOR    
Version First Reported In: nightly build (please specify the git hash!)   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description grum999 2023-08-12 11:00:26 UTC
SUMMARY
When using method Krita.instance().resources("preset") for the first time in a Krita session, execution is pretty slow.

STEPS TO REPRODUCE
--> Ensure no plugin is active 

1. Open Scripter
2. Execute following script 
```
import time

ts=time.time()
l = Krita.instance().resources("preset")
print(len(l), time.time() - ts)
```


OBSERVED RESULT
I have 1058 presets on my DEV computer
-> First execution duration is ~3.932s
-> Next execution duration are ~0.019s

EXPECTED RESULT
For users with a lot of presets (I'm pretty sure to not be the only one :-)) a plugin that call this method will freeze Krita for a 'long' duration (on my side, at plugin startup)

Not sure why there's a such difference between first execution and next one, and if it's really possible or not to fix it.
I suppose the instantiation of 1058 Resources objects is the reason; if yes, in this case, a solution could be:
- implement Krita.instance().resourceNames("preset")
  ==> return only an array of String of resources names 
- implement Krita.instance().resource("preset", "resourcename")
  ==> return only one Resource object 

SOFTWARE/OS VERSIONS
Krita
 Version: 5.2.0-beta1 (git 0447d30)

Qt
  Version (compiled): 5.15.7
  Version (loaded): 5.15.7

OS Information
  Build ABI: x86_64-little_endian-lp64
  Build CPU: x86_64
  CPU: x86_64
  Kernel Type: linux
  Kernel Version: 5.19.0-45-generic
  Pretty Productname: KDE neon 5.27
  Product Type: neon
  Product Version: 22.04
  Desktop: KDE
  Appimage build: Yes

Hardware Information
  GPU Acceleration: auto
  Memory: 64201 Mb
  Number of Cores: 24
  Swap Location: /tmp
  Built for: sse2
  Base instruction set: fma3+avx2
  Supported instruction sets: fma3+avx2 avx2 fma3+avx avx fma4 fma3+sse4.2 sse4.2 sse4.1 ssse3 sse3 sse2 

REMARK
Problem is not relative to 5.2, it also occurs on
- 5.1.5 
- 5.3 
- 5.0.2 (even if it was faster, ~2.75s to load resources)
Comment 1 Halla Rempt 2023-11-20 12:52:45 UTC
Yes, this will load all presets, or rather, all specified resources. I'm not sure we should change the way this works, though, since people might be dependent on it.
Comment 2 grum999 2023-11-20 13:02:37 UTC
Hi

My proposal if this method can't be improved is to provide a split into 2 methods:
- implement Krita.instance().resourceNames("preset")
  ==> return only an array of String of resources names 
- implement Krita.instance().resource("preset", "resourcename")
  ==> return only one Resource object 

Then you can get list of preset without any freeze time 
And then later just load the preset(s) you need when needed