Skip to content

Commit

Permalink
Add Global Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Dec 11, 2024
1 parent 402ea4a commit 0257e55
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.com.caucho.hessian.io;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class GlobalCache {
private static final CacheItem[] CACHE_ITEMS = new CacheItem[16];

static {
for (int i = 0; i < CACHE_ITEMS.length; i++) {
CACHE_ITEMS[i] = new CacheItem();
}
}

static final AtomicReferenceFieldUpdater<CacheItem, byte[]> BYTES_UPDATER
= AtomicReferenceFieldUpdater.newUpdater(CacheItem.class, byte[].class, "bytes");


static final class CacheItem {
volatile byte[] bytes = new byte[8 * 1024];
}

public static byte[] getBytes() {
CacheItem cacheItem = CACHE_ITEMS[System.identityHashCode(Thread.currentThread()) & (CACHE_ITEMS.length - 1)];
return BYTES_UPDATER.getAndSet(cacheItem, null);
}

public static void putBytes(byte[] bytes) {
CacheItem cacheItem = CACHE_ITEMS[System.identityHashCode(Thread.currentThread()) & (CACHE_ITEMS.length - 1)];
BYTES_UPDATER.set(cacheItem, bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class Hessian2Output
// map of classes
private final IdentityHashMap<Object, Integer> _classRefs
= new IdentityHashMap<>(8);
private final byte[] _buffer = new byte[SIZE];
private final byte[] _buffer;
// the output stream/
protected OutputStream _os;
private int _refCount = 0;
Expand All @@ -96,6 +96,7 @@ public class Hessian2Output
private boolean _isPacket;

private boolean _isUnshared;
private final boolean _isUseGlobalCache;

/**
* Creates a new Hessian output stream, initialized with an
Expand All @@ -104,6 +105,14 @@ public class Hessian2Output
* @param os the underlying output stream.
*/
public Hessian2Output() {
byte[] bytes = GlobalCache.getBytes();
if (bytes == null) {
bytes = new byte[SIZE];
_isUseGlobalCache = false;
} else {
_isUseGlobalCache = true;
}
_buffer = bytes;
}

/**
Expand All @@ -113,6 +122,7 @@ public Hessian2Output() {
* @param os the underlying output stream.
*/
public Hessian2Output(OutputStream os) {
this();
init(os);
}

Expand Down Expand Up @@ -1554,6 +1564,10 @@ public void close()
OutputStream os = _os;
_os = null;

if (_isUseGlobalCache) {
GlobalCache.putBytes(_buffer);
}

if (os != null) {
if (_isCloseStreamOnClose)
os.close();
Expand Down

0 comments on commit 0257e55

Please sign in to comment.