Skip to content

Instantly share code, notes, and snippets.

@getchoo
Created December 3, 2024 04:29
Show Gist options
  • Select an option

  • Save getchoo/df684f6b14d2cc79a4a6e25c374abd6d to your computer and use it in GitHub Desktop.

Select an option

Save getchoo/df684f6b14d2cc79a4a6e25c374abd6d to your computer and use it in GitHub Desktop.

Revisions

  1. getchoo created this gist Dec 3, 2024.
    42 changes: 42 additions & 0 deletions optimized-java.nix
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    let
    pkgs = import <nixpkgs> {
    config = { };
    overlays = [ ];
    };
    inherit (pkgs) lib;

    # Packages we want to build optimized versions of
    packages = lib.getAttrs [
    "hello" # Test package

    "jdk8"
    "jdk17"
    "jdk21"
    ] pkgs;

    # Microarchitectures we want to optimize for
    microarchitectures = lib.attrNames lib.systems.architectures.features;

    # Do something for each microarchitecture
    forAllMicroarchitectures = lib.genAttrs microarchitectures;

    # Create an optimized stdenv for each microarchitecture
    stdenvFor = forAllMicroarchitectures (
    march:
    pkgs.stdenvAdapters.withCFlags [
    "-O3"
    "-march=${march}"
    "-mtune=${march}"
    ] pkgs.stdenv
    );

    # Override a given package with the stdenv optimized for the given microarchitecture
    applyOptimizations = march: package: package.override { stdenv = stdenvFor.${march}; };
    in

    # For each microarchitecture
    forAllMicroarchitectures (
    march:
    # Apply optimizations for each package
    lib.mapAttrs (lib.const (applyOptimizations march)) packages
    )