Giter Club home page Giter Club logo

Comments (3)

akarnokd avatar akarnokd commented on May 22, 2024

In case the capacity is power of 2, the following code I suggested in #45 should utilize the full capacity:

if (null == lvElement(buffer, calcElementOffset(index + 1, mask))) { // buffer is not full, keep last slot for resize indicator
    return writeToQueue(buffer, e, index, offset);
} else if (mask == maxSize) {
    if (null == lvElement(buffer, offset)) { // try using the last slot as there won't be any resize anymore
        return writeToQueue(buffer, e, index, offset);
    }
    // we're full and can't grow
    return false;
}

To fix the issue I would like to re-structure the queue such that a marker value is used to signal queue is full, but the link to next queue is pointed to in an extra slot.

What happens when the producer grows twice while the consumer is still in the first array?

Edit: I did some testing and the following test fails on the last assert:

@Test
public void testPowerOf2Capacity() {
    int n = 128;
    SpscGrowableArrayQueue<Integer> q = new SpscGrowableArrayQueue<>(8, n);

    for (int i = 0; i < n; i++) {
        assertTrue(q.offer(i));
    }
    assertFalse(q.offer(n));
}

The reason is that if the queue grows, the last grown array contains null in its front (up to index 62) and the producerLookAhead is adjusted so that it points to array index 0. Even if there is no look-ahead, the next producerIndex will point to a null element and thus accepting the offer, making the effective queue capacity 243 elements (7 + 15 + 31 + 63 + 127).

from jctools.

nitsanw avatar nitsanw commented on May 22, 2024

Fixed and test added

from jctools.

nitsanw avatar nitsanw commented on May 22, 2024

Test below:

@Test
public void testPowerOf2Capacity() {
    assumeThat(spec.isBounded(), is(true));
    int n = Pow2.roundToPowerOfTwo(spec.capacity);

    for (int i = 0; i < n; i++) {
        assertTrue("Failed to insert:"+i,queue.offer(i));
    }
    assertFalse(queue.offer(n));
}

from jctools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.