Skip to content

Commit b309243

Browse files
committed
Document and benchmark core stdlib utilities
1 parent 502f839 commit b309243

4 files changed

Lines changed: 152 additions & 1 deletion

File tree

ROADMAP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Goal: reduce host-side boilerplate for common scripting tasks.
303303

304304
- [ ] Add comprehensive docs pages and examples for each new family.
305305
- [x] Add negative tests for malformed JSON/regex patterns.
306-
- [ ] Add benchmark coverage for hot stdlib paths.
306+
- [x] Add benchmark coverage for hot stdlib paths.
307307

308308
### v0.18.0 Definition of Done
309309

docs/introduction.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ dives on specific topics.
3030
behavior (exports, aliasing, policy hooks).
3131
- `examples/module_require.md` – practical example showing how to share
3232
helpers with `require` and module search paths.
33+
- `stdlib_core_utilities.md` – examples for JSON, regex, random IDs, numeric
34+
conversion, and common time parsing helpers.

docs/stdlib_core_utilities.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Core Stdlib Utilities
2+
3+
This page covers the core utility helpers added in v0.18.
4+
5+
## JSON
6+
7+
```vibe
8+
def parse_payload(raw)
9+
JSON.parse(raw)
10+
end
11+
12+
def emit_payload(record)
13+
JSON.stringify(record)
14+
end
15+
```
16+
17+
## Regex
18+
19+
```vibe
20+
def normalize_ids(text)
21+
Regex.replace_all(text, "ID-[0-9]+", "X")
22+
end
23+
24+
def first_id(text)
25+
Regex.match("ID-[0-9]+", text)
26+
end
27+
```
28+
29+
## Random IDs
30+
31+
```vibe
32+
def new_event_id()
33+
uuid()
34+
end
35+
36+
def short_token()
37+
random_id(8)
38+
end
39+
```
40+
41+
## Numeric Conversion
42+
43+
```vibe
44+
def parse_score(raw_score)
45+
to_int(raw_score)
46+
end
47+
48+
def parse_ratio(raw_ratio)
49+
to_float(raw_ratio)
50+
end
51+
```
52+
53+
## Common Time Parsing
54+
55+
`Time.parse` accepts common formats without manually passing a layout:
56+
57+
- RFC3339 / RFC3339Nano
58+
- RFC1123 / RFC1123Z
59+
- `YYYY-MM-DD` and `YYYY-MM-DD HH:MM:SS`
60+
- `YYYY/MM/DD` and `YYYY/MM/DD HH:MM:SS`
61+
- `MM/DD/YYYY` and `MM/DD/YYYY HH:MM:SS`
62+
63+
```vibe
64+
def parse_seen_at(raw)
65+
Time.parse(raw, in: "UTC")
66+
end
67+
```

vibes/execution_benchmark_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,85 @@ end`)
157157
}
158158
}
159159
}
160+
161+
func BenchmarkExecutionJSONParseLoop(b *testing.B) {
162+
engine := benchmarkEngine()
163+
script, err := engine.Compile(`def run(raw, n)
164+
total = 0
165+
for i in 1..n
166+
payload = JSON.parse(raw)
167+
total = total + payload[:score]
168+
end
169+
total
170+
end`)
171+
if err != nil {
172+
b.Fatalf("compile failed: %v", err)
173+
}
174+
175+
args := []Value{
176+
NewString(`{"score":7,"tags":["a","b","c"],"active":true}`),
177+
NewInt(80),
178+
}
179+
b.ReportAllocs()
180+
b.ResetTimer()
181+
for i := 0; i < b.N; i++ {
182+
if _, err := script.Call(context.Background(), "run", args, CallOptions{}); err != nil {
183+
b.Fatalf("call failed: %v", err)
184+
}
185+
}
186+
}
187+
188+
func BenchmarkExecutionJSONStringifyLoop(b *testing.B) {
189+
engine := benchmarkEngine()
190+
script, err := engine.Compile(`def run(payload, n)
191+
out = ""
192+
for i in 1..n
193+
out = JSON.stringify(payload)
194+
end
195+
out
196+
end`)
197+
if err != nil {
198+
b.Fatalf("compile failed: %v", err)
199+
}
200+
201+
payload := NewHash(map[string]Value{
202+
"id": NewString("player-7"),
203+
"score": NewInt(42),
204+
"active": NewBool(true),
205+
"tags": NewArray([]Value{NewString("a"), NewString("b"), NewString("c")}),
206+
})
207+
args := []Value{payload, NewInt(80)}
208+
b.ReportAllocs()
209+
b.ResetTimer()
210+
for i := 0; i < b.N; i++ {
211+
if _, err := script.Call(context.Background(), "run", args, CallOptions{}); err != nil {
212+
b.Fatalf("call failed: %v", err)
213+
}
214+
}
215+
}
216+
217+
func BenchmarkExecutionRegexReplaceAllLoop(b *testing.B) {
218+
engine := benchmarkEngine()
219+
script, err := engine.Compile(`def run(text, n)
220+
out = ""
221+
for i in 1..n
222+
out = Regex.replace_all(text, "ID-[0-9]+", "X")
223+
end
224+
out
225+
end`)
226+
if err != nil {
227+
b.Fatalf("compile failed: %v", err)
228+
}
229+
230+
args := []Value{
231+
NewString("ID-12 ID-34 ID-56 ID-78 ID-90"),
232+
NewInt(80),
233+
}
234+
b.ReportAllocs()
235+
b.ResetTimer()
236+
for i := 0; i < b.N; i++ {
237+
if _, err := script.Call(context.Background(), "run", args, CallOptions{}); err != nil {
238+
b.Fatalf("call failed: %v", err)
239+
}
240+
}
241+
}

0 commit comments

Comments
 (0)