Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THRIFT-4712: Improve Performance of ShortStack #1664

Merged
merged 1 commit into from Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/java/README.md
Expand Up @@ -170,3 +170,7 @@ The access modifier of the AutoExpandingBuffer class has been changed from
public to default (package) and will no longer be accessible by third-party
libraries.

The access modifier of the ShortStack class has been changed from
public to default (package) and will no longer be accessible by third-party
libraries.

Expand Up @@ -16,64 +16,59 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.thrift;
package org.apache.thrift.protocol;

import java.util.Arrays;

/**
* ShortStack is a short-specific Stack implementation written for the express
* purpose of very fast operations on TCompactProtocol's field id stack. This
* implementation performs at least 10x faster than java.util.Stack.
*/
public class ShortStack {
class ShortStack {

private short[] vector;
private int top = -1;

/** Always points to the next location */
private int top = 0;

public ShortStack(int initialCapacity) {
vector = new short[initialCapacity];
}

public short pop() {
return vector[top--];
return vector[--top];
}

public void push(short pushed) {
if (vector.length == top + 1) {
if (vector.length == top) {
grow();
}
vector[++top] = pushed;
vector[top++] = pushed;
}

private void grow() {
short[] newVector = new short[vector.length * 2];
System.arraycopy(vector, 0, newVector, 0, vector.length);
vector = newVector;
}

public short peek() {
return vector[top];
vector = Arrays.copyOf(vector, vector.length << 1);
}

public void clear() {
top = -1;
top = 0;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("<ShortStack vector:[");
for (int i = 0; i < vector.length; i++) {
boolean isTop = (i == (top - 1));
short value = vector[i];
if (i != 0) {
sb.append(" ");
sb.append(' ');
}

if (i == top) {
sb.append(">>");
}

sb.append(vector[i]);

if (i == top) {
sb.append("<<");
if (isTop) {
sb.append(">>").append(value).append("<<");
} else {
sb.append(value);
}
}
sb.append("]>");
Expand Down
Expand Up @@ -24,7 +24,6 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import org.apache.thrift.ShortStack;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransport;

Expand Down
116 changes: 0 additions & 116 deletions lib/java/test/org/apache/thrift/TestShortStack.java

This file was deleted.

42 changes: 42 additions & 0 deletions lib/java/test/org/apache/thrift/protocol/TestShortStack.java
@@ -0,0 +1,42 @@
/*
* 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 org.apache.thrift.protocol;

import junit.framework.TestCase;

public class TestShortStack extends TestCase {

public void testOps() throws Exception {
ShortStack s = new ShortStack(1);
s.push((short)10);
s.push((short)11);
s.push((short)12);
assertEquals((short)12, s.pop());
assertEquals((short)11, s.pop());
s.push((short)40);
assertEquals((short)40, s.pop());
assertEquals((short)10, s.pop());
try {
s.pop();
fail("should have thrown an exception!");
} catch (Exception e) {
// yay
}
}
}