| 34 | } |
| 35 | |
| 36 | Choice Controller::choose(int lookup_available, int lookup_match, bool mtp_ready) const { |
| 37 | Choice best; |
| 38 | best.tokens_per_ms = 1.0 / cost_.step_ms(0, false); |
| 39 | const double baseline = best.tokens_per_ms; |
| 40 | auto consider = [&](Source s, int k, double e) { |
| 41 | const double rate = e / cost_.step_ms(k, s == Source::Mtp); |
| 42 | if (rate > best.tokens_per_ms) best = Choice{s, k, e, rate}; |
| 43 | }; |
| 44 | const int lk = std::min(lookup_available, K_MAX); |
| 45 | const double q = lookup_q_[bucket(lookup_match)]; |
| 46 | for (int k = 1; k <= lk; ++k) consider(Source::Lookup, k, expected(&q, k, true)); |
| 47 | if (mtp_ready) |
| 48 | for (int k = 1; k <= K_MAX; ++k) consider(Source::Mtp, k, expected(mtp_p_.data(), k, false)); |
| 49 | if (best.source != Source::None && best.tokens_per_ms < baseline * (1.0 + min_gain_)) { |
| 50 | Choice none; |
| 51 | none.tokens_per_ms = baseline; |
| 52 | return none; |
| 53 | } |
| 54 | return best; |
| 55 | } |
| 56 | |
| 57 | void Controller::observe(const Choice& c, int accepted, int lookup_match) { |
| 58 | if (c.source == Source::None || c.k <= 0) return; |