• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Hyshmily / hotkey / 28845108212

07 Jul 2026 06:00AM UTC coverage: 90.477% (+0.9%) from 89.623%
28845108212

push

github

Hyshmily
fix and perf: simplified the code and promote the Weigher accuracy,performance1

Signed-off-by: Hyshmily <cxm8607@outlook.com>

1312 of 1516 branches covered (86.54%)

Branch coverage included in aggregate %.

102 of 121 new or added lines in 5 files covered. (84.3%)

2 existing lines in 2 files now uncovered.

3714 of 4039 relevant lines covered (91.95%)

4.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/common/src/main/java/io/github/hyshmily/hotkey/cache/codec/DefaultWeigher.java
1
/*
2
 * Copyright 2026 Hyshmily. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.hyshmily.hotkey.cache.codec;
17

18
import com.github.benmanes.caffeine.cache.Weigher;
19
import io.github.hyshmily.hotkey.model.CacheEntry;
20
import org.jspecify.annotations.NonNull;
21

22
/**
23
 * Rough heap-weight estimator for {@link CacheEntry} values.
24
 *
25
 * <p>Used when {@code hotkey.local.cache.max-weight} is set. {@code String} values
26
 * are weighted by {@code length() * 2} (UTF-16 byte width, bounds object overhead
27
 * separately). Raw {@code byte[]} values are weighted by {@code length}. All other
28
 * types get a flat {@code 1024} byte estimate. Object headers and Caffeine internal
29
 * metadata are accounted via the overhead constants.
30
 */
31
@SuppressWarnings("all")
32
public final class DefaultWeigher implements Weigher<String, Object> {
33

34
  public static final DefaultWeigher INSTANCE = new DefaultWeigher();
×
35

36
  private static final int STRING_OVERHEAD = 48;      // String obj(24) + byte[] header(16) + padding
37
  private static final int BYTE_ARRAY_OVERHEAD = 24;  // byte[] header(16) + padding
38
  private static final int CACHE_ENTRY_OVERHEAD = 80; // ~5 long/int fields + object header + padding
39
  private static final int ENTRY_OVERHEAD = 512;      // Caffeine AccessOrderNode(40) + CHM.Node(32) + refs + alignment
40

41
  private DefaultWeigher() {}
42

43
  @Override
44
  public int weigh(String key, @NonNull Object value) {
45
    int valueWeight = valueOf(value);
×
46
    return (key.length() << 1) + valueWeight + ENTRY_OVERHEAD;
×
47
  }
48

49
  private static int valueOf(Object v) {
NEW
50
    if (v instanceof String s) return (s.length() << 1) + STRING_OVERHEAD;
×
NEW
51
    if (v instanceof byte[] b) return b.length + BYTE_ARRAY_OVERHEAD;
×
NEW
52
    if (v instanceof CacheEntry ce) return valueOf(ce.getValue()) + CACHE_ENTRY_OVERHEAD;
×
UNCOV
53
    return 1024;
×
54
  }
55
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc