openlang

Does this queue need a cached count?

The only thing the caller ever asks this queue is List.length items > 8, and it asks repeatedly. The queue is one immutable OCaml list, and every update returns a new queue. Two options are on the table. Carry an exact count, or expose a threshold query.

With (items, count), the pair pattern names both components, and from then on count = List.length items has to hold after every update. Enqueue adds one. A successful dequeue subtracts one. Dequeuing an empty queue leaves zero. Any constructor that takes both components has to keep them in step too.

The threshold version is smaller. List.compare_length_with items 8 > 0 says exactly what the caller wants, and the OCaml documentation says this comparison may stop after at most the supplied number of iterations. Right now nobody asks for the count itself, which makes me suspicious of storing one.

后续讨论 4

顺着彼此的依据读下去
  1. 固定链接

    Could we start with the question itself? exceedsLimit queue limit, with the limit required to be nonnegative. The caller gets the answer it asks for, and the queue doesn't grow a second component that has to be kept honest. Underneath, List.compare_length_with does the work for this representation.

    Where the caller is deciding whether to admit work, naming the outcomes might make the branches easier to read.

    type admission = WithinLimit | OverLimit
    
    let admit items limit =
      if List.compare_length_with items limit > 0
      then OverLimit
      else WithinLimit

    A plain boolean is fine too when the surrounding if already says what the distinction means. Wrapping a comparison in cases for its own sake isn't worth it.

    If exact counts turn out to be useful elsewhere, the cache can come then. When it does, construction and updates should sit behind the same interface, so no caller can replace items or count on its own. Until somebody asks for the number, I'd leave it out.

  2. 固定链接

    OverLimit tells a producer to stop. Then what? If producers block on it, count them. I see this most in Go, where a goroutine parked on a full channel is cheap, but nothing caps how many can park there, so the queue's limit just moves the pile somewhere nobody is measuring.

  3. 固定链接

    Same in Kotlin, nothing caps it unless you add something. A Channel with a fixed capacity suspends send when it is full, and every suspended sender is still a live coroutine holding whatever it captured. Their number is just how many you launched. Bound the launches, or put a Semaphore in front.

同一个问题的其他角度

  1. A slow gfortran loop that copied an array section on every call

    A loop over the rows of a matrix, calling a subroutine with the row a(i, :) on every iteration. Right answer, embarrassing sp…

    回复 2最近活动 fortran
  2. GOMAXPROCS in a container now follows the CPU limit

    Before Go 1.25, GOMAXPROCS defaulted to the logical CPU count of the machine. A container limited to two CPUs on a host with…

    回复 8最近活动 go
  3. try vs catch unreachable compares two different programs

    try and catch unreachable don't make the same promise, so timing one against the other measures two different programs. Both…

    回复 4最近活动 zig