Commit 5481f2e
committed
String solver: avoid cubic formulas for string-to-int conversions
Previously when the string radix was unknown we generated a conjunction of no-overflow checks
which was (in total) cubic in the number of digits in the string being converted to an integer.
For instance, it would define a series of 'sum's, like:
sum0 = digit0
sum1 = digit0 * radix + digit1
sum2 = (digit0 * radix + digit1) * radix + digit2
Where the definitions are truly inlined like this and don't use symbols to represent sum0...n,
so |sumn| is O(n), and the sum of all sums is O(n^2)
Then for each digit it would emit an overflow check:
constraint2 = size = 2 => nooverflow(sum1)
constraint3 = size = 3 => nooverflow(sum1) && nooverflow(sum2)
constraint4 = size = 4 => nooverflow(sum1) && nooverflow(sum2) && nooverflow(sum3)
However those sums are again expanded inline, meaning the sum of all constraints is O(n^3)
The obvious fix is to define symbols for sum0... sumn and then constraint0... constraintn,
but this slows the solver down considerably for small formulas (e.g. Byte.parseByte, with
max size 3 digits), so I compromise here by replacing the constraints with:
constraint2 = size >= 2 => nooverflow(sum1)
constraint3 = size >= 3 => nooverflow(sum2)
constraint4 = size >= 4 => nooverflow(sum3)
Thus the formula size is quadratic not cubic but still fast for cases where the radix is known.1 parent cccdfd9 commit 5481f2e
File tree
1 file changed
+13
-13
lines changed- src/solvers/strings
1 file changed
+13
-13
lines changedLines changed: 13 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
385 | 385 | | |
386 | 386 | | |
387 | 387 | | |
388 | | - | |
389 | 388 | | |
390 | 389 | | |
391 | 390 | | |
| |||
407 | 406 | | |
408 | 407 | | |
409 | 408 | | |
410 | | - | |
411 | | - | |
| 409 | + | |
412 | 410 | | |
413 | 411 | | |
414 | 412 | | |
| |||
424 | 422 | | |
425 | 423 | | |
426 | 424 | | |
| 425 | + | |
427 | 426 | | |
428 | 427 | | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
| 428 | + | |
| 429 | + | |
433 | 430 | | |
434 | 431 | | |
435 | 432 | | |
436 | | - | |
437 | | - | |
| 433 | + | |
438 | 434 | | |
439 | | - | |
| 435 | + | |
440 | 436 | | |
441 | | - | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
442 | 440 | | |
443 | 441 | | |
444 | 442 | | |
| 443 | + | |
| 444 | + | |
445 | 445 | | |
446 | | - | |
| 446 | + | |
447 | 447 | | |
448 | 448 | | |
449 | 449 | | |
450 | 450 | | |
451 | | - | |
| 451 | + | |
452 | 452 | | |
453 | 453 | | |
454 | 454 | | |
| |||
0 commit comments