Skip to content

Commit

Permalink
Some write optimizations for buffers and lazy initialization of heade…
Browse files Browse the repository at this point in the history
…rs. Fixed problem in FixedBufferData constructor.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
  • Loading branch information
spericas committed Jan 17, 2024
1 parent afa735d commit 6b4ada8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

class CompositeArrayBufferData extends ReadOnlyBufferData {
class CompositeArrayBufferData extends ReadOnlyBufferData implements CompositeBufferData {
private final BufferData[] data;

CompositeArrayBufferData(BufferData[] data) {
Expand All @@ -38,6 +38,13 @@ public BufferData rewind() {

@Override
public void writeTo(OutputStream out) {
if (data.length == 1) {
BufferData datum = data[0];
if (!(datum instanceof CompositeBufferData)) {
datum.writeTo(out);
return;
}
}
copy().writeTo(out);
}

Expand Down Expand Up @@ -260,4 +267,9 @@ public int get(int index) {
public String toString() {
return "comp-array: a=" + available();
}

@Override
public CompositeBufferData add(BufferData bufferData) {
throw new UnsupportedOperationException("Add not supported for " + getClass().getSimpleName() + " buffer");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,13 @@ public BufferData rewind() {

@Override
public void writeTo(OutputStream out) {
if (data.size() == 1) {
BufferData datum = data.get(0);
if (!(datum instanceof CompositeBufferData)) {
datum.writeTo(out);
return;
}
}
copy().writeTo(out);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,7 +43,7 @@ class FixedBufferData implements BufferData {
FixedBufferData(byte[] bytes, int position, int length) {
this.bytes = bytes;
this.length = length;
this.writePosition = length;
this.writePosition = position + length;
this.readPosition = position;
}

Expand Down
27 changes: 18 additions & 9 deletions http/http/src/main/java/io/helidon/http/HeadersImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,10 +34,11 @@ class HeadersImpl<T extends WritableHeaders<T>> implements WritableHeaders<T> {
Optimization for most commonly used header names
*/
private final Header[] knownHeaders = new Header[KNOWN_HEADER_SIZE];
// custom (unknown) headers are slower
private final Map<HeaderName, Header> customHeaders = new HashMap<>();
private IntSet knownHeaderIndices = new IntSet(KNOWN_HEADER_SIZE);

// custom (unknown) headers are slower
private Map<HeaderName, Header> customHeaders = null;

HeadersImpl() {
}

Expand Down Expand Up @@ -85,7 +86,7 @@ public Header get(HeaderName name) {

@Override
public int size() {
return customHeaders.size() + knownHeaderIndices.size();
return (customHeaders == null ? 0 : customHeaders.size()) + knownHeaderIndices.size();
}

@Override
Expand Down Expand Up @@ -167,7 +168,7 @@ public T set(Header header) {
}
int index = name.index();
if (index == -1) {
customHeaders.put(name, usedHeader);
customHeaders().put(name, usedHeader);
} else {
knownHeaders[index] = usedHeader;
knownHeaderIndices.add(index);
Expand All @@ -179,7 +180,7 @@ public T set(Header header) {
public T clear() {
Arrays.fill(knownHeaders, null);
knownHeaderIndices = new IntSet(KNOWN_HEADER_SIZE);
customHeaders.clear();
customHeaders().clear();
return (T) this;
}

Expand Down Expand Up @@ -231,20 +232,28 @@ public Header doRemove(HeaderName name) {
knownHeaderIndices.remove(index);
return value;
}
return customHeaders.remove(name);
return customHeaders().remove(name);
}

private Header find(HeaderName name) {
int index = name.index();

if (index > -1) {
return knownHeaders[index];
}

return customHeaders.get(name);
return customHeaders().get(name);
}

private Map<HeaderName, Header> customHeaders() {
if (customHeaders == null) {
customHeaders = new HashMap<>();
}
return customHeaders;
}

private class HeaderIterator implements Iterator<Header> {
private final boolean noCustom = customHeaders.isEmpty();
private final boolean noCustom = (customHeaders == null || customHeaders.isEmpty());

private boolean inKnown = true;
private int last = -1;
Expand Down

0 comments on commit 6b4ada8

Please sign in to comment.