Skip to content

Commit

Permalink
xrd: add Xdr#xdrEncodeShallowByteBuffer(Buffer)
Browse files Browse the repository at this point in the history
Motivation:
If client code already has grizzly Buffer direct use of it will avoud
extra allocations.

Modification:
introduce Xdr#xdrEncodeShallowByteBuffer(Buffer)

Result:
better performance when grizzy polled buffers are used.

Acked-by: Lea Morschel
Target: master
  • Loading branch information
kofemann committed Jan 3, 2023
1 parent 506c949 commit 032d7dc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions oncrpc4j-core/src/main/java/org/dcache/oncrpc4j/xdr/Xdr.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2023 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -768,14 +768,26 @@ public void xdrEncodeByteBuffer(ByteBuffer buf) {
* @param buf The buffer from which bytes are to be retrieved.
*/
public void xdrEncodeShallowByteBuffer(ByteBuffer buf) {
var wrap = new ByteBufferWrapper(buf);
wrap.allowBufferDispose(true);
xdrEncodeShallowByteBuffer(wrap);
}

/**
* A version of {@link #xdrEncodeShallowByteBuffer(ByteBuffer)} which uses Grizzly {@code buffer}.
* Note: any change to the {@code buf} will cause unpredicted behavior.
*
* @param buf The buffer from which bytes are to be retrieved.
*/
public void xdrEncodeShallowByteBuffer(Buffer buf) {
int len = buf.remaining();
int padding = (4 - (len & 3)) & 3;
xdrEncodeInt(len);
int ep = _buffer.position() + buf.remaining();
var b = new ByteBufferWrapper(buf);
b.allowBufferDispose(true);

var composite = BuffersBuffer.create(_memoryManager);
composite.append(_buffer.slice(0, _buffer.position())).append(b);
composite.allowBufferDispose(true);
composite.append(_buffer.slice(0, _buffer.position())).append(buf);
composite.position(ep);
composite.limit(ep);

Expand Down

0 comments on commit 032d7dc

Please sign in to comment.