aboutsummaryrefslogtreecommitdiffstats
path: root/prog/rand.go
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2024-01-12 13:35:45 +0100
committerAleksandr Nogikh <nogikh@google.com>2024-01-15 11:02:35 +0000
commit659da5e58b9a9c032a4127591796722120aa0c2f (patch)
treef223691565a57e21668a1dcc2ad5ca0f822a06b5 /prog/rand.go
parent551587c192ecb4df26fcdab775ed145ee69c07d4 (diff)
prog: restructure resource generation
If no matching resource was already present in the program, we used to substitute a random value in ~50% of cases. That's not efficient. Restructure the resource generation process so that, if there are no other options, we generate a new resource in 80% cases and in the remaining 20% we substitute an integer.
Diffstat (limited to 'prog/rand.go')
-rw-r--r--prog/rand.go22
1 files changed, 14 insertions, 8 deletions
diff --git a/prog/rand.go b/prog/rand.go
index 3fbaaaa87..0ca509e62 100644
--- a/prog/rand.go
+++ b/prog/rand.go
@@ -737,32 +737,38 @@ func (r *randGen) generateArgImpl(s *state, typ Type, dir Dir, ignoreSpecial boo
}
func (a *ResourceType) generate(r *randGen, s *state, dir Dir) (arg Arg, calls []*Call) {
+ canRecurse := false
if !r.inGenerateResource {
// Don't allow recursion for resourceCentric/createResource.
// That can lead to generation of huge programs and may be very slow
// (esp. if we are generating some failing attempts in createResource already).
r.inGenerateResource = true
defer func() { r.inGenerateResource = false }()
-
+ canRecurse = true
+ }
+ if canRecurse && r.nOutOf(8, 10) ||
+ !canRecurse && r.nOutOf(19, 20) {
+ arg = r.existingResource(s, a, dir)
+ if arg != nil {
+ return
+ }
+ }
+ if canRecurse {
if r.oneOf(4) {
arg, calls = r.resourceCentric(s, a, dir)
if arg != nil {
return
}
}
- if r.oneOf(3) {
+ if r.nOutOf(4, 5) {
+ // If we could not reuse a resource, let's prefer resource creation over
+ // random int substitution.
arg, calls = r.createResource(s, a, dir)
if arg != nil {
return
}
}
}
- if r.nOutOf(9, 10) {
- arg = r.existingResource(s, a, dir)
- if arg != nil {
- return
- }
- }
special := a.SpecialValues()
arg = MakeResultArg(a, dir, nil, special[r.Intn(len(special))])
return