GithubHelp home page GithubHelp logo

Comments (1)

cheenar avatar cheenar commented on May 28, 2024

The method exposing this function, getBindingOrThrow is defined as

<T> BindingImpl<T> getBindingOrThrow(Key<T> key, Errors errors, JitLimitation jitType)
      throws ErrorsException {
  // Check explicit bindings, i.e. bindings created by modules.
  BindingImpl<T> binding = bindingData.getExplicitBinding(key);
  if (binding != null) {
    return binding;
  }

  // Look for an on-demand binding.
  return getJustInTimeBinding(key, errors, jitType);
}

If possible, you could side-step this by just having your bindings explicitly in your module. Alternatively, I wonder if your proposal has any meaningful performance impact -- the synchronized block allows the cache to synchronously checked for a value before attempting to write to it. If you duplicated the read block, i.e. you just check the cache before the synchronized and then once again in the block, you do not lose the synchronous read/write (safe) but in exchange for the latency to recurse up.

private <T> Optional<BindingImpl<T>> findJitBindingFromCache(Key<T> key, Errors errors, JitLimitation jitType) throws ErrorsException {
  boolean jitOverride = isProvider(key) || isTypeLiteral(key) || isMembersInjector(key);
  for (InjectorImpl injector = this; injector != null; injector = injector.parent) {
    @SuppressWarnings("unchecked") // we only store bindings that match their key
    BindingImpl<T> binding = (BindingImpl<T>) injector.jitBindingData.getJitBinding(key);

    if (binding != null) {
      // If we found a JIT binding and we don't allow them,
      // fail.  (But allow bindings created through TypeConverters.)
      if (options.jitDisabled
          && jitType == JitLimitation.NO_JIT
          && !jitOverride
          && !(binding instanceof ConvertedConstantBindingImpl)) {
        throw errors.jitDisabled(key).toException();
      } else {
        return Optional.of(binding);
      }
    }
  }
  return Optional.empty();
}

/**
 * Returns a just-in-time binding for {@code key}, creating it if necessary.
 *
 * @throws ErrorsException if the binding could not be created.
 */
private <T> BindingImpl<T> getJustInTimeBinding(Key<T> key, Errors errors, JitLimitation jitType)
    throws ErrorsException {
  final Optional<BindingImpl<T>> cachedBinding = findJitBindingFromCache(key, errors, jitType);
  if (cachedBinding.isPresent()) {
    return cachedBinding.get();
  }

  synchronized (jitBindingData.lock()) {
    // first try to find a JIT binding that we've already created
    final Optional<BindingImpl<T>> synchronizedBinding = findJitBindingFromCache(key, errors, jitType);
    if (synchronizedBinding.isPresent()) {
      return synchronizedBinding.get();
    }

from guice.

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.