# I found out something VERY IMPORTANT! And: Help from Nils! Update to KScript Editor! Thanks Nils!



## Dynamitec (Jul 25, 2006)

Hi everybody!

I found out it's not only the number of lines. It's the size of your script (and of your variable names!) that slows down kontakt! If you replace all variables with short name and save space where you can, it runs significantly faster! I really make a differents! My full Arp Script has now 5500 Lines compiled. And it's slow if you don't work in performance view. But: i replaced all variables to shorter ones and: now it works MUCH smoother.

So please, Kotori: If you are here again, PLEASE implement this to your editor. Replace all variables in compiled code with short ones: a1, a2, a3 for example...remove all unnecessary spaces and lines (like my CodeCompacter do)...

NI, what the hell is this?!


----------



## Big Bob (Jul 25, 2006)

*Re: I found out something VERY IMPORTANT!*

Hey Benj,

Do you think maybe K2 continuously scans the source text interpretively? Originally I thought that they ran interpretively but only after a pre-pass compilation to reduce the source text to a series of tokens. Now I'm beginning to wonder if they don't execute (interpretively) straight from the text (ugh!). Has anyone from NI ever stated how the KSP does its job?

Bob


----------



## Dynamitec (Jul 25, 2006)

*Re: I found out something VERY IMPORTANT!*

Hi Bob!

I really don't know. I don't thinks that it is a problem of the interpreter, because it seems really fast (i do A LOT of calculations without any slowdown). It's seems to be a problem of their UI handling. 

This is a really strange behavior of Kontakt!

What i observed:

1) If you have a very complex UI performance view is minimum twice as fast than the original view! You can have scripts with almost 10000 Lines running very smooth in performance view, but aren't useable anymore in original view.

2) Large scripts ( => many lines AND many kb) slow down standard view but not performance view

I really don't understand how this works and why this editor sucks so much. I mean: NI was able to program one of the most flexiblest samplers of the world (if not the flexiblest) but they can't program a simple texteditor that doesn't have any feature ?!?! :shock: (I'm not sure if Kotori could develop something like Kontakt, but in fact he did a great job developing a good and useable texteditor :mrgreen: Hello NI?! )

And i don't understand why they have such problems with such a slow down of the UI in standard view. It still sounds ok and the output is correct but UI isn't usable anymore.

And now i'm really dissapointed because i had to remove a lot of features of my large Patternmodul and i had to minimize the number of presets only because of the fact it wasn't usable in standard view anymore because the ui had a latency of almost 1s to react on clicks for example!


----------



## ComposerDude (Jul 25, 2006)

*Re: I found out something VERY IMPORTANT!*

Awhile back, Nils posted some interesting examples of delayed parameter evaluation; these might provide clues as to how much deferred binding is taking place.


----------



## kotori (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*

Since NI can't do their job, I guess I'll just take a look at writing a code compacter... :wink: 
That's a very interesting observation Benjamin, that shorter variable names matter. I'll sure look into this. 

Since the problem is only there when the editor is open the problem doesn't seem to have anything to do with the execution performance. Regarding NI statements about the performance I only know of one and it doesn't say very much:
my question
NI's answer

Nils


----------



## Dynamitec (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*

Hi Nils!

That's not absolutly correct. The problem isn't only if you have the editor open! Large scripts with more than 5000 lines slow down the UI in standard view even if the editor is closed! But: if you replace all variables and minimize the size of the script the UI gets a faster in standard view. Only in performance view size doesn't matter at all.

It would be very great if you replace all variables while compiling. I think it shouldn't be that hard because you have almost everythin written whats needed for this.

BTW: There is a very anoying bug in KScript Editor. Sometimes if you want to do search and replace or search alone, the editor shuts down...and if you haven't save you'll lose your work.


----------



## kotori (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*

Hi Benjamin,
thanks for correcting me. Apparently I read your post a bit too quickly and missed important parts. That's indeed very strange. 

I'm looking at replacing the variables (it would be an optional compilation setting).

Thanks for letting me know about the editor bug. I'll try to fix it. I think it has to do with the GUI library not being able to handle "Find" and "Find and Replace" dialogs at the same time. Until I fix this, please try not to open one of these when the other is already open.

Cheers,
Nils


----------



## Dynamitec (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*

Thank you Nils! I'm really looking forward to this!


----------



## kotori (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*



Dynamitec @ Sat Jul 29 said:


> Thank you Nils! I'm really looking forward to this!



I added a 1.0.1 version of the editor (no OSX version yet) with code compacting. The option to turn it on/off can be found in the View menu (a bit unlogical maybe, but there were already some other settings there). It removes all unnecessary white-space and comments, and renames all variables to $0, $1, $2 and so on. I had no code in place to determine which variables are UI controls and which are not, so since all variables are renamed one has to manually use set_text to get ui labels. In the process I guess I just invented a better script protection than the incredibly weak one built into Kontakt. However, I hope people will still continue to share their source code.

Nils


----------



## Dynamitec (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*

Wow Nils!

That was fast  Thank you 

Do you know how Kontakt encrypt the scripts?

Yeah, lol, now the scripts are almost on assembler level if you want to read this output 

Sure it's great to share, but sometimes if you have a commercial project going on, it's better to have some ideas saved...


----------



## Dynamitec (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*

Nils! By using you new compact code i can almost double the speed of my ui in my large arppegiator script in standard view!

With compact code: 
5215 Lines compiled => 93 KB

With your "old" compiler:
5874 Lines compiled => 203 KB

This really makes a differents! 45% of the old size!

Thank you very much!


----------



## kotori (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*



Dynamitec @ Sat Jul 29 said:


> Wow Nils!
> 
> That was fast  Thank you
> 
> Do you know how Kontakt encrypt the scripts?



You're welcome. I haven't tested it much, so please let me know if there's anything you want me to change/fix.
No, I don't know how the encryption works. I wish I new, then I could read and write table files and there would be no need for the fancy GUI tricks in my table utility. It's really unfortunate that NI don't open up their file formats. I believe they use an encrypted XML format, so maybe it would be possible to very easily write programs to generate nki files, and automatically map samples (the K2 doesn't exactly excel at mapping - strangely!).


----------



## Dynamitec (Jul 29, 2006)

Yes, i already asked NI, but they really don't give out the format. Absolutly no chance. So maybe one could find out how it is encrypted  But i'm not sure if this is legal.

I just wonder how sample converters can write NKI format. Do they have a deal with NI or are they just very good at reversed engineering?


----------



## kotori (Jul 29, 2006)

*Re: I found out something VERY IMPORTANT!*



Dynamitec @ Sat Jul 29 said:


> BTW: There is a very anoying bug in KScript Editor. Sometimes if you want to do search and replace or search alone, the editor shuts down...and if you haven't save you'll lose your work.


I have fixed this in the new 1.0.2 version. Well, actually it's a workaround - the program won't open a Find dialog when a 'Find and Replace' one is already open and vice versa. One has to manually close the open one to be able to open an instance of the other type. Not perfect, but it should stop the shutdowns. Thanks again for letting me know.

Btw. a nice thing about the new compact compiler output is that it should be pretty easy to add comments that indicate the original line number of each compiled line which could help when tracking down errors reported by end-users.

Cheers,
Nils


----------



## Dynamitec (Jul 29, 2006)

Hi Nils! Thank you very much for your fast implementation of the compact code output and fixing the annoying bug!


----------



## Giorgio Tommasini (Jul 30, 2006)

:???: 

Benjamin and Nils,

I used Nils' Kscript editor vers. 1.02. Loaded the Stradivari script, 1854 lines, original size 52.2 kB. After compression, 24.7 kB. However, no difference in CPU load was apparent by loading K2 2.1 with the compressed script, working in normal mode.

Am I missing something?

Thanks,

Giorgio


----------



## kotori (Jul 30, 2006)

Hi Giorgio,
Isn't the Stadivari script loaded in the performance view? According to Benjamin's observations that would mean it doesn't suffer from the same slowdown bug/inefficiency as non-perfview scripts. Do you get the same result if you remove the make_perfview line?

And btw. I cast my vote for Benjamin in the ongoing who-can-write-the-longest-post-subject-line competetition :razz: :mrgreen: 

Nils


----------



## Giorgio Tommasini (Jul 30, 2006)

Hi Nils,

I've been using a previous version of the script, actually not using at all the make_perfview command.

Giorgio


----------



## kotori (Jul 30, 2006)

I see. Maybe one has to get up to around 4000-5000 lines to notice any real difference...


----------



## Dynamitec (Jul 30, 2006)

Hi Nils!

Yes Nils, there is no significant slow down with less than 4000 lines. It gets critical with more than 5000 lines.

Btw: Thanks for voting :mrgreen: Hehe, i think this is one thing germans can do easily...because our language allows it to build almost endless words. So we are used to build large words and sentences.

An example:
"Der Donaudampfschiffskapitänskajütenklimaanlageneinschalteknopf ist rot.". 

This is a correct (but ironical used  word). It's say something like: 

The button of the air conditioning system of the captains cabin on a steam ship from the 'Donau' navigation company is red.

:shock:


----------



## Giorgio Tommasini (Jul 30, 2006)

Thanks Nils and Benjamin for clarifying this. 

Giorgio


----------

