zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hanm <...@git.apache.org>
Subject [GitHub] zookeeper pull request #590: [ZOOKEEPER-1177] Add the memory optimized watch...
Date Sat, 22 Sep 2018 04:38:34 GMT
Github user hanm commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/590#discussion_r219659808
  
    --- Diff: src/java/main/org/apache/zookeeper/server/util/BitMap.java ---
    @@ -0,0 +1,125 @@
    +/**
    + * 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.zookeeper.server.util;
    +
    +import java.util.Map;
    +import java.util.HashMap;
    +import java.util.BitSet;
    +import java.util.concurrent.locks.ReentrantReadWriteLock;
    +
    +/**
    + * This is a helper class to maintain the bit to specific value and the
    + * reversed value to bit mapping.
    + */
    +public class BitMap<T> {
    +
    +    private final Map<T, Integer> value2Bit = new HashMap<T, Integer>();
    +    private final Map<Integer, T> bit2Value = new HashMap<Integer, T>();
    +
    +    private final BitSet freedBitSet = new BitSet();
    +    private Integer nextBit = Integer.valueOf(0);
    +
    +    private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    +
    +    public Integer add(T value) {
    +        Integer bit = getBit(value);
    +        if (bit != null) {
    +            return bit;
    +        }
    --- End diff --
    
    is it usual that we usually call this `add` method with same value over and over? If that's
the case, then this optimization is good, but if not, then this adds unnecessary cost of acquiring
and releasing the reader lock plus writer lock per `add`, as opposed to just acquiring and
releasing writer lock once per `add`. 


---

Mime
View raw message