Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.format is slow #464

Open
buremba opened this issue Oct 18, 2020 · 5 comments
Open

std.format is slow #464

buremba opened this issue Oct 18, 2020 · 5 comments

Comments

@buremba
Copy link

buremba commented Oct 18, 2020

We're heavily using string concentration via std.format and I realized that it dramatically hurts the performance. We had to switch using the old way '' + '' and had a huge performance boost. Here is an example:

["%s and %s" % ['test', 'test1'] for x in std.range(0, 1000)]

The script above is compiled in ~5000ms whereas the following one only takes around 50ms:

local test = 'test';
local test1 = 'test1';

[test + 'and' + test1 for x in std.range(0, 1000)]

I see that std.format is implemented in Jsonnet but I believe it might be best to implement it as a native function as the logic is complex and it affects the performance dramatically.

@sbarzowski
Copy link
Collaborator

Well, std.format will always be somewhat slower than just using +, because it needs to parse the strings format and then concatenate the parts. Of course it will be way way faster if implemented as a builtin.

BTW probably a large part of 50ms in the latter example is actually printing the output. The calculation itself is probably even faster than that.

@sbarzowski sbarzowski changed the title std.format hurts the performance std.format is slow Oct 19, 2020
@buremba
Copy link
Author

buremba commented Oct 19, 2020

@sbarzowski sure, that's expected. On the other hand, it would be nice to have a convenient way to concentrate the string other than using '' + '' if std.format is designed to be more powerful rather than being the default way to concentrate the strings. It's hard to read the '' + '' format and most programming languages provide a simple way such as '${variable}'.

Jsonnet doesn't need to cover all the cases but I believe that string concentration is a common use-case for a data templating language. WDYT?

P.S: For one of our projects, almost 80% of the time is spent on std.format when compiling the Jsonnet.

@CertainLach
Copy link

Rust implementation of jsonnet (https://github.com/CertainLach/jrsonnet/) implements std.format in native, and its performance is almost same as string concatenation (while still taking 1/2 of golang impl time)

# a.jsonnet
["%s and %s" % ['test', 'test1'] for x in std.range(0, 1000)]
# b.jsonnet
local test = 'test';
local test1 = 'test1';

[test + 'and' + test1 for x in std.range(0, 1000)]
Command Mean [ms] Min [ms] Max [ms] Relative
./target/release/jrsonnet a.jsonnet 2.9 ± 0.1 2.6 3.7 1.20 ± 0.09
./target/release/jrsonnet b.jsonnet 2.4 ± 0.1 2.2 4.4 1.00
jsonnet a.jsonnet 521.0 ± 2.2 518.3 524.6 217.80 ± 13.39
jsonnet b.jsonnet 4.2 ± 0.2 3.9 5.9 1.74 ± 0.13

@sbarzowski
Copy link
Collaborator

That's nice! This is the implementaiton, right? https://github.com/CertainLach/jrsonnet/blob/8db09e1bbe2ea8a499da56d2b972a5510a654f82/crates/jrsonnet-evaluator/src/builtin/format.rs

We might want to use it as an inspiration here.

@CertainLach
Copy link

Yep, it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants