PageSourceSearch

http://ibeetl.com/assets/index.html-BABWTGU6.js

js ibeetl.com collected 2026-10-03 23:19:53 UTC 10,844 bytes, 39 lines download raw bytes

1import{_ as a,c as s,b as n,o as i}from"./app-CNefOTx1.js";const t={};function p(l,e){return i(),s("div",null,e[0]||(e[0]=[n(`<h2 id="_5-1-重量级对象" tabindex="-1"><a class="header-anchor" href="#_5-1-重量级对象"><span>5.1 重量级对象</span></a></h2><p>重量级对象是那些构造需要较多初始化过程,或者使用过程中会做缓存的对象。比如,看Jackson的ObjectMapper类定义</p><div class="language-plaintext line-numbers-mode" data-ext="plaintext" data-title="plaintext"><button class="copy" title="Copy code" data-copied="Copied"></button><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code><span class="line"><span>public class ObjectMapper extends ObjectCodec implements Versioned, Serializable {</span></span>
2<span class="line"><span>    //忽略其他代码</span></span>
3<span class="line"><span>    protected SerializerFactory _serializerFactory;</span></span>
4<span class="line"><span>    protected DeserializationConfig _deserializationConfig;</span></span>
5<span class="line"><span>    protected DefaultDeserializationContext _deserializationContext;</span></span>
6<span class="line"><span>    protected Set&lt;Object _registeredModuleTypes;</span></span>
7<span class="line"><span>    protected final ConcurrentHashMap&lt;JavaType, JsonDeserializer&lt;Object _rootDeserializers;
7</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上类有较多需要初始化变量,尤其是集合类型。如果你看初始化构造函数,就能看出来,尤其是_rootDeserializers变量,初始化代码是</p><div class="language-plaintext line-numbers-mode" data-ext="plaintext" data-title="plaintext"><button class="copy" title="Copy code" data-copied="Copied"></button><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code><span class="line"><span> protected ObjectMapper(ObjectMapper src) {</span></span>
8<span class="line"><span>        this._rootDeserializers = new ConcurrentHashMap(64, 0.6F, 2);//忽略其他}</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div></div></div><p>这些集合类通常用来作为<code>缓冲</code>,缓存<code>预处理</code>或者<code>预编译</code>的对象,以提高性能。 如果每次使用时候,都构建ObjectMapper将导致这些优化战术失效</p><p>幸运的是,Jackson在官网文档已经说明到整个系统通常只需要一个ObjectMapper实例就可以了。</p><p>在代码中,如果把重量级对象当成轻量级对象使用,在方法中调用一次就构造一次,那么性能会急剧下降,一方面,构造这些重量级对象需要较多初始化过程,另外最重要的影响是,重量级对象会通常在反复使用中会缓存一些中间计算过程,ObjectMapper会缓存JsonDeserializer到 _rootDeserializers中,重量级对象通常包含Map,Set,ThreadLocal这些成员变量, 或者包含IO链接。 调用一次的时候就构造一次会导致应用性能急剧减低</p><p>如下是ObjectMapper每次使用时候的性能</p><div class="language-plaintext line-numbers-mode" data-ext="plaintext" data-title="plaintext"><button class="copy" title="Copy code" data-copied="Copied"></button><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code><span class="line"><span>@BenchmarkMode(Mode.Throughput)</span></span>
9<span class="line"><span>@Warmup(iterations = 5)</span></span>
10<span class="line"><span>@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)</span></span>
11<span class="line"><span>@Threads(1)</span></span>
12<span class="line"><span>@Fork(1)</span></span>
13<span class="line"><span>@OutputTimeUnit(TimeUnit.MILLISECONDS)</span></span>
14<span class="line"><span>@State(Scope.Benchmark)</span></span>
15<span class="line"><span>public class HeavyweightObjectTest {/*重用*/</span></span>
16<span class="line"><span>        ObjectMapper objectMapper = new ObjectMapper();</span></span>
17<span class="line"><span>        Data data = null;</span></span>
18<span class="line"><span>        @Benchmark</span></span>
19<span class="line"><span>        public Data createNew() throws IOException {/*每次都构造*/</span></span>
20<span class="line"><span>                ObjectMapper encodeMapper = new ObjectMapper();</span></span>
21<span class="line"><span>                String json = encodeMapper.writeValueAsString(data);</span></span>
22<span class="line"><span>                ObjectMapper decodeMapper = new ObjectMapper();</span></span>
23<span class="line"><span>                Data myData = decodeMapper.readValue(json,Data.class);return myData;</span></span>
24<span class="line"><span>        }</span></span>
25<span class="line"><span>        @Benchmark</span></span>
26<span class="line"><span>        public Data reused() throws IOException {</span></span>
27<span class="line"><span>                String json = objectMapper.writeValueAsString(data);</span></span>
28<span class="line"><span>                Data myData = objectMapper.readValue(json,Data.class);return myData;</span></span>
29<span class="line"><span>        }</span></span>
30<span class="line"><span>        @Setup</span></span>
31<span class="line"><span>        public void init(){</span></span>
32<span class="line"><span>                data = new Data();</span></span>
33<span class="line"><span>                data.setAge(18);</span></span>
34<span class="line"><span>                data.setId(123);</span></span>
35<span class="line"><span>                data.setName(&quot;lijiazhi&quot;);}</span></span>
36<span class="line"><span>        public static void main(String[] args) throws RunnerException {</span></span>
37<span class="line"><span>                Options opt = new OptionsBuilder().include(HeavyweightObjectTest.class.getSimpleName()).forks(1).build();new Runner(opt).run();}}</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>测试结果,重用重量级对象性能优势非常明显</p><div class="language-plaintext line-numbers-mode" data-ext="plaintext" data-title="plaintext"><button class="copy" title="Copy code" data-copied="Copied"></button><pre class="shiki shiki-themes vitesse-light vitesse-dark vp-code"><code><span class="line"><span>Benchmark                                   Mode  Samples     Score  Score error   Units</span></span>
38<span class="line"><span>c.i.c.c.HeavyweightObjectTest.createNew    thrpt        5    38.334        2.962  ops/ms</span></span>
39<span class="line"><span>c.i.c.c.HeavyweightObjectTest.reused       thrpt        5  2316.950       23.8
3907  ops/ms</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div>`,12)]))}const r=a(t,[["render",p],["__file","index.html.vue"]]),d=JSON.parse('{"path":"/article/performance/policy_heavyobject/","title":"性能优化-重量级对象","lang":"en-US","frontmatter":{"title":"性能优化-重量级对象","tags":["性能","新书"],"createTime":"2026/08/21 14:36:15","permalink":"/article/performance/policy_heavyobject/","description":"5.1 重量级对象 重量级对象是那些构造需要较多初始化过程,或者使用过程中会做缓存的对象。比如,看Jackson的ObjectMapper类定义 如上类有较多需要初始化变量,尤其是集合类型。如果你看初始化构造函数,就能看出来,尤其是_rootDeserializers变量,初始化代码是 这些集合类通常用来作为缓冲,缓存预处理或者预编译的对象,以提高性能...","head":[["meta",{"property":"og:url","content":"http://ibeetl.com/article/performance/policy_heavyobject/"}],["meta",{"property":"og:site_name","content":"闲大赋"}],["meta",{"property":"og:title","content":"性能优化-重量级对象"}],["meta",{"property":"og:description","content":"5.1 重量级对象 重量级对象是那些构造需要较多初始化过程,或者使用过程中会做缓存的对象。比如,看Jackson的ObjectMapper类定义 如上类有较多需要初始化变量,尤其是集合类型。如果你看初始化构造函数,就能看出来,尤其是_rootDeserializers变量,初始化代码是 这些集合类通常用来作为缓冲,缓存预处理或者预编译的对象,以提高性能..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2026-08-21T06:57:39.000Z"}],["meta",{"property":"article:tag","content":"性能"}],["meta",{"property":"article:tag","content":"新书"}],["meta",{"property":"article:modified_time","content":"2026-08-21T06:57:39.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"性能优化-重量级对象\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2026-08-21T06:57:39.000Z\\",\\"author\\":[]}"]]},"headers":[],"readingTime":{"minutes":1.94,"words":581},"git":{"updatedTime":1787295459000,"contributors":[{"name":"lijiazhi","username":"lijiazhi","email":"[email protected]","commits":1,"avatar":"https://gravatar.com/avatar/1b5114831459724ad4acc394423b4b48ecfa4a7e0659fbe07a0ce5181a1050a7?d=retro"}]},"autoDesc":true,"filePathRelative":"article/性能/heavyweight.md","categoryList":[{"id":"92a2b5","sort":10000,"name":"article"},{"id":"afdb50","sort":10004,"name":"性能"}]}');export{r as comp,d as data};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.